You are provided with all the files correctly setup as needed. Take some time to look at Lab08part1.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.
You will also need to create a new file in the same folder called "key.as". The code that goes into that file is in key.pdf.
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.
Download the lab files. You are provided with all the files correctly setup as needed. Take some time to look at Lab08part2.fla to see how it is setup ( the linkage on the items in the library, the FPS, and the document class ).
Open up gameMain.as to fill in the new code ( you'll have to compare this to what you have already).
The game class:
You will be adding new variables for enemies and bullets, and updating those respectively. In addition, you will add scrolling to make this a true sidescroller
Enemies:
The enemies code will be created in a seperate folder. You will have to create a file badMan.as in the folder "/src/enemies" for the code to work. In addition, the package will have to be "package src.enemies" to compile correctly. This will help you clean your code up immensely in future actionscripting.
The enemy has simple ai defined by a switch statement, and some chase code that works by checking the distance between enemy and player.
Bullets fly straight in this example, but gravity can be applied to it rather simply. Care must be taken not to allow too many bullets on the stage. While flash can handle quite a bit in ActionScript 3.0, each bullet is checking if it is hitting each enemy; with a number of both on the stage, it can get computationally itensive quickly.