当我在做我目前的项目时,我发现当我移动对象时,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 也适用于下一帧。
到目前为止,有人遇到过同样的事情吗?