Solved

Actionscript Collision Detection

Posted on 2007-03-30
2
1,609 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:mms_master
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Accepted Solution

by:
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,
blu.
0
 
LVL 5

Author Comment

by:mms_master
ID: 18876039
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
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…

688 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