2015-02-11 123 views
0

嗨即时尝试做一个基本的游戏在Adobe Flash的AS3帮助学习碰撞检测,其目的是让你的方式通过交通。玩家(box_MC)必须将其移到另一侧,而其他物体正在进行碰撞检测(循环)。我通过进入循环动画片段并制作其他更小的循环来进行碰撞检测,如果您碰撞创建碰撞。碰撞as3 dectect

冲突错误在于,如果玩家向下移动到循环它不运行碰撞

P.S如果做碰撞的更好的办法它怎么办?

回答

0

hitTestObject()hitTestPoint()在碰撞检测方面并不是很好,这有点讽刺意味,因为当然,这些是大多数人在尝试实现碰撞时首先要看的东西。然而,我发现简单的数学(如真的很简单)与一个简单的while()循环相结合是最好的方法。

我要做的就是:

// the stage collision box 
var mStageRect:Rectangle = new Rectangle(/*stage collision box properties here*/); 

// create a Point object that holds the location of the bottom center of the player 
var mPlayerBase:Point = new Point(player.x + (player.width/2), player.y + player.height); 

// call this function every frame through your game loop (onEnterFrame) 
private function checkCollision(e:Event):void 
{ 
    // while the player's bottom center point is inside of the stage... 
    while (rectContainsPoint(mStageRect, mPlayerBase)) 
    { 
     // decrement the player's y 
     player.y--; 

     // set gravity to 0 
     player.gravity = 0; 

     // set isOnGround to true 
     player.isOnGround = true; 
    } 
} 

// checks if a point is currently positioned within the bounds of a rectangle object using ultra simple math 
private function rectContainsPoint(rect:Rectangle, point:Point):Boolean 
{ 
    return point.x > rect.x && point.x < rect.x + rect.width && point.y > rect.y && point.y < rect.y + rect.height; 
} 

这是waaaaaaay比hitTestObject /点更有效,海事组织,并给了我没有问题。