Link to home
Start Free TrialLog in
Avatar of mms_master
mms_masterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Actionscript Collision Detection

Hi,

I am creating my own version of Pacman as practice for developing flash games. (It is the first one I've ever tried to make).

I have managed to detect a collision with walls etc using mc1.hitTest(mc2)
If the pacman collides with a wall it stops...
The problem I have is if the pacman is between 2 walls (above and below it or to the left and right of it) I want it to only react to the arrow keys in whic directions it can travel.

I.E. if there is a wall above and below it, I want it to only react to the left and right arrow keys...

Using mc1.hitTest(mc2) the pacman reacts to the keypress and just stops where it is, facing the wall either above or below it...

Is there a way to detect if the pacman will collide with a wall before it reacts to the keypress? (hence disabling the keys which point in a direction which the pacman cannot go)

I tried to explain this as best I can. If you need any more information please ask.

Thanks in advance
mms_master
ASKER CERTIFIED SOLUTION
Avatar of blue-genie
blue-genie
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mms_master

ASKER

Hi,

I actualy found a way to work around it before reading your post, but thanks for your reply.
I hittest the walls to see if any of them hit the point just above/below etc the pacman.
Here is my code so you can understand me better:

if (Key.isDown(Key.RIGHT)) {
                     PathClear = "YES";
      for (i=1; i<=_root.WallCount; i++) {
            if (_root["Wall"+i].hitTest((_root["PacMan"]._x+(_root["PacMan"]._width/2)+2), _root["PacMan"]._y-(_root["PacMan"]._height/2), false)) {
                  PathClear = "NO";
                  break;
            }
            if (_root["Wall"+i].hitTest((_root["PacMan"]._x+(_root["PacMan"]._width/2)+2), _root["PacMan"]._y, false)) {
                  PathClear = "NO";
                  break;
            }
            if (_root["Wall"+i].hitTest((_root["PacMan"]._x+(_root["PacMan"]._width/2)+2), _root["PacMan"]._y+(_root["PacMan"]._height/2), false)) {
                  PathClear = "NO";
                  break;
            }
      }
      if (PathClear == "YES") {
            PlayerDirection = "RIGHT";
      }
}

I will still accept your answer as your solution would have also worked.

Thanks for the help.
mms_master