Enter Frame does not detect hitTestObject precisely when tweenling
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].hit开发者_运维技巧TestObject(collisionArray[k]));
}
}
}
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);
}
精彩评论