0

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];    
       }
    }
}
  • I don't see anything right off hand that makes me think this code is wrong. Where is your code that starts the physics engine? – PWiggin Feb 28 '14 at 16:20
  • When using the above code the sprite falls down, what I want it to do is wait until the user touches the screen then let it fall down. What code will I have to use to stop the sprite from falling? – Anthony Orias Feb 28 '14 at 16:24
  • Sorry. I edited my question a little too late. Where is your code that starts the physics engine? – PWiggin Feb 28 '14 at 16:31
  • updated code above with physics (I think) – Anthony Orias Feb 28 '14 at 16:56

2 Answers2

0

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

Community
  • 1
  • 1
PWiggin
  • 956
  • 2
  • 12
  • 24
0

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;
}
Srikanth
  • 1,861
  • 17
  • 29
  • viewDidLoad doesnt work for me when using cocos2d, I have to use didLoadFromCCB instead (I think) – Anthony Orias Feb 28 '14 at 17:58
  • Thanks didnt know about 'onEnter', it seems to work but how can I prevent the sprite to fall before the first touch? – Anthony Orias Feb 28 '14 at 18:11
  • Does your physicsWorld or Space has gravity set to it? If yes, then set the gravity to zero in `onEnter` and then set it to desired gravity in `touchesBegan` function. – Srikanth Feb 28 '14 at 18:15
  • _physicsNode.paused = TRUE; works except that the sprite wont have animation, are you talking about the _physicsNode.position? sorry noob here – Anthony Orias Feb 28 '14 at 18:27
  • No. I am talking about the physicsWorld/Space. If your body has to fall, then the only reason is because of gravity which is getting applied on your body. By setting your physicsWorld or Space to zero gravity, your body will not fall. Something like `cpSpaceSetGravity( _space, cpv(0, 0) );` – Srikanth Feb 28 '14 at 18:34
  • Fixed it by using '_pixie.physicsBody.affectedByGravity = NO;' then doing YES when touch event is triggered. Thanks for helping me out! :) – Anthony Orias Feb 28 '14 at 18:46