0

我正在 cocos2D 中制作一个球类游戏,您可以在其中向一个方向滑动球,它会一直移动直到撞到墙壁。当我向一个方向滑动球时,我的碰撞检测给了我一个“断言错误”。

我正在使用 tiledmap 来制作关卡。

我编码的方式是找到我的球所在的tilesize,然后在for循环中检查球周围是否有任何墙壁精灵。

有什么想法哪里出错了吗?

-(void)callEveryFrame:(ccTime)dt{

     //Error Here vvv (Assertion Failure in -[CCTMXLayer tileAt:])


     NSLog(@"First Touch x:  /%f  Last Touch x: /%f  First Touch y: /%f   Last Touch y:       /%f", firstTouch.x, lastTouch.x, firstTouch.y, lastTouch.y);

     if(swipeLeft || swipeRight || swipeDown || swipeUp)
     {
         bool collision = false;
         int ballTileX = (player.position.x / level1.tileSize.width);
         int ballTileY = (player.position.y / level1.tileSize.width);
         int collisionAreaX = ballTileX-1;
         int collisionAreaY = ballTileY-1;

         for(int i = 0; i < 9; i++)
         {
             bool tilePresent = [background tileAt:CGPointMake(collisionAreaX, collisionAreaY)] != NULL;

             if(tilePresent && CGRectIntersectsRect(CGRectMake(collisionAreaX*level1.tileSize.width, collisionAreaY*level1.tileSize.height, level1.tileSize.width, level1.tileSize.height), CGRectMake(ballTileX*level1.tileSize.width, ballTileY*level1.tileSize.height, level1.tileSize.width, level1.tileSize.height)))
             {
                 collision = true;
                 break;
             }

             if(i % 3 == 0)
             {
                 collisionAreaX -= 2;
                 collisionAreaY++;
             }
             else
             {
                 collisionAreaX++;
             }
         }

         if(collision) {
             swipeLeft = false;
             swipeRight = false;
             swipeDown = false;
             swipeUp = false;
         }
    }
 }
4

0 回答 0