如何让CCSequence检查TouchEnded是否为True

时间:2013-05-03 23:07:48

标签: objective-c cocos2d-iphone kobold2d

我有一个使用手指滑动移动的播放器精灵(ccTouchBegan到ccTouchMoved)。一旦TouchEnded我想停止我的行动,但我不想让它在中途停止行动。所以我想我需要我的onCallFunc来检查触摸是否已经结束,如果是这样的话,那就是exAute stopAllActions。关于如何做到这一点的任何想法?

-(void) onCallFunc
{
    // Check if TouchEnded = True if so execute [Player stopAllActions];
    CCLOG(@"End of Action... Repeat");
}

- (void)ccTouchMoved:

//Move Top
        if (firstTouch.y < lastTouch.y && swipeLength > 150 && xdistance < 150 && xdistance > -150) {        
            CCMoveTo* move = [CCMoveBy actionWithDuration:0.2  position:ccp(0,1)];
            CCDelayTime* delay = [CCDelayTime actionWithDuration:1];
            CCCallFunc* func = [CCCallFunc actionWithTarget:self selector:@selector(onCallFunc)];
            CCSequence* sequence = [CCSequence actions: move, delay, func, nil];
            CCRepeatForever* repeat = [CCRepeatForever actionWithAction:sequence];
            [Player runAction:repeat];
            NSLog(@"my top distance = %f", xdistance);
        }
        if (firstTouch.y < lastTouch.y && swipeLength > 151 && xdistance < 151 && xdistance > -151) {
            NSLog(@"my top distance = %f", xdistance);
        }

    }

我想要实现的目标:我试图通过让玩家使用触摸事件逐层移动来模拟像宠物小精灵和塞尔达这样的游戏中的动作。

更新:评论后创建BOOL标志 我是使用Objective-C的新手,但是我试图使用BOOL标志。我为每个部分收到了一个未使用的变量警告。

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    BOOL endBOOL = YES;
    NSLog(@"Stop Actions");
}

-(void) onCallFunc
{
    if(endBOOL == YES){
        [Player stopAllActions];
    }
    // Check if TouchEnded = True if so execute [Player stopAllActions];
    CCLOG(@"End of Action... Repeat");
}

1 个答案:

答案 0 :(得分:0)

您正在获取未使用的变量警告,因为您正在创建BOOL标志,然后从不使用它。在您的代码中:

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    BOOL endBOOL = YES;
    NSLog(@"Stop Actions");
}

在ccTouchesEnded中创建BOOL标志endBOOL,一旦该方法完成,BOOL就不再保留在内存中。要实现您想要的功能,您需要创建一个实例变量。

在.h内添加此

// Inside .h
BOOL endBOOL;

然后将代码更改为

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    endBOOL = YES;
    NSLog(@"Stop Actions");
}

这样就保留了endBOOL,你可以在if语句中使用它。另请注意,不需要(endBOOL == YES)。你可以简单地使用它:

-(void) onCallFunc
{
    if(endBOOL){
        [Player stopAllActions];
    }
    // Check if TouchEnded = True if so execute [Player stopAllActions];
    CCLOG(@"End of Action... Repeat");
}