0
\$\begingroup\$

(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();
  layer2.addChild(newEnemy);
  newEnemy.x = Math.random() * 700;
  newEnemy.y = 10;
  if (enemiesThere == 0)
  {
      enemiesThere = true;
      player.addEventListener(Event.ENTER_FRAME, collisionDetection)
  }



  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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Draco18s no longer trusts SE Dec 30 '15 at 21:29
  • \$\begingroup\$ Okay thank you I will try out Tweener and see if that works out :) \$\endgroup\$ – werer cool Dec 30 '15 at 22:09
  • \$\begingroup\$ Okay so I tried out Tweener, it worked but there is the exact same problem, exactly the same. \$\endgroup\$ – werer cool Dec 30 '15 at 22:50
  • \$\begingroup\$ I'm not sure then. I've never tried to use tweener to handle movement in anything that wasn't pure animation. \$\endgroup\$ – Draco18s no longer trusts SE Dec 31 '15 at 3:06
  • \$\begingroup\$ 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^^ \$\endgroup\$ – werer cool Dec 31 '15 at 3:11
2
\$\begingroup\$

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();
layer2.addChild(newEnemy);
newEnemy.x = Math.random() * 700;
newEnemy.y = 10;
newEnemy.addEventListener(Event.ENTER_FRAME, collisionDetection);
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");
    }
}
\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – werer cool Dec 31 '15 at 17:39
  • \$\begingroup\$ Oh it worked, but only on the very first cube that falls down. The others don't detect collision, only the very first cube. \$\endgroup\$ – werer cool Dec 31 '15 at 17:55
  • \$\begingroup\$ I think your newEnemy should be a local variable. I've updated my code \$\endgroup\$ – bummzack Dec 31 '15 at 18:03
  • \$\begingroup\$ Tried it out, still only the first cube. \$\endgroup\$ – werer cool Dec 31 '15 at 18:25
  • \$\begingroup\$ 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. \$\endgroup\$ – bummzack Dec 31 '15 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.