减少 hittestobject 的面积 - flash cs5

发布于 2024-12-01 10:40:48 字数 611 浏览 1 评论 0原文

我正在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2024-12-08 10:40:48

在蛇电影剪辑中,添加另一个作为其中心的剪辑,然后测试中心剪辑而不是整个蛇剪辑。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文