# AS3 .hitTestObject() not working with Tween function

(This was originally in an other senction but many told me to ask here, hopefully someone can answer this) I am having a major problem in my new browser app.

Okay so I made game where different cubes (squares) spawn at the top of the screen and I use the Tween class to make them go down the screen and then disappear.

However I want to detect a collision when a cube hits the player (that is also a flying cube).

I tried everything, truly everything but it does not seem to work. The problematic thing is that when I remove the "Tween" function it does detect collision with the hitTestObject method but when I add the "Tween" line collision won't be detected anymore.

The code looks like this:

    function enemiesTimer (e:TimerEvent):void {

newEnemy = new Enemy1();
newEnemy.x = Math.random() * 700;
newEnemy.y = 10;
if (enemiesThere == 0)
{
enemiesThere = true;
}

var Tween1:Tween = new Tween(newEnemy, "y", null, newEnemy.y, newEnemy.y+distance, movingTime, true); }


The collision detection part:

private function collisionDetection (e:Event):void
{

if (player.hitTestObject(newEnemy))
{
trace("aaa");
}

}


*Another weird thing is that the collision detection works only in the starting point, where the squares spawn but they aren't really there anymore so I don't quite get it.

I hope someone can answer this and maybe tell me how I can code this. Thanks in advance :)

*I've tried out Tweener & TweenMax as well, the problem is always the same

• I don't know how the built-in tween engine works (your observation suggests that the built-in tweens don't actually move the object, just its visual representation), you may want to get Tweener or other 3rd party Tween library. You could also try hitTestPoint though again, it will depend on what the built-in function is actually doing. – Draco18s no longer trusts SE Dec 30 '15 at 21:29
• Okay thank you I will try out Tweener and see if that works out :) – werer cool Dec 30 '15 at 22:09
• Okay so I tried out Tweener, it worked but there is the exact same problem, exactly the same. – werer cool Dec 30 '15 at 22:50
• I'm not sure then. I've never tried to use tweener to handle movement in anything that wasn't pure animation. – Draco18s no longer trusts SE Dec 31 '15 at 3:06
• Yes ;/ By the way I tried out TweenMax as well but it's always the same problem. It looks like all these tween libs all just move it's visual content instead of it's actual position ;/ Thanks anyways^^ – werer cool Dec 31 '15 at 3:11

I'm sure this has nothing to do with the Tweening library you're using (as indicated in the comments) but rather with how you reference your enemy in the collisionDetection. Depending on how your code is structured, newEnemy might not be set in that scope or be set to something you're not expecting.

I suggest you try this:

var newEnemy:MovieClip = new Enemy1();
newEnemy.x = Math.random() * 700;
newEnemy.y = 10;
var Tween1:Tween = new Tween(newEnemy, "y",
null, newEnemy.y, newEnemy.y+distance, movingTime, true);


And in your collisionDetection method:

private function collisionDetection (e:Event):void
{
// e.currentTarget is the enemey that emits the event
if (e.currentTarget.hitTestObject(player)){
trace("aaa");
}
}

• Hi, I tried this out. (Code was exactly the same), but it didn't work. However it didn't detect any collisions in the starting point now neither – werer cool Dec 31 '15 at 17:39
• Oh it worked, but only on the very first cube that falls down. The others don't detect collision, only the very first cube. – werer cool Dec 31 '15 at 17:55
• I think your newEnemy should be a local variable. I've updated my code – bummzack Dec 31 '15 at 18:03
• Tried it out, still only the first cube. – werer cool Dec 31 '15 at 18:25
• I think it should be currentTarget instead of target in the collisionDetection method. Sorry, my AS is rusty. If that doesn't work, please post your complete class somewhere and I'll have a look. – bummzack Dec 31 '15 at 18:26