2014-03-13 174 views
0

我正在使用actionscript 3開發Flash遊戲。它就像迷宮遊戲一樣。我們有一條船和繩索創建路徑。我遇到了碰撞問題。在通過該路徑時,它正確地碰撞到X軸& Y軸上,並且其工作正常,但是當它在任何角落(其中X軸與Y軸相遇)碰撞時,它只是穿過繩索。 這是我的碰撞腳本。ActionScript 3碰撞

if (leftArrow) 
{ 
    boat.x -= speed; 
    if(rope.hitTestPoint(boat.x,boat.y,true)){ 
       boat.x += 5; 
      } 
      if(rope.hitTestPoint(boat.x,boat.y+height,true)){ 
       boat.x += 5; 
      } 

} 
else if (rightArrow) 
{ 
    boat.x += speed; 
    if(rope.hitTestPoint(boat.x+boat.width,boat.y,true)){ 
       boat.x -= 5; 
      } 
      if(rope.hitTestPoint(boat.x+boat.width,boat.y+height,true)){ 
       boat.x -= 5; 
      } 

} 
else if (upArrow) 
{ 
    boat.y -= speed; 
    if(rope.hitTestPoint(boat.x,boat.y,true)){ 
       boat.y += 5; 
      } 
      if(rope.hitTestPoint(boat.x+boat.width,boat.y,true)){ 
       boat.y += 5; 
      } 

} 
else if (downArrow) 
{ 
    boat.y += speed; 
    if(rope.hitTestPoint(boat.x,boat.y+boat.height,true)){ 
       boat.y -= 5; 
      } 
      if(rope.hitTestPoint(boat.x+boat.width,boat.y+boat.height,true)){ 
       boat.y -= 5; 
      } 
} 

回答

0

hitTestPoint好尋找一個點是否在一個對象,但使用它的遊戲般情況下,當因爲你有很多點的檢查,如果你正在處理不同的形狀和程度卻不盡如人意運動。解決方案在hitTestObject中。它與hitTestPoint類似,除了比較顯示對象而不是單個點。但有一個問題:你正在比較邊界框。訣竅是在物體的圖形上創建一個包含alpha 0的盒子(在你的情況下是船),它儘可能地覆蓋並且不會造成對玩家沒有意義的碰撞,你稱它爲'hit ',然後運行碰撞檢測。這裏有一個很好的解釋:http://bit.ly/Oj2ZCe

一個註釋:所以在他們的例子中,該船的這一點距離不可見的'hit'框很遠。你可以做些什麼來進一步改進它,將它與hitTestPoint函數結合起來,並在任何奇怪的尖端部件上運行額外的碰撞檢測。

這就是它的要義。這會使你的代碼降低到這樣的程度:

var rememberX:Number = boat.x; 
var rememberY:Number = boat.y; 

if (leftArrow) 
{ 
    boat.x -= speed; 
} 
else if (rightArrow) 
{ 
    boat.x += speed; 
} 
else if (upArrow) 
{ 
    boat.y -= speed; 
} 
else if (downArrow) 
{ 
    boat.y += speed; 
} 

if(rope.hitTestObject(boat.hit)) 
{ 
    boat.x = rememberX; 
    boat.y = rememberY; 
}