2014-03-13 264 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; 
}