2012-04-11 147 views
0

我正在用cocos2d在objective-c中制作iphone应用程序,在下面的代码中,我尝试检测碰撞并运行动画。 (box1通过触摸移动)碰撞检测Objective-C(cocos2d)

当“[self getChildByTag:d]”和“box1”碰撞并重叠时,我得到“JUMP NOW!”显示,但我没有得到跳转本身,但是当box1从“[self getChildByTag:d]”移开时发生跳转。

我知道这可能与多次调用该动作有关,但请向我解释发生了什么,请帮助我解决问题!

- (void)update:(ccTime)dt { 
    for (int d = lowestAvailableTag; d <= highestAvailableTag; d++) { 
     if ([self getChildByTag:d].position.y < (box1.position.y+45)&&    
      [self getChildByTag:d].position.x > (box1.position.x-45) && 
      [self getChildByTag:d].position.x < (box1.position.x+45)) { 

      NSLog(@"JUMP NOW!"); 

      if ([self getChildByTag:d].position.x < 150) { 
      [[self getChildByTag:d] runAction: 
       [CCJumpTo actionWithDuration:1.5 
         position:ccp(240, 140) height:110 jumps:1]]; 
      } 
     } 
    } 
} 

//阿尔巴

回答

0

您可以添加一些布尔标志来检测,如果你跳发生。水木清华这样的:

- (void) update:(ccTime)dt 
{ 
    if(jumpOccured == false) 
    { 
     BOOL needToJump = // your jump condition 
     if(needToJump == true) 
     { 
      // your jump code 

      jumpOccured = true; 
     } 
    } 
} 

顺便说一下,如果你有很多可能的冲突,你可以使用的Box2D检测到它们