CONTINUED FROM LAST POST
So I left off with the walls code that goes in the walls. Now you can copy the walls with the code in them and past it anywhere you want to have walls. Your character will not be able to go past.
Now it is time for the explainations... oh joy.
Body Actions
First was the "playerhitbody"; the rectangle that is inside the player. This is only used for to test bounds when jumping to make sure that the location of the feet is what is to hit the ground. If you have an animation where the character raises its feet, you might run into some problems. The only actions in this make it invisible.
Player Actions
Second was the players actions. I'll go through this in blocks of lines so I don't run out of space again:
Lines 1-8: These were the loading actions. The hitwalll and hitwallr variables refer to "hit wall left" and "hit wall right" respectively. The side you hit a wall on triggers one of the variables and will not allow you to move in that direction. grav is the variable for gravity and is set to 0 to start out. jumping and running are boolean values that pretty much stand for what they sound like.
Lines 9-29: These test to see if the right or left keys are down. If they are and the player is not jumping, the _xscale will change to make the character face the right direction, the character will go to the frame with the running animation, and the _x values will change to make the player move. If no keys are down, player is not running and goes to the frame with the standing animation.
Lines 30-46: When the up arrow is pressed and the player is not already jumping, the player will then be jumping and the gravity will be changed to make the player go up. The character goes to the frame with the jumping animation and if the right or left arrow key is down and the player is not hitting a wall to block the movement, the variable gox will be set to how much the player will move in the x direction while in air.
Lines 47-59: If the character is jumping it will always be at the jumping frame and the gravity will always be decreasing. If it is not hitting any walls, the x value will change depening upon the value of gox, otherwise gox will be set to 0 to indicate no movement.
Ground Actions
Lines 1-4: The load actions make the ground invisible and set the variable active to false. The ground will only be active if the player is touching it.
Lines 5-10: If the player hits the ground it becomes active, otherwise it is not.
Lines 11-17: If the ground is active, it will get the bounds of it and the bounds of the body inside the character relative to the _root stage.
Lines 18-25: It will test to see if the character is inside the bounds of the ground. If it is, and the player has downward movement, the player will stop jumping and gravity will be set to 0.
Inside Wall Actions
These actions tell the player that it is not hitting the wall. The wall goes to this frame once it is not touching the player. It is done this way to give a slight delay. There were some problems using just enterFrame actions.
Wall Actions
Lines 1-4: Cause the wall to become invisible and non active, similar to the ground.
Lines 5-12 If it hits the player it become active and goes to the second frame (with no actions). Otherwise is is not active and goes to the first frame (sets player hitwall variables to false).
Lines 13-17: If active, get the bounds of the player and wall.
Lines 18-29: If it is active and the right key is down or it is in air and moving right, the hitwallr variable will be set to true and the character will no longer be able to move right. The same is done for moving left.
So, there you have it. This was taken from the game engine of my current project. Any questions? No! Didn't think so!