使用Cocos2D的schedule:方法时,检查另一种方法是否正常工作

时间:2014-11-13 20:54:56

标签: ios objective-c cocos2d-iphone

我正在尝试运行一个带有3个参数的if语句,其中一个参数是一个方法正在运行。让我在下面展示我的代码并尝试澄清。

-(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:)];

}

我要添加的参数是测试方法btnRightTapped当前是否正在运行,如下所示:

-(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:)];

}

我想要完成的是按下按钮时移动我的爪子精灵。目前它只能将爪移动到一侧,当你试图将它移回时,if语句再次成为真,并且两个按钮的时间表打开和关闭,使得精灵像疯了一样来回跳跃。

所以我需要使用当前参数运行if语句PLUS没有按下/运行其他按钮/方法的参数。我该怎么做?

这就是我想要的简短:

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

2 个答案:

答案 0 :(得分:2)

在ivar中使用布尔标志表示您已经“安排”工作正在运行。当您执行“取消预定”时,将其设置为NO

答案 1 :(得分:1)

你可以尝试在安排另一个方法时取消安排方法吗?这样的事情:

-(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:)];
}