IPhone shooter game bullet physics!

Creating a new shooter game here, in the spirit of "Galagi" (my favorite shooter game is growing).

Here is the code 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];
}
}

In any case, the obvious problem is that when the bullet leaves the screen, its center is reset, so I reuse the same mark for each press of the fire button.

Ideally, I would like the user to spam the fire button without causing the program to crash. How would I redo this existing code so that the bullet object appears on the button every time and then disappears after it exits the screen or collides with an adversary?

Thanks for any help you can offer!

+3
source share
1

, , , , , .., .

, . , , , ( PLAYING, END_GAME, WAITING_FOR_OTHERPLAYERS) . ONE, , . . , "", , - , . , , " ". , , , ..

, ,

Kenny

: (.. ), . , , ACTUAL, , , , , , , , , , . ! Woo hoo!!

// implementation in MainGame class

  // After application has had its lunch....
- (void) applicationDidFinishLunching {
    gameTimer = [NSTimer scheduledTimerWithTimeInterval:GAMETIME_INTERVAL target:self selector:@selector(myGameLoop)    userInfo:nil repeats:TRUE];
}

- (void) startGame {
    gameState = PLAYING;
// And most likely a whole bunch of other stuff
}

NSMutableArray * mTransientObjects;

- (void) fireButtonPressed:(float) atY andTime:(NSTimeInterval) fireTime {
    // I will assume only a "Y" variable for this game, Galaga style.
    Bullet * aBullet = [[Bullet alloc] initWithY:atY atTime:fireTime];
    [mTransientObjects addObject:aBullet];

    [aBullet release];
}

// myGameLoop is called via the timer above firing at whatever interval makes sense for your game
- (void) myGameLoop {

switch (gameState) {
    case PLAYING:
        for (nextObject in mTransientObjects)
            [nextObject updateYourself:gameTime];

        // Now check for dead object, add explosions to mTransientObjects
        // Remove dead things, add anything new etc.
            etc.
}

// implementation in Bullet subclass
// Bullet is SubClass of your overall Sprite class
- (void) updateYourself:(NSTimerInterval) timeInterval  {
// Do any movement, change of animation frame, start sound....etc. etc.
}
+3

Source: https://habr.com/ru/post/1742569/


All Articles