Hey guys, I have seen many tutorials for AS3 but many of them are not the best OOP style programming. I thought I would share some insights on what I have learned.
Initial Setup:
[LIST=1]
[]Make sure your main timeline has 3 KeyFrames.
[]Frame 1 must have an AS3 code:
stop();
[]Frame 2 must have a MovieClip that contains ALL your things in the library (this is used for loading). They do not have to be initiated nor will they ever be used. Simply drag this MovieClip (I call it “Assets”) anywhere OUTSIDE of the main stage.
[]3rd frame is just an EMPTY keyframe.
[/LIST]
Main.as:
package{
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
import flash.ui.ContextMenu;
import flash.ui.Keyboard;
public class Main extends MovieClip{
//control variables
private var prevTime:Number;
//game variables
private var world:World;
public function Main(){
loaderInfo.addEventListener( Event.COMPLETE, onCompleteLoad);
loaderInfo.addEventListener( ProgressEvent.PROGRESS, onProgress);
}
private function setup():void{
//initialize
trace("Started");
world = new World(this);
//finalizing
var hiddenContexMenu:ContextMenu = new ContextMenu();
hiddenContexMenu.hideBuiltInItems();
contextMenu = hiddenContexMenu;
stage.addEventListener(Event.ENTER_FRAME, updateGame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
stage.frameRate = 60;
stage.focus = this;
prevTime = getTimer();
}
private function onCompleteLoad(e:Event):void{
trace("download complete!");
loaderInfo.removeEventListener( Event.COMPLETE, onCompleteLoad);
loaderInfo.removeEventListener( ProgressEvent.PROGRESS, onProgress);
gotoAndStop(3);
setup();
}
private function onProgress(e:ProgressEvent):void{
trace( Math.floor( 100 * loaderInfo.bytesLoaded / loaderInfo.bytesTotal ) );
}
private function updateGame(e:Event):void {
var timeDiff = getTimer() - prevTime;
var duration:Number = timeDiff / 1000;
//updates
world.updateWorld(duration);
//end of updates
//user interface
prevTime = getTimer();
}
private function handleKeyDown(e:KeyboardEvent):void {
var key:int = e.keyCode;
switch (key) {
default :
break;
}
}
private function handleKeyUp(e:KeyboardEvent):void {
var key:int = e.keyCode;
switch (key) {
default :
break;
}
}
}
}
So this class is basically where everything is put into. World class is an object that holds everything in the game (like enemies, player, bonus ect.) This also assumes you are using time-based animation, which the variable “duration” is “dt” or delta time or change in time or the step in update
World.as:
package{
import flash.display.*;
import flash.events.*;
public class World extends MovieClip{
private var rootMC:MovieClip;
public function World(_rootMC:MovieClip){
rootMC = _rootMC;
//initialize
trace("Started World");
//finalize
addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
rootMC.addChild(this);
}
public function updateWorld(duration:Number):void{
//update all objects in world
}
private function handleKeyDown(e:KeyboardEvent):void {
var key:int = e.keyCode;
switch (key) {
default :
break;
}
}
private function handleKeyUp(e:KeyboardEvent):void {
var key:int = e.keyCode;
switch (key) {
default :
break;
}
}
}
}