• <i id='ixanR'><tr id='ixanR'><dt id='ixanR'><q id='ixanR'><span id='ixanR'><b id='ixanR'><form id='ixanR'><ins id='ixanR'></ins><ul id='ixanR'></ul><sub id='ixanR'></sub></form><legend id='ixanR'></legend><bdo id='ixanR'><pre id='ixanR'><center id='ixanR'></center></pre></bdo></b><th id='ixanR'></th></span></q></dt></tr></i><div id='ixanR'><tfoot id='ixanR'></tfoot><dl id='ixanR'><fieldset id='ixanR'></fieldset></dl></div>

    <small id='ixanR'></small><noframes id='ixanR'>

  • <legend id='ixanR'><style id='ixanR'><dir id='ixanR'><q id='ixanR'></q></dir></style></legend>

    • <bdo id='ixanR'></bdo><ul id='ixanR'></ul>

      <tfoot id='ixanR'></tfoot>

        Cocos2d 游戏中的碰撞检测?

        时间:2024-08-12
        <tfoot id='SehLN'></tfoot>
          1. <small id='SehLN'></small><noframes id='SehLN'>

            • <bdo id='SehLN'></bdo><ul id='SehLN'></ul>
              <legend id='SehLN'><style id='SehLN'><dir id='SehLN'><q id='SehLN'></q></dir></style></legend>

                <i id='SehLN'><tr id='SehLN'><dt id='SehLN'><q id='SehLN'><span id='SehLN'><b id='SehLN'><form id='SehLN'><ins id='SehLN'></ins><ul id='SehLN'></ul><sub id='SehLN'></sub></form><legend id='SehLN'></legend><bdo id='SehLN'><pre id='SehLN'><center id='SehLN'></center></pre></bdo></b><th id='SehLN'></th></span></q></dt></tr></i><div id='SehLN'><tfoot id='SehLN'></tfoot><dl id='SehLN'><fieldset id='SehLN'></fieldset></dl></div>
                    <tbody id='SehLN'></tbody>
                1. 本文介绍了Cocos2d 游戏中的碰撞检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试通过以下方式检测两个精灵碰撞...但是当我尝试运行游戏时没有发生碰撞......什么我可能做错了吗?

                  i am trying to detect Collision of two sprites in the following way...but when i try to run the game no collision takes place.... what am i possibly doing wrong??

                  - (void)update:(ccTime)dt {
                  
                  
                  
                      CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), 
                                                         projectile.position.y - (projectile.contentSize.height/2), 
                                                         projectile.contentSize.width, 
                                                         projectile.contentSize.height);
                  
                      //CGRectMake(0,220,320,50);
                      CGRect targetRects =  CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2), 
                                                   _monkey.position.y - (_monkey.contentSize.height/2), 
                                                   _monkey.contentSize.width, 
                                                   _monkey.contentSize.height);
                  
                          if (CGRectIntersectsRect(projectileRect, targetRects)) {
                                      NSLog(@"ha ha Collision detected"); 
                          }                       
                  

                  }

                  projectile 精灵从屏幕顶部到底部动画,monkey 精灵在底部从左到右动画 弹丸穿过猴子,但日志确实不被叫???

                  projectile sprite is animating from top of screen to the bottom and monkey sprite is animating from left to right at the bottom the projectile goes through the monkey but the log does not get called???

                  - (void)update:(ccTime)dt {
                  
                  CGRect projectileRect = [projectile boundingBox];
                  CGRect targetRects = [_monkey boundingBox];
                  
                  if (CGRectIntersectsRect(projectileRect, targetRects))
                  {
                      NSLog(@"ha ha Collision detected");
                  }
                  
                  CGRect projectileRects = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), 
                                                     projectile.position.y - (projectile.contentSize.height/2), 
                                                     projectile.contentSize.width, 
                                                    projectile.contentSize.height);
                  CGRect targetRect = CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2), 
                                                 _monkey.position.y - (_monkey.contentSize.height/2), 
                                                 _monkey.contentSize.width, 
                                                _monkey.contentSize.height);
                  if (CGRectIntersectsRect(projectileRects, targetRect)) {
                      NSLog(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@");             
                  }
                  

                  }

                  -(void)spriteMoveFinished:(id)sender {

                  -(void)spriteMoveFinished:(id)sender {

                  //NSLog(@"spriteMoveFinished");
                  CCSprite *sprite = (CCSprite *)sender;
                  [self removeChild:sprite cleanup:YES];
                  
                  if (sprite.tag == 1) { 
                      [_targets removeObject:sprite];
                  
                  
                  
                  } else if (sprite.tag == 2) { 
                      [_projectiles removeObject:sprite];
                  }
                  

                  }

                  -(void)addTarget {

                  -(void)addTarget {

                  projectile = [CCSprite spriteWithFile:@"egg.png" rect:CGRectMake(0, 0, 10, 10)];
                  projectile.position = ccp(_bear.position.x,_bear.position.y-20);
                  projectile.tag=2;
                  [self addChild:projectile];
                  
                  CGPoint realDest = ccp(_bear.position.x, _bear.position.y - 380);
                  
                  int minDuration = 2.0;
                  int maxDuration = 4.0;
                  int rangeDuration = maxDuration - minDuration;
                  int actualDuration = (arc4random() % rangeDuration) + minDuration;
                  
                  // Move projectile to actual endpoint
                  [projectile runAction:[CCSequence actions:
                                         [CCMoveTo actionWithDuration:actualDuration position:realDest],
                                         [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
                                         nil]];
                  
                  // Add to projectiles array
                  projectile.tag = 2;
                  [_projectiles addObject:projectile];
                  

                  }

                  -(void) registerWithTouchDispatcher
                  {
                      [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
                  }
                  

                  -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

                  -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

                  CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
                  
                  
                  if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation)) 
                  {
                      if (![_walkMonkey isDone]) {
                          [_monkey runAction:_walkMonkey];
                      }
                  
                  
                  }
                  else {
                  
                  }
                  
                  return YES;
                  

                  }

                  -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {

                  -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {

                  CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
                  
                  if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation)) 
                  {
                  
                      [_monkey stopAction:_walkMonkey];
                  
                  }
                  

                  }

                  - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {       
                  
                      CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
                  
                      CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
                      oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
                      oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
                  
                      CGPoint translation = ccpSub(touchLocation, oldTouchLocation);    
                  
                      if (translation.x > 3) {
                  
                          _monkey.flipX=YES;
                      }
                      else if (translation.x < -3){
                          _monkey.flipX=NO;
                  
                      }
                  
                      if(CGRectContainsPoint(CGRectMake(40,0,240,50),touchLocation)) 
                      {
                  
                          CGPoint newPos = ccpAdd(translation,_monkey.position);
                          if(newPos.x >= 320 || newPos.x <= 20)
                          {
                              NSLog(@"monkey not walking");
                          }
                          else {
                              newPos.y = 100;
                              _monkey.position = newPos;
                          }
                  
                      }
                  
                  }
                  

                  推荐答案

                  你应该使用内置功能:

                  CGRect projectileRect = [projectile boundingBox];
                  CGRect targetRects = [_monkey boundingBox];
                  
                  if (CGRectIntersectsRect(projectileRect, targetRects))
                  {
                      NSLog(@"ha ha Collision detected");
                  }
                  

                  boundingBox 方法考虑了更多的事情,例如,如果节点相对于其父节点定位.

                  The boundingBox method takes a couple more things into account, for example if the node is positioned relative to its parent.

                  另外请注意,在 Objective-C 中使用下划线作为变量前缀被认为是不好的做法.Apple 为其内部库保留带有前导下划线的变量名称.如果确实需要对实例变量进行分类,一种常见的方法是在它们后面加上下划线.

                  Also note that prefixing variables with an underscore is considered bad practice in Objective-C. Apple reserves variable names with leading underscores for their internal libraries. If you really need to classify instance variables, one common way is to suffix them with an underscore.

                  这篇关于Cocos2d 游戏中的碰撞检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何检测音效何时播放完毕? 下一篇:Cocos2D求助:如何连续旋转一个精灵并在旋转的精灵中生成子精灵

                  相关文章

                    • <bdo id='mSxPs'></bdo><ul id='mSxPs'></ul>
                    <i id='mSxPs'><tr id='mSxPs'><dt id='mSxPs'><q id='mSxPs'><span id='mSxPs'><b id='mSxPs'><form id='mSxPs'><ins id='mSxPs'></ins><ul id='mSxPs'></ul><sub id='mSxPs'></sub></form><legend id='mSxPs'></legend><bdo id='mSxPs'><pre id='mSxPs'><center id='mSxPs'></center></pre></bdo></b><th id='mSxPs'></th></span></q></dt></tr></i><div id='mSxPs'><tfoot id='mSxPs'></tfoot><dl id='mSxPs'><fieldset id='mSxPs'></fieldset></dl></div>
                  1. <tfoot id='mSxPs'></tfoot>

                      <small id='mSxPs'></small><noframes id='mSxPs'>

                      <legend id='mSxPs'><style id='mSxPs'><dir id='mSxPs'><q id='mSxPs'></q></dir></style></legend>