Setup

You are provided with all the files correctly setup as needed. Take some time to look at lab11.fla to see how it is setup ( the linkage on the items in the library, the FPS, and the document class ).

When you are ready to begin, create a new file in the same folder called "gameMain.as". The code that is to go into this file is in gameMain.pdf. Short explanations of some of the key items in the file follow.

The game class

After creating a package for the class and importing the correct areas to be used in this file, you define the gameMain class. This class is taking over for the root of the flash movieclip, so it has to extend MovieClip.

Within this class, the first thing you do is define any objects, constants, and other variables that will be used throughout the game. After that, you create a function with the same name as the class ( gameMain ), this is the instantiator function, it is run the instant this class is created, and is your "startup" code. Within this function initialize all your variables for the game, and lastly, start the game loop, in this case we're using "onEnterFrame", which means essentially, all the code in our enterFrame function will be run once every time flash displays a new frame to the user. In this file, that was set to 30fps

The Game loop

Within our game loop is the 'meat' of the game code. We update the player's velocity ( a seperate variable from the players X and Y, this allows us to do physics code on it ), get game input, and check its collision with the stage, every frame

Point based collision detection

Point-based collision detection allows us to check collision based on only the visible parts of a movieclip ( or, the parts of a movieclip with data in it ). In this case, we are taking the x & y coords on the player ( set to the player's feet in the movieclip ), and checking to see if the player has hit the ground. If he has, we run some code to deal with that as needed.

Gravity

Because we have our player's velocity stored outside of his simple x & y, we are able to implement good-enough gravity by simply increasing his downward velocity by our gravity constant every frame. We negate his velocity if he is on the ground

Friction

For good-enough friction, we multiply the player's velocity by a number less than one every turn to decrease his speed by a set amount