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.
 
Never done any actionscript but when a collision is detected cant you just reverse/modify the velocity of the ball appropriate to how you need it to behave?
 
I've never used collision detection in Flash so these will probably be more suggestions.

To make it stop you could have the if statement you could simply reverse what ever is making it move as RobHu said.

So
Code:
var xpos:Number = 5;

if (evt.keyCode == Keyboard.RIGHT && ghost1.x<500) { 
		ghost1.x +=xpos; 
	} 
if (ghost1.x > 400) {
			ghost1.x -=xpos;
		}

That could probably be refined greatly but the idea is there.

If I wanted to animate a bounce I'd probably use the third party tweener class Caurina and simply on it hitting the side do something like.

Code:
Tweener.addTween(Ghost, {x:150, time:2, transition:"easeOutBounce"});

So on it hitting the object it could run a function called bounce that contains the above.

It will take its position at the time as the starting point of the tween but you need to set the ending, I'd bet you can set it to something like x:ghost.x +=5, so it would tween 5 pixels away from its current position.

But depending on what side you hit the object from I'm sure you'd want that to change and I can't really help you with that off the top of my head.

Can you give us some more information on how you're moving the object and what its going to be hitting?

I'm just learning AS myself and this has me interested.
 
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