0
\$\begingroup\$

I'm making a simple game. The character is movable with the arrow keys. The enemies are moving around the screen with a (classic) tween.

I want to detect collision between the character and the enemies. At first I thought it was working properly, but it also seems to detect a collision on the starting position from the enemies. So even if the enemies are already moving/tweening away from their start position, there's a collision detected between the character and these starting positions. (please ask questions if you don't quite get what I mean)

First method:

addEventListener(Event.ENTER_FRAME, checkIfHitTest);
function checkIfHitTest(Event)
{
      if(player.hitTestObject(enemies))
      {
        trace("yes");

      }
      else
      {
           trace("no");

      }
}

This didn't work properly because it seems to detect the collition even if the enemy moved away from the starting point. (Probably because the ENTER_FRAME but correct me if i'm wrong)

My question to you: What's causing the detection and what is the best way to detect collision between a tweening movieclip and a movieclip wich is movable with the arrowkeys?

\$\endgroup\$
3
  • \$\begingroup\$ The tweens are made on stage so not dynamicly in code and I'm programming on the timeline (actions) Thanks in advance! \$\endgroup\$
    – Opoe
    Apr 25, 2012 at 14:37
  • \$\begingroup\$ Hum I guess your tweens are inside one "enemy" movie-clip and this movie-clip is used to represent all enemies on your stage at different places, you do not tweens each enemy one by one, right ? \$\endgroup\$
    – Valkea
    Apr 25, 2012 at 18:03
  • \$\begingroup\$ @Valkea Yes you're right! I called it enemies, but it's just one movieclip. Sorry for the confusion :) \$\endgroup\$
    – Opoe
    Apr 25, 2012 at 18:12

1 Answer 1

2
\$\begingroup\$

From what you describe, your problem is probably relative to the movie-clip you are targeting to test collisions with.

To be more precise, I think you are testing your collisions against the movie-clip containing the enemy animation and the enemy graphics. But indeed this movie-clip doesn't move at all... it's fixed and only its content is moving (well at least it seems to be fixed from what I can read).

To solve this problem, you need to test against the movie-clip you animate inside the movie-clip containing the animation. So give a name to this movie-clip (e.g. enemyShape) inside the animation move-clip and try to test collision using this one (take care to give the name on each key-frame of the tween animation).

Basically you will have to test against "enemyClipXXX.enemyShape" with XXX being any method you are using to name your enemies movie-clips.

\$\endgroup\$
2
  • \$\begingroup\$ thanks for your excellent answer Valkea, if you know this I will be amazed, I have been stuck for a while now... Same situation but applying the hitTest to a MovieClip inside a movieclip that is being dynamically added to stage through as3. I have tried your way and it works perfectly if I place the MC on the stage manually, but through AS3 it says enemyClipXXX.enemyShape undefined property "enemyShape" Help if you can, thanks very much \$\endgroup\$
    – user16237
    May 14, 2012 at 11:21
  • \$\begingroup\$ @user16237 if you are sure you loaded through AS3 the same MC (with an "enemyShape" MC defined into it) you placed manually, then the only issue I can see with those informations is probably an incorrect path to find the "enemyShape" sub-MC. To test this, define a temp variable on each levels involved. Say var temp:int = 0; on first frame of the MC you try to load you enemy clips, 1 on the first frame of your "enemyClipXXX" and 2 on the first frame of the "enemyShape". Then before your hitTest trace the temp variable of the involved MCs... this way you could spot the problem and fix it. \$\endgroup\$
    – Valkea
    May 14, 2012 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .