I am making a game in flash CS3 with AS2. I have a player that moves around when the arrow keys are pressed. I want to know what i need to type to make the character stop when he hits a wall. thanks
I was actually working on that on my game and finished it pretty well. You pretty much just need to constantly check for collisions and even there is a collision then do something.
There are a number of that you could accomplish. I'll give you a few ways to tackle this and you can choose one that works best for what you're trying to make.
If you're game has only two walls on each end of the stage in order to block the player from moving out of the visible part of the stage, you don't even need to look for a collision. Simply use an if statement that checks if your player is past a specified point (where the player is touching the wall) and change his position to keep him from moving further. Ex: Lets say I have two walls, one at x=0 and one at x=500
if (player.x > 500) { player.x = 500 } else if (player.x < 0) { player.x = 0 }
That's the simplest way to do it. Now, basically every other way requires two for loops checking for x and y collisions. I commonly use this to make things like bird-view walking and platformers. My apologies if I get any syntax wrong, as I normally code in AS3, but it's still the same concept.
xVel - how fast the player moves xSign - determines if the player is moving in a positive or negative direction
var xSign:Number = xVel/Math.abs(xVel) for (var i:Number = 0; i < Math.abs(xVel); i++) { player.x += xSign if (player.hitTest(wall)) { player.x -= xSign } } Then repeat that for the y axis if needed. Basically what it's doing is it moves it one pixel, checks if it's hitting something and if it is, moves it one back. It does this for however fast your player is moving, so he's not limited to one pixel per frame. :P
Now, there's also the possibility that you have multiple walls. If that's the case, you're still using the second example code that I gave you, you're just adding in another for loop to check every wall. If you need help with multiple wall collision, I might be able to help, but again, I code in AS3, so I might not be the best source.