Enter Frame 在补间时无法精确检测 hitTestObject

发布于 2024-09-15 01:36:30 字数 958 浏览 4 评论 0原文

我有一个运行 Enter_Frame 的函数,并在该函数期间对其进行补间。我知道你可以使用 object.y=object.y + 1 而不是我现在使用的 TweenLite。但 TweenLite 给出了我想要的最佳效果。我现在的问题是,我想在相互不碰撞时删除 Enter_frame 函数。但是当我跟踪 hitTestObject 时,hitTestObject 结果立即返回“false”。因此,补间无法真正完成第一,并在对象仍在碰撞时提前删除 Enter_frame 运行。

private function checkCollision (e:Event):void
        {
            //detect collision in array
            for (var j:uint = 0; j < collisionArray.length - 1; j++)
            {
                for (var k:uint = j + 1; k < collisionArray.length; k++)
                {
                    if (collisionArray[j].hitTestObject(collisionArray[k]))
                    {
                        //do something
                        TweenLite.to (objectA,0.2,{y:move2Y});
                        TweenLite.to (objectB,0.2,{y:move3Y});
                    }
                    trace (collisionArray[j].hitTestObject(collisionArray[k]));
                }
            }
        }

I have a function with Enter_Frame running and have it tween during the function. I know you could use object.y=object.y + 1 instead of the TweenLite i'm using now. But TweenLite gives the best effect I wanted. My problem now is, I wanted to remove the enter_frame function when its not collision each other. But when I traced hitTestObject, it have like a split second where the hitTestObject result return 'false'. So the tweening can't really finish 1st, and remove enter_frame run early while the object is still collisioning.

private function checkCollision (e:Event):void
        {
            //detect collision in array
            for (var j:uint = 0; j < collisionArray.length - 1; j++)
            {
                for (var k:uint = j + 1; k < collisionArray.length; k++)
                {
                    if (collisionArray[j].hitTestObject(collisionArray[k]))
                    {
                        //do something
                        TweenLite.to (objectA,0.2,{y:move2Y});
                        TweenLite.to (objectB,0.2,{y:move3Y});
                    }
                    trace (collisionArray[j].hitTestObject(collisionArray[k]));
                }
            }
        }

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

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

发布评论

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

评论(1

如梦初醒的夏天 2024-09-22 01:36:30

您的问题是,补间直到调用 checkCollision 方法后 0.2 秒才完成。

如果您在 ENTER_FRAME 中调用此方法,您将不断覆盖现有的补间。想想看 - ENTER_FRAME 调用 checkCollisions,checkCollisions 将启动一些补间,在补间有时间完成之前,下一个 ENTER_FRAME 触发,再次调用 checkCollisions 并在同一对象上启动补间,因为补间尚未完成重新定位对象尚未。

您最好的选择可能是根本不使用 ENTER_FRAME - 运行 checkCollisions,为相交对象启动补间,如果存在相交对象,则 0.2 秒后再次调用 chechCollisions(补间完成后)

private function checkCollision (e:Event):void
        {
            var collisionsDetected:Boolean = false;
            //detect collision in array
            for (var j:uint = 0; j < collisionArray.length - 1; j++)
            {
                for (var k:uint = j + 1; k < collisionArray.length; k++)
                {
                    if (collisionArray[j].hitTestObject(collisionArray[k]))
                    {
                        //do something
                        TweenLite.to (objectA,0.2,{y:move2Y});
                        TweenLite.to (objectB,0.2,{y:move3Y});
                        collisionsDetected = true;
                    }
                    trace (collisionArray[j].hitTestObject(collisionArray[k]));
                }
            }

            //you might want to make the timeout a little longer than 200ms,
            //to make sure that the tweens have completed when it fires.
            //you can figure out the best delay based on your framerate
            if(collisionsDetected) setTimeout(checkCollision, 250);
        }

Your problem is that the tweens don't finish until 0.2 secs after the checkCollision method is called.

If you are calling this method in ENTER_FRAME, you will constantly be overwriting existing tweens. Just think about it - ENTER_FRAME calls checkCollisions, checkCollisions will start some tweens, before the tweens have time to complete, the next ENTER_FRAME fires, calls checkCollisions again and starts tweens on the same objects, since the tweens haven't finished re-positioning the objects yet.

Your best bet might be to not use ENTER_FRAME at all - run checkCollisions, start tweens for intersecting objects, if there are intersecting objects then call chechCollisions again 0.2 seconds later (once the tweens finished)

private function checkCollision (e:Event):void
        {
            var collisionsDetected:Boolean = false;
            //detect collision in array
            for (var j:uint = 0; j < collisionArray.length - 1; j++)
            {
                for (var k:uint = j + 1; k < collisionArray.length; k++)
                {
                    if (collisionArray[j].hitTestObject(collisionArray[k]))
                    {
                        //do something
                        TweenLite.to (objectA,0.2,{y:move2Y});
                        TweenLite.to (objectB,0.2,{y:move3Y});
                        collisionsDetected = true;
                    }
                    trace (collisionArray[j].hitTestObject(collisionArray[k]));
                }
            }

            //you might want to make the timeout a little longer than 200ms,
            //to make sure that the tweens have completed when it fires.
            //you can figure out the best delay based on your framerate
            if(collisionsDetected) setTimeout(checkCollision, 250);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文