Objective c 碰撞检测目标-C(cocos2d)

Objective c 碰撞检测目标-C(cocos2d),objective-c,ios,cocos2d-iphone,Objective C,Ios,Cocos2d Iphone,我正在用objective-c和cocos2d制作一个iphone应用程序,在下面的代码中,我尝试检测碰撞,然后运行动画。(通过触摸移动盒子1) 当“[self-getChildByTag:d]”和“box1”发生碰撞和重叠时,我会显示“立即跳转!”,但不会显示跳转本身,但当box1从“[self-getChildByTag:d]”移开时,会发生跳转 我理解这可能与动作被多次调用有关,但请向我解释到底发生了什么,并请帮助我找到解决方案 - (void)update:(ccTime)dt {

我正在用objective-c和cocos2d制作一个iphone应用程序,在下面的代码中,我尝试检测碰撞,然后运行动画。(通过触摸移动盒子1)

当“[self-getChildByTag:d]”和“box1”发生碰撞和重叠时,我会显示“立即跳转!”,但不会显示跳转本身,但当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]];
          }
      }
   }
}
-(void)更新:(ccTime)dt{
对于(int d=较低的可用标签;d(框1.位置x-45)和
[self-getChildByTag:d].位置.x<(框1.位置.x+45)){
NSLog(@“立即跳转!”);
if([self-getChildByTag:d].position.x<150){
[[self-getChildByTag:d]运行操作:
[持续时间内的行动:1.5
位置:ccp(240140)高度:110跳:1];
}
}
}
}

//albar

您可以添加一些BOOL标志来检测是否发生跳转。Smth类似:

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

            jumpOccured = true;
        }
    }
}
顺便说一下,如果您有许多可能的碰撞,您可以使用box2d来检测它们