Forums

ForumsProgramming Forum

Wall Collisions

Posted Feb 2, '12 at 10:41pm

Sisenta

Sisenta

1 post

I'm currently working on creating a Flash game, although I'm new to the program and need some assistance with ActionScript 3.0 programming. How do you go about programming a player stopping its movement when it touches a block? And I want to emphasis blocks plural, because I tried a basic hitTestObject function and it only worked for one instance of the block, and not any more than that. Help is greatly appreciated! Thank you!

 

Posted Feb 3, '12 at 1:50pm

Freckls627

Freckls627

33 posts

A great method to look into is the getRect() function. You pass an object to it, in this case the player, and you get a rectangle object which you can refer to easily such as, playerRectangle.top, playerRectangle.bottom, playerRectangle.left, etc. This makes it easy to check for collisions among multiple blocks. You can also use this function on the block you're colliding with as well. I hope this helps a little bit.

 

Posted Feb 3, '12 at 5:34pm

BlueJayy

BlueJayy

27 posts

You're going to need a grouping to system to be able to cycle through all of the blocks that you want to check collisions with. You can think of the stage as one large MovieClip that houses all of the objects on the screen. You can also add objects to other MovieClips contained within the stage to create an organization system.

First, declare your new MovieClip:

var Blocks:MovieClip = new MovieClip()

This MovieClip will contain all of the blocks that will be added to the stage. Don't forget to addChild(Blocks) just like you would for any other MovieClip, or else they won't appear on the stage.

Whenever you want to add a new block, you'll want to add it to the Blocks MovieClip so that you can reference it later:

Blocks.addChild(new Block(<paramters&gt;))

Now that you can reference the blocks that you add, you can do the collision system. You'll want to cycle through the Blocks MovieClip and check each block to see if it is colliding with whatever you want.

For the example below, I'll be checking for a collision with the "player" object.

public function collisionCheck() {

for (var u:Number = 0; u < Blocks.numChildren; u++) {

var block:MovieClip = MovieClip(Blocks.getChildAt(u))

if (player.x-player.width/2 < block.x+block.width/2 && player.x+player.width/2 > block.x-block.width/2) {
if (player.y-player.height/2<block.y+block.height/2 && player.y+player.height/2>block.y-block.height/2) {
return true
}
}
}
return false
}

So now you know when a something is colliding with one of the blocks, but you still have to incorporate that knowledge with movement. To do this, you can move the block, check if there's a collision, and if there is, move it back. I personally don't like moving it directly from one point to the end point though, because a lot of the time, you'll actually collide with the blocks and move back, but there will be a gap in between the object and the wall since the block is moving too fast. (I didn't really know how to explain that, so sorry if it doesn't make sense.) To combat that problem (that I didn't explain very well) you can slowly move the object one point at a time, checking each time to see if you are colliding with the blocks.

Utilizing the collision code from above and denoting the player's speeds as xVel and yVel (x velocity & y y velocity):

var xDir:Number = 0
if (xVel != 0) xDir = xVel / Math.abs(xVel)
for (var X:Number = 0; X < Math.abs(xVel); X++) {

player.x += xDir
if (collisionCheck) player.x -= xDir

}

var yDir:Number = 0
if (yVel != 0) yDir = yVel / Math.abs(yVel)
for (var Y:Number = 0; Y < Math.abs(yVel); Y++) {

player.y += yDir
if (collisionCheck) player.y -= yDir

}

So whenever the player collides with a block, the player will immediately be sent back 1. This way, the player will be able to be right against the blocks that it is colliding with, and makes it much more realistic.

If you have any questions with any of the code or concepts, feel free to post back. If anyone finds any errors with my code (Knowing me, I probably messed up somewhere unknowingly) please reply so that I can fix it.

Hope I was able to help. :D

 
Reply to Wall Collisions

You must be logged in to post a reply!