problem with game code!!!!

Member
Posts: 43
Joined: 2009.04
Post: #1
I am making a basic game that shoots when i touch the screen!! It runs fine except for one little detail the bullet moves faster each time i tap the screen.
Here is the source if anyone can help

Code:
#import "GameTestViewController.h"

@implementation GameTestViewController
@synthesize character;
@synthesize bullet;
@synthesize count;








- (void)viewDidLoad {
    count=0;
    bulletpos = CGPointMake(20.0,20.0);
    characterpos = CGPointMake(0,0);
    bullet.hidden=YES;
    [super viewDidLoad];
}



- (void)touchesBegan:(NSSet *)touches withEvent :(UIEvent *)event {
    bullet.hidden=NO;
    count=1;
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

}
-(void) onTimer {  
    if(count==1){
        character.center = CGPointMake(character.center.x+characterpos.x,220+220);
        bullet.center= CGPointMake(bullet.center.x+bulletpos.x,220+220);
        if(bullet.center.x > 320 || bullet.center.x < 0){
            bullet.center= CGPointMake(character.center.x+characterpos.x,220+220);
            bullet.hidden=YES;
            count=0;
        }    
    
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    bullet.hidden=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)dealloc {
    [bullet release];
    [character release];
    [super dealloc];
}

@end
Thanks
Chris
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
You're creating a new NSTimer every time -touchesBegan:withEvent: is called. You need to either recycle or invalidate the old one.
Quote this message in a reply
Member
Posts: 43
Joined: 2009.04
Post: #3
Can you please point me into the right direction on how to do that?
Thanks
Chris
Quote this message in a reply
Member
Posts: 43
Joined: 2009.04
Post: #4
Actually i think i might have it does this look better
Code:
- (void)viewDidLoad {
    count=0;
    bulletpos = CGPointMake(20.0,20.0);
    characterpos = CGPointMake(0,0);
    bullet.hidden=YES;
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

    [super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent :(UIEvent *)event {
    bullet.hidden=NO;
    count=1;

}


-(void) onTimer {  
    if(count==1){
        character.center = CGPointMake(character.center.x+characterpos.x,220+220);
        bullet.center= CGPointMake(bullet.center.x+bulletpos.x,220+220);
        if(bullet.center.x > 320 || bullet.center.x < 0){
            bullet.center= CGPointMake(character.center.x+characterpos.x,220+220);
            bullet.hidden=YES;
            count=0;
        }    
    
    }
}

Does this look better? It works fine now!! But i was just making sure there isn't anything else you see!!
Thanks
Chris
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #5
That'll more or less work, though on the iPhone, you'll want to be very careful about leaving NSTimers running. They consume power unnecessarily, which is undesirable anywhere but particularly of concern on the phone, since it's a mobile device with limited battery.

This is a fairly basic question compared to what it looks like you're trying to do with the code. I'd recommend you study the fundamentals of programming, write very simple test programs, and read lots of Apple documentation before you get too deeply into unknown territory.
Quote this message in a reply
Member
Posts: 43
Joined: 2009.04
Post: #6
oh i have read beginning iphone development, and learn c on the mac. so i know some stuff about proggrmaing i am just a basic noob at game dev. Can you please post some code to stop the timer?
Thanks
Chris
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #7
[timer invalidate];

Its right there in the docs, there are only 8 instance methods for NSTimer, I find it hard to believe you read the docs for NSTimer. Slow down and take your time, you can't rush programming.

You need to slow down when you post here and read over what you are writing as well.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2009.03
Post: #8
hello Chris,

Code:
- (void)viewDidLoad {
  count = 0;
  bullet.hidden = YES;
  [super viewDidLoad];
}

- (void)touchesBegan:(NSSet *)touches withEvent :(UIEvent *)event {
  if(count == 0) {
    bullet.center = CGPointMake(character.center.x,220+220);
    bulletTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    count = 1;
  }
}

- (void)onTimer {
  CGFloat bulletSpeedX = 10;
  bullet.center = CGPointMake(bullet.center.x+bulletSpeedX,220+220);
  if(bullet.center.x > 320 || bullet.center.x < 0){
    bullet.hidden = YES;
    count = 0;
    [bulletTimer invalidate];
  }
}

To make this work you need to declare bulletTimer as an NSTimer in your .h file.

iPlayful : games for iPhone and iPod Touch
Quote this message in a reply
Member
Posts: 43
Joined: 2009.04
Post: #9
Thansk, but if i invalidate it there won't that mean that i cant shoot anymore?
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #10
chrism Wrote:Thansk, but if i invalidate it there won't that mean that i cant shoot anymore?

Read the docs and understand what you have written the code to do.

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Questions Using Source Code and Drag & Drop Game Makers Thunder_Strike 2 2,138 Jul 12, 2013 07:59 AM
Last Post: Thunder_Strike
  Difficult Game to Code? mickyg 10 6,517 Mar 12, 2012 04:31 PM
Last Post: sealfin
  Problem with making a game with the iPhone Game Development book MrPenguin9 2 5,444 Feb 1, 2010 09:13 AM
Last Post: MrPenguin9