2015-02-11 121 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 /點更有效,海事組織,並給了我沒有問題。