2013-05-03 40 views
0

我有一个使用手指滑动移动的播放器精灵(ccTouchBegan到ccTouchMoved)。我想让我的行动在TouchEnded后停止,但我不想让行动结束一半。所以我想我会需要我的onCallFunc来检查触摸是否结束,如果有的话,它有exicute stopAllActions。关于如何做到这一点的任何想法?如何让CCSequence检查TouchEnded = True

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

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

    } 

我想要实现:我试图模仿这样的比赛就像宠物小精灵和塞尔达进行的移动通过让玩家移动通过平铺使用触摸事件平铺。

更新:在创建布尔标志的注释后 我使用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"); 
} 
+0

添加一个布尔标志,并将其设置在触摸端,然后检查它onCallFunc。我不太喜欢永远重复的设计选择,但我没有得到你想要达到的。 – Ultrakorne 2013-05-03 23:18:22

+0

我添加了'我试图实现的内容':' – 2013-05-03 23:25:35

回答

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"); 
} 
相关问题