Collision detection in Actionscript 3 - HELP!

Soldato
Joined
8 Oct 2007
Posts
2,844
Hi,

I'm making a simple game in Flash using AS3. So far I've created a ball character which the player controls with the arrow keys, it has acceleration, friction and mavity.

What I want to do next is create a maze like level, so I'm trying to make it so when the player touches the wall / level it doesn't go through it and bounces off in the opposite direction.

I've managed to get a function working that detects the collisions:

Code:
                private function wall_block(e:Event) : void
		{
			if (Player.hitTestObject(level1_wall))
			{
				trace("Collision");
			}
                } //end wall_block()

This function runs on every frame using:

Code:
addEventListener(Event.ENTER_FRAME, wall_block);

Player is the ball and level1_wall is the wall. It outputs "Collision" when the ball touches the wall so I know it's working.

Next I need a way to stop the ball going through the wall, and also a way to make it bounce off in the opposite direction that it hit.

So I'm stuck with that, can anyone offer any assistance? :p P.S. I suck at programming in general.
 
Thanks for the replies, yeah I think what I need is a way to reverse the direction and velocity when it hits the wall. What I tried was adding this to the collision detection function: yspeed -= power;

Which is the same thing that happens when the Up key is pressed. Since the wall is only a floor on the ground right now this does actually make it bounce up when it touches the wall. But I still need a way for it to detect which direction it is hitting the wall from, because right now even if it hit a wall above it will still bounce up instead of down.

analog, to make the ball move with the arrow keys I am basically doing this: declaring variables for the power, friction, mavity, xspeed and yspeed. Using a function to track key presses and releases, like so for the Up key:

Code:
                       case Keyboard.UP:
                       trace("Up key pressed");
                       up = true;
                       break;

Then adding or subtracting to the x and y axis of the Player object to make it move with another formula, when the keys are pressed, for the Up key:

Code:
                        if (up) // If up key is pressed
			{
				yspeed -= power; // yspeed - power = n
			} // end if

                        xspeed *= friction; // xspeed * friction = n
			yspeed *= friction; // uspeed * friction = n
			yspeed += mavity; // yspeed + mavity = n
			Player.x += xspeed; // Player.x + xspeed = n
			Player.y += yspeed; // Player.y + xspeed = n

Edit: Oh and the object it's hitting is simply a box object that is drawn on the scene, an instance of the Wall movieclip in the library labelled "level1_wall"
 
Last edited:
Anyone got any ideas? I'm still stuck with this :/

To simplify the problem: I have a way to stop the ball going through the wall ONLY from one direction at a time, and I have a way to make the ball bounce off from one direction at a time.

If I only had walls on either side of the screen (top, bottom, left, right) this would work perfectly I'd just have 4 different If statements for each one.

BUT since the walls are going to be in the middle of the screen I need a way for the ball to be blocked and bounce off of each side because the ball can hit the wall from any side.

So ultimately I think what I need is either a way of detecting which side of the wall the ball collides with the wall or a way of reversing the direction when it collides with the wall.

I feel so close but I just can't work it out it's so frustrating.
 
Back
Top Bottom