检测 Flash 中的碰撞...或者更好的方法

发布于 2024-10-10 10:31:49 字数 210 浏览 1 评论 0原文

我正在尝试使用 AS3 在 Flash 中创建一个空气曲棍球游戏。

目前,我正在使用输入框架功能来检查 3 个物体(2 个球拍和一个球)的位置,然后检查它们是否接触,如果接触,则启动检测碰撞功能。

然而,它会在每次加载帧时进行检查,并且在 25 fps 下,这是相当多的,并且应用程序滞后。

有什么想法或更好的方法来做到这一点吗?

提前致谢。

I'm trying to create a air-hockey game, in flash using AS3.

At the moment, I am using an enter frame function to check the positioning of the 3 objects, 2 paddles and a ball, then it checks to see if they are in contact, if they are it then initiates a detect collision function.

However it's checking every time the frame is loaded, and at 25 fps, this is quite a lot and the app lags.

Any ideas, or better ways to do this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

成熟的代价 2024-10-17 10:31:49

两个毕达哥拉斯陈述正在减慢你的游戏速度? 25 帧/秒?出了点问题——不应该是这样。

完全删除碰撞检测并检查是否恢复了 25fps,然后一次一行添加语句,直到速度再次下降。

检查每帧调用碰撞代码的次数是否超过一次(好吧,两次)。

请记住,您可以在不使用 Math.sqrt 的情况下测试碰撞:(

function circlesTouching(circle1:Point, circle1Radius:Number, circle2:Point, circle2Radius:Number):Boolean {
    var dx:Number = circle1.x - circle2.x;
    var dy:Number = circle1.y - circle2.y;
    var minDist:Number = circle1Radius + circle2Radius;
    return (dx*dx) + (dy*dy) < (minDist * minDist);
}

您仍然需要 sqrt 来解决碰撞,但这应该很少见.)

然而,根据我的经验,尽管 Math.sqrt 是 Pythagoras 最慢的部分,但它仍然足够快以 25fps 管理每帧两次调用。听起来好像还有什么地方不对劲。

Two pythagoras statements are slowing your game down? At 25fps? Something is wrong -- that should not be the case.

Remove the collision detection completely and check that you get your 25fps back, then add statements a line at a time until the slowdown reappears.

Check you are not calling your collision code more than once (well, twice) per frame.

Remember that you can test for collision without using Math.sqrt:

function circlesTouching(circle1:Point, circle1Radius:Number, circle2:Point, circle2Radius:Number):Boolean {
    var dx:Number = circle1.x - circle2.x;
    var dy:Number = circle1.y - circle2.y;
    var minDist:Number = circle1Radius + circle2Radius;
    return (dx*dx) + (dy*dy) < (minDist * minDist);
}

(You will still need sqrt to resolve the collision but this should be pretty rare.)

However in my experience, even though Math.sqrt is the slowest part of Pythagoras, it's still easily fast enough to manage two calls per frame at 25fps. It sounds like something else is wrong.

所谓喜欢 2024-10-17 10:31:49

如果您需要定期检查某些内容,我想 EnterFrame 事件是一个合适的机制。

您没有提到是否使用内置的命中测试功能,所以我想我会提到它们: hitTestObjecthitTestPoint

If you need to check something periodically, I guess the enterFrame event is a suitable mechanism to do it.

You don't mention if you use the built-in hit test functions or not, so I thought I'd mention them: hitTestObject and hitTestPoint.

ま昔日黯然 2024-10-17 10:31:49

你尝试过定时器吗?

var timer:Timer = new Timer(250); // 4 times a second

timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();


private function onTimer(ev:TimerEvent):void
{
  checkCollision();
}

Have you tried a timer?

var timer:Timer = new Timer(250); // 4 times a second

timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();


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