减少 hittestobject 的面积 - flash cs5
我正在as3 中制作一个简单的贪吃蛇游戏。我遇到的问题是元素上的笔划稍微重叠,因此当蛇经过相邻方块中的苹果时,它会触发击中测试对象。如果我减小蛇或苹果的大小或行程,那么它们就无法正确排列到网格上。有没有一种方法可以使用 hittestobject 但减少触发它的对象的面积。即只有当它击中物体的中心时才触发?
//this will hit when snake is adjacent due to overlapping strokes
if(snake.hitTestPoint(apple.x,apple.y,true))
{
trace('hit');
}
//this doesnt work for some reason
if(snake.hitTestPoint(apple.x+2,apple.y+2,true))
{
trace('hit');
}
当我跟踪底部代码的值时,这些值是预期的,但由于某些原因 hitTestPoint 不会触发。奇怪的是,如果我做+1,它仍然会工作,但因为行程是2,我至少需要+3。如果我将 apple.x+n 设置为变量并在 hitTestPoint 中使用该变量,它将不会触发。此外,在它不触发的所有情况下,我都没有收到错误,并且所有跟踪值都是您所怀疑的......
I'm making a simple snake game in as3. the problem I'm having is that the stroke on elements slightly overlap thus when the snake passes by the apple in an adjacent square it triggers hit test object. If i reduce the size or stroke of either the snake or the apple then they dont properly line up to the grid. is there a way i can use hittestobject but reduce the area of the object that will trigger it. ie only trigger if it hits the center of the object?
//this will hit when snake is adjacent due to overlapping strokes
if(snake.hitTestPoint(apple.x,apple.y,true))
{
trace('hit');
}
//this doesnt work for some reason
if(snake.hitTestPoint(apple.x+2,apple.y+2,true))
{
trace('hit');
}
when I traced the values for the bottom code the values are what would be expected but some reason hitTestPoint wont trigger. strangely if I do + 1 it will still work but because the stroke is 2 i need at least +3. If i set apple.x+n as a variable and use the variable in hitTestPoint it wont trigger. also on all cases where it does not trigger i get no errors and all trace values are what you would suspect...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在蛇电影剪辑中,添加另一个作为其中心的剪辑,然后测试中心剪辑而不是整个蛇剪辑。
if(snake.center.hitTestPoint(apple.x,apple.y,true))
{
追踪('命中');
我猜这
是解决你的问题的最简单的方法。
In the snake-movieclip, add another clip that functions as it's center, then test for the center-clip instead of the whole snake-clip.
if(snake.center.hitTestPoint(apple.x,apple.y,true))
{
trace('hit');
}
I'm guessing this is the easiest way to solve your problem.