sp3kk
sp3kk

Reputation: 23

Check whether another method is doing work when using Cocos2D's schedule: method

I'm trying to run a if statement that will take 3 arguments, where the one argument is whethe a method is running. Let me show my code below and try to clarify.

-(void) btnRightTapped: (id) sender{

    CCLOG(@"Left button Tapped");


    CGSize viewSize = [CCDirector sharedDirector].viewSize;

    if (_claw.position.x >= viewSize.width || _claw.position.x >= 1 && // HERE I WANT TO ADD THE ARGUMENT) {

        // Do stuff

        [self schedule:@selector(btnRightTapped:) interval:0.1];


    }


    else [self unschedule:@selector(btnRightTapped:)];

}

and the argument I want to add is to test whether the method btnRightTapped is currently running, which looks like this:

-(void) btnLeftTapped: (id) sender{

    CCLOG(@"Right button Tapped");



    CGSize viewSize = [CCDirector sharedDirector].viewSize;

    if (_claw.position.x < viewSize.width && // AND HERE) {


        // Do stuff


        [self schedule:@selector(btnLeftTapped:) interval:0.1];

    }


    else [self unschedule:@selector(btnLeftTapped:)];

}

What I'm trying to accomplish is to move my claw sprite when the button is pressed. Currently it only works to move the claw to one side, and as you try to move it back the if statement becomes true again and the schedules turn on and off for both buttons, making the sprite jump back and forth like crazy.

So I need to run the if statements with the current arguments PLUS the argument that the other button/method isn't pressed/running. How do I do this?

This is what I want in short:

if (_claw.position.x < viewSize.width && btnRightTapped.is.active == NO) 
{    
    do this and that

Upvotes: 0

Views: 163

Answers (2)

Maxime LM
Maxime LM

Reputation: 61

Can you try to unschedule a method when the other is scheduled ? Something like that :

-(void) btnRightTapped: (id) sender{

    CCLOG(@"Left button Tapped");

    CGSize viewSize = [CCDirector sharedDirector].viewSize;

    if (_claw.position.x >= viewSize.width || _claw.position.x >= 1) {

        CCLOG(@"Left button Tapped inside if statement");

        float clawSpeed = - viewSize.width / 10.0;
        float distanceToMoveClaw = clawSpeed;

        float newXClaw = _claw.position.x + distanceToMoveClaw;
        float newYClaw = _claw.position.y;

        _claw.position = ccp(newXClaw, newYClaw);

        [self schedule:@selector(btnRightTapped:) interval:0.1];
        [self unschedule:@selector(btnLeftTapped:)]; // New line here

    }


    else [self unschedule:@selector(btnRightTapped:)];
}

-(void) btnLeftTapped: (id) sender{

    CCLOG(@"Right button Tapped");

    CGSize viewSize = [CCDirector sharedDirector].viewSize;

    if (_claw.position.x < viewSize.width) {

        float clawSpeed = viewSize.width / 10.0;
        float distanceToMoveClaw = clawSpeed;

        float newXClaw = _claw.position.x + distanceToMoveClaw;
        float newYClaw = _claw.position.y;

        _claw.position = ccp(newXClaw, newYClaw);


        [self schedule:@selector(btnLeftTapped:) interval:0.1];
        [self unschedule:@selector(btnRightTapped:)]; // New line here
    }

    else [self unschedule:@selector(btnLeftTapped:)];
}

Upvotes: 1

jscs
jscs

Reputation: 64012

Use a boolean flag in an ivar to indicate that you have "scheduled" work running. Set it to NO when you do the "unschedule".

Upvotes: 2

Related Questions