【问题标题】:AS3 hitTestPoint accuracy is delayed by one frameAS3 hitTestPoint 精度延迟一帧
【发布时间】:2012-06-28 02:28:59
【问题描述】:

当我在做我当前的项目时,我发现当我移动对象时,hitTestPoint 的准确性会延迟一帧。因此,如果对象 A 被移动到一个全新且遥远的位置,对该对象执行的 hitTestPoint 将返回 false。为了说明这一点,我使用最基本的逻辑做了一个快速实验:

import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.Event;

var me:Sprite = new Sprite();
var g:Graphics = me.graphics;
g.beginFill( 0xFF0000 );
g.drawRect( -10, -10, 20, 20 );
g.endFill();
addChild( me );

trace( "Hit test at (0,0) while object is at (" + me.x + "," + me.y + ") = " + me.hitTestPoint( 0, 0, true ) );

me.x = 300;
me.y = 300;

trace( "\n>>>>>>>>>> WHERE DID THE OBJECT GO? >>>>>>>>>>" );
trace( "Hit test at (0,0) while object is at (" + me.x + "," + me.y + ") = " + me.hitTestPoint( 0, 0, true ) );
trace( "Hit test at (150,150) while object is at (" + me.x + "," + me.y + ") = " + me.hitTestPoint( 150, 150, true ) );
trace( "Hit test at (300,300) while object is at (" + me.x + "," + me.y + ") = " + me.hitTestPoint( 300, 300, true ) );
trace( ">>>>>>>>>> WHERE DID THE OBJECT GO? >>>>>>>>>>\n" );

removeChild( me );
addChild( me );

trace( "After remove/addChild, hit test at (300,300) while object is at (" + me.x + "," + me.y + ") = " + me.hitTestPoint( 300, 300, true ) );

结果如下:

Hit test at (0,0) while object is at (0,0) = true

>>>>>>>>>> WHERE DID THE OBJECT GO? >>>>>>>>>>
Hit test at (0,0) while object is at (300,300) = false
Hit test at (150,150) while object is at (300,300) = false
Hit test at (300,300) while object is at (300,300) = false
>>>>>>>>>> WHERE DID THE OBJECT GO? >>>>>>>>>>

After remove/addChild, hit test at (300,300) while object is at (300,300) = true

我发现在将孩子移除并添加回舞台后,hitTestPoint 再次起作用。但 hitTestPoint 也适用于下一帧。

到目前为止,有人遇到过同样的事情吗?

【问题讨论】:

    标签: actionscript-3 flash hittest


    【解决方案1】:

    我认为这个问题的发生是由于 flash 处理显示对象的方式。 我的猜测是你打电话的那一刻

    addChild(me);
    

    “addChild”强制 flash 绘制“我”。 但是当您使用 x 和 y 参数移动“我”时,flash 会等待帧完成,然后再重新绘制图形。

    我再次猜测如果你想强制 flash 立即重绘

    import flash.display.Sprite;
    import flash.display.Graphics;
    import flash.events.Event;
    
    var me:Sprite = new Sprite();
    var g:Graphics = me.graphics;
    var myIndex:int;
    g.beginFill( 0xFF0000 );
    g.drawRect( -10, -10, 20, 20 );
    g.endFill();
    addChild( me );
    
    me.x = 300;
    me.y = 300;
    
    addChild(me);
    

    除非没有办法,否则不要使用此 hack,因为它会降低您的应用程序的速度。 同样,我无法检查我是否正确,因为我的电脑上没有闪存。

    【讨论】:

      【解决方案2】:

      我可以确认 chandings 所说的关于 flash 更新对象在舞台上的位置的时间,但可以更正和扩展解释。 Flash 确实具有固定的帧速率,并且显示更新遵循此时间线。但是游戏逻辑可以比渲染速度更快或更慢。如果您调用addChild(me),它不会立即更新位置,但也会在下一帧开始时更新。

      因此,您必须选择使用固定时间步长或可变时间步长。与您做出的选择无关,最好使用OnEnterFame 之类的事件来管理时间步,而不是依靠 Flash 播放器来更新您的逻辑。

      我在gamedev.stackexchange.com 上写了an answer 到一个类似的问题,并且该线程也有一些有用的链接:

      这是另一个与语言无关的有用线程:Fixed time step vs Variable time step

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-15
        • 1970-01-01
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 2023-03-20
        相关资源
        最近更新 更多