0
votes

I am using UIPanGestureRecognizer to detect swipes in my app. The gesture recogniser fires correctly but when I set a variable or try to call a function from within it the variables are set but, on next iteration (of my tick function for example) the variable is not set. It is as if it is setting it on a different instance? Can anyone help me out here?

So _ApplyForce gets set to true in the handlePan function BUT it is always false when I try to read it elsewhere (in my tick function).

Update: I have added all the places _ApplyForce is used to help show how it is used. Also, the reason I use this variable to trigger off the force impulse is because of this question where I couldn't seem to trigger a force from within the gesture function, after some investigation it turned out I couldnt change or alter any class variable - hence the new question.

AppDelegate.mm:

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
    ....
    HelloWorldLayer *layer = (HelloWorldLayer *) [[HelloWorldLayer scene].children objectAtIndex:0];
    UIPanGestureRecognizer *gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:layer action:@selector(handlePanFrom:)] autorelease];
    [viewController.view addGestureRecognizer:gestureRecognizer];
}

HelloWorldLayer.h

@interface HelloWorldLayer : CCLayer
{
    ...

    bool _ApplyForce;
}

HelloWorldLayer.mm

- (id)init {
    if ((self=[super init])) {
         _ApplyForce = false;
    }
}

- (void)tick:(ccTime) dt {
    _world->Step(dt, 10, 10);

    if (_ApplyForce)
    {
        NSLog(@"apply force");
        b2Vec2 force = b2Vec2(200, 200);
        _body->ApplyLinearImpulse(force,_body->GetWorldCenter());
        _ApplyForce = false;
    }
}

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateEnded) {
            _ApplyForce=true;
    }      
}
1
where is _ApplyForce declared ? are sure nothing else is altering the value ? - user756245
Sorry - its declared in the HelloWorldLayer.h - It only gets altered in the init function and that is to set it to false. - Chris
is it a BOOL ? this is really strange - user756245
add the entire code where you use or define _ApplyForce, it's easier to find your problem. - Cyprian
@Cyprian done - thankyou - Chris

1 Answers

0
votes

Isn't that because in your tick method if _ApplyForce is true the end of the statement sets it to false? Which would mean that immediately after it is true it is false.

I might recommend adding some NSLogs to track ticks and _ApplyForce states.