Solved

Actionscript Collision Detection

Posted on 2007-03-30
2
1,569 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
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
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 what frame rate is, how to control it and what effect it has on the video.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now