Actionscript Collision Detection

Posted on 2007-03-30
Last Modified: 2012-05-05

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
Question by:mms_master
LVL 39

Accepted Solution

blue-genie earned 125 total points
ID: 18845494
hi, i remember creating one a couple of years ago (Cant' find the file) - but if you think about it, you can't do a hit test until it hits the thing, so maybe a way to hack it, would be to put a bigger invisible bounding box around your item, that way the hit test will kick in earlier.

my 2c worth,

Author Comment

ID: 18876039

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";
            if (_root["Wall"+i].hitTest((_root["PacMan"]._x+(_root["PacMan"]._width/2)+2), _root["PacMan"]._y, false)) {
                  PathClear = "NO";
            if (_root["Wall"+i].hitTest((_root["PacMan"]._x+(_root["PacMan"]._width/2)+2), _root["PacMan"]._y+(_root["PacMan"]._height/2), false)) {
                  PathClear = "NO";
      if (PathClear == "YES") {
            PlayerDirection = "RIGHT";

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

Thanks for the help.

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question