Question

Hi Im making a cocos2d game that has a sprite flying and falling, Im trying to have a first tap touch event for example when the user touches the screen for the first time it'll start the game animation and start the physics engine. Whats happening is that when the user starts the game the sprite falls down right away, can anyone give me a hand with this?

right now Im using something like this but Im not sure how to get the physics engine to wait until the user touches the screen for the first time.

    CCSprite *_pixie
    CCNode *_start;
    BOOL *_firstTap;
    CCPhysicsNode *_physicsNode;

    -(void)didLoadFromCCB{
         _physicsNode.collisionDelegate = self;
         _firstTap = True;
    }

    - (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
         if(_firstTap == TRUE){
            _start.visible = FALSE;
           _firstTap = False;
         }

        //flying sounds & so on
         if (!_gameOver) {
           [[OALSimpleAudio sharedInstance] playEffect:MAGIC volume:0.4 pitch:1 pan:0 loop:NO];
           [_pixie.physicsBody applyImpulse:ccp(0, 420.f)];
           [_pixie.physicsBody applyAngularImpulse:11000.f];
           _sinceTouch = 0.f;
        }
    }

- (void)update:(CCTime)delta {

    if(_firstTap == FALSE){
        float yVelocity = clampf(_pixie.physicsBody.velocity.y, -1 * MAXFLOAT, 200.f);
       if ((_sinceTouch > .5f)) {
        [_pixie.physicsBody applyAngularImpulse:-40000.f*delta];    
       }
    }
}
Was it helpful?

Solution

Change

BOOL *_firstTap;

to

BOOL _firstTap;  //No asterisk

And also make sure that you set _firsttap = YES in viewDidLoad functions

- (void)viewDidLoad
{
    [super viewDidLoad];

    _firstTap = YES;
}

OTHER TIPS

Looks to me like the first touch Boolean value may not be defined until after the update code is called. You are also mixing BOOL values with bool values.

Objective-C : BOOL vs bool

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top