iPhone shooter game bullet physics!
Posted
by user298261
on Stack Overflow
See other posts from Stack Overflow
or by user298261
Published on 2010-04-24T07:03:24Z
Indexed on
2010/04/24
7:13 UTC
Read the original article
Hit count: 253
Hello,
Making a new shooter game here in the vein of "Galaga" (my fav shooter game growing up).
Here's the code I have for bullet physics:
-(IBAction)shootBullet:(id)sender{
imgBullet.hidden = NO;
timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(fireBullet) userInfo:Nil repeats:YES];
}
-(void)fireBullet{
imgBullet.center = CGPointMake(imgBullet.center.x + bulletVelocity.x , imgBullet.center.y + bulletVelocity.y);
if(imgBullet.center.y <= 0){
imgBullet.hidden = YES;
imgBullet.center = self.view.center;
[timer invalidate];
}
}
Anyway, the obvious issue is that once the bullet leaves the screen, its center is being reset, so I'm reusing the same bullet for each press of the "fire" button.
Ideally, I would like the user to be able to spam the "fire" button without causing the program to crash. How would I tinker this existing code so that a bullet object would spawn on the button press each time, and then despawn after it exits the screen, or collides with an enemy?
Thank you for any assistance you can offer!
© Stack Overflow or respective owner