/* * PlatformGame.as - the game class for the platform game moving platform tutorial * Author: Stephen Harris, Kaiparasoft Ltd * */ import mx.utils.Delegate; class PlatformGame { // game objects var myCharacter:Character; var platforms:Array; var movingPlatforms:Array; // input var goLeft; var goRight; var jump; // input flags var spacePress:Boolean = false; var spaceIsDown:Boolean = false; // game constants var charSpeed = 5; var jumpSpeed = 8; // reference to the main time line var mainTimeline:MovieClip; // Class constructor function PlatformGame(timeLine){ // set the reference to the main time line, which was passed in when this class was created mainTimeline = timeLine; myCharacter = mainTimeline.player; // set the game loop function to be called every frame mainTimeline.onEnterFrame = Delegate.create(this, GameLoop); // set up the obstacles platforms = new Array(); platforms.push(mainTimeline.longPlat); platforms.push(mainTimeline.longPlat2); platforms.push(mainTimeline.smallPlat); platforms.push(mainTimeline.movPlat1); platforms.push(mainTimeline.movPlat2); platforms.push(mainTimeline.movPlat3); platforms.push(mainTimeline.endPlat); // set the flag in the platform objects to make them moving platforms mainTimeline.movPlat1.movPlat = true; mainTimeline.movPlat2.movPlat = true; mainTimeline.movPlat3.movPlat = true; // set the parameters for each moving platform mainTimeline.movPlat3.leftMost = 400; mainTimeline.movPlat3.rightMost = 500; mainTimeline.movPlat3.dirn = -1; mainTimeline.movPlat3.linearSpeed = 3; mainTimeline.movPlat1.leftMost = 186; mainTimeline.movPlat2.leftMost = 318; mainTimeline.movPlat1.rightMost = 244; mainTimeline.movPlat2.rightMost = 369; mainTimeline.movPlat1.dirn = -1; mainTimeline.movPlat2.dirn = 1; mainTimeline.movPlat1.linearSpeed = 2; mainTimeline.movPlat2.linearSpeed = 2; // set the characters reference to this game class so it is accessable from there myCharacter.mainApp = this; } // called every frame function GameLoop(){ ResolveInput(); CheckCollisions(); UpdateMovingPlats(); myCharacter.Update(); } // check all the input states and update them appropriately function ResolveInput(){ // only let the player jump if they're touching the ground or a platform object if(myCharacter.onGround == true){ if(Key.isDown(Key.UP)){ jump = true; } } else{ jump = false; } if(Key.isDown(Key.LEFT) ){ goLeft = true; } else{ goLeft = false; } if(Key.isDown(Key.RIGHT) ){ goRight = true; } else{ goRight = false; } // once the input is set, move the player based on what is currently pressed if(goRight == true){ myCharacter.vx = charSpeed; } if(goLeft == true){ myCharacter.vx = -charSpeed; } // stop if the player has stopped pressing left or right if(goLeft == false and goRight == false){ myCharacter.vx = 0; } // jumping is accomplished by just setting a negative y velocity if(jump == true){ myCharacter.vy = -jumpSpeed; } } // check for collisions with the platforms function CheckCollisions(){ var touchGround = false; if(myCharacter._y > 398){ myCharacter._y = 398; touchGround = true; } for(var i = 0; i < platforms.length; i++){ // if the players base position has hit a platform if( platforms[i].hitTest(myCharacter._x, myCharacter._y, false) ){ touchGround = true; // set the y position to the same as the platform so the player sits neatly on the platform with no penetration myCharacter._y = platforms[i]._y; // if the platform the character has landed on is a moving platform if(platforms[i].movPlat){ // set the characters 'moving platform' reference to the one they are sitting on myCharacter.myMovPlat = platforms[i]; } else{ // set the players moving platform reference to null myCharacter.myMovPlat = null; } } } myCharacter.onGround = touchGround; } // update the movement for the moving platforms function UpdateMovingPlats(){ mainTimeline.movPlat1.Update(); mainTimeline.movPlat2.Update(); mainTimeline.movPlat3.Update(); } }