为什么hitTestObject检测到冲突晚了? [英] Why does hitTestObject detect the collision late?

查看:145
本文介绍了为什么hitTestObject检测到冲突晚了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做这个快速的乒乓球比赛,并使用hitTestObject检查碰撞。不应该是一个问题,因为一切都是矩形反正。向量中的每个球被检查以看它是否与桨每帧发生碰撞。为什么每个球不会触发碰撞,直到它通过它得到一半的方式?我不能调用函数的频率比ENTER_FRAME做,或者可以吗?我似乎只是它没有更新速度不够快,或者什么的。

I'm making this quick pong game and using hitTestObject to check for the collision. Shouldn't be a problem, since everything is rectangle anyway. Each ball in the vector is checking to see if it's colliding with the paddle every frame. Why does each ball not trigger the collision until it gets about half way through it? I can't call a function more frequently than enter_frame does, or can I? I just seems like it's not updating fast enough or something.

if (ball[i].hitTestObject(paddle)){

     // Flip
     ball[i] *= -1;
}

它的快速PIC:

Quick pic of it:

推荐答案

我会假设这是因为这是在运行命中测试,因为它只是检查瞬时位置,而不是运动的流畅的线条球。换句话说,如果在一帧上球的边缘5PX远离桨,命中测试将是假的。如果球然后移动8像素的下一帧,点击测试将是真实的,但球优势将挡板里面的3px。

I would assume it's because that's where the ball is when you run the hit test, as it's only checking momentary locations instead of a smooth line of movement. In other words, if on one frame the edge of the ball is 5px away from the paddle, the hit test will be false. If the ball then moves 8px on the next frame, the hit test will be true, but the ball edge will be 3px inside the paddle.

当我有一个简单的平边这样的测试,我倾向于做沿着这些路线的东西(给定一个球快捷半径变量和 paddle.y ==桨叶前缘):

When I have a simple flat edge like this to test, I tend to do something along these lines (given a ball with speedY and radius variables, and paddle.y == the leading edge of the paddle):

if (ball.y + ball.radius > paddle.y) {
    // Mirror the ball's speed
    ball.speedY *= -1;

    // Mirror the ball's location as if it had bounced on the edge
    // by moving it back by the same distance it went past.
    ball.y -= (ball.y + ball.radius - paddle.y) * 2;
}

这篇关于为什么hitTestObject检测到冲突晚了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆