Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //These variables will note which keys are down
- //We don't need the up or down key just yet
- //but we will later
- var leftKeyDown:Boolean = false;
- var upKeyDown:Boolean = false;
- var rightKeyDown:Boolean = false;
- var downKeyDown:Boolean = false;
- //the main speed of the character
- var mainSpeed:Number = 7;
- trace("variables are good");
- //adding a listener to mcMain which will make it move
- //based on the key strokes that are down
- main_mc.addEventListener(Event.ENTER_FRAME, moveChar);
- function moveChar(event:Event):void{
- //if certain keys are down then move the character
- if (leftKeyDown) {
- main_mc.x -= mainSpeed;
- }
- if (rightKeyDown) {
- main_mc.y += mainSpeed;
- }
- if (upKeyDown || mainJumping) {
- // "||" means OR
- mainJump();
- }
- }
- trace("moveChar is good");
- //listening for the keystrokes
- //this listener will listen for down keystrokes
- stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
- function checkKeysDown(event:KeyboardEvent):void{
- //making the booleans true based on the keycode
- //WASD Keys or arrow keys
- if(event.keyCode == 37 || event.keyCode == 65) {
- leftKeyDown = true;
- }
- if(event.keyCode == 38 || event.keyCode == 87) {
- upKeyDown = true;
- }
- if(event.keyCode == 39 || event.keyCode == 68) {
- rightKeyDown = true;
- }
- if(event.keyCode == 40 || event.keyCode == 83) {
- downKeyDown = true;
- }
- }
- trace("checkKeysDown is good");
- //this listener will listen for keys being released
- stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
- function checkKeysUp(event:KeyboardEvent):void{
- //making the booleans false based on the keycode
- if(event.keyCode == 37 || event.keyCode == 65) {
- leftKeyDown = false;
- }
- if(event.keyCode == 38 || event.keyCode == 87) {
- upKeyDown = false;
- }
- if(event.keyCode == 39 || event.keyCode == 68) {
- rightKeyDown = false;
- }
- if(event.keyCode == 40 || event.keyCode == 83) {
- downKeyDown = false;
- }
- }
- trace("checkKeysUp is good");
- //whether or not the main guy is jumping
- var mainJumping:Boolean = false;
- //how quickly should the jump start off
- var jumpSpeedLimit:int = 15;
- //the current speed of the jump;
- var jumpSpeed:Number = jumpSpeedLimit;
- trace("variables 2 is good");
- //jumping function
- function mainJump():void{
- //if main isn't already jumping
- if(!mainJumping){
- /*putting an exclamation mark in front of a
- boolean inverts the condition. An exclamation
- means 'not'. This statement says:
- "if not mainJumping". Or rather, "if not true".
- So the results of this statement is false.*/
- //then start jumping
- mainJumping = true;
- jumpSpeed = jumpSpeedLimit*-1;
- main_mc.y += jumpSpeed;
- } else {
- //then continue jumping if already in the air
- //crazy math that I won't explain
- if(jumpSpeed < 0){
- jumpSpeed *= 1 - jumpSpeedLimit/75;
- if(jumpSpeed > -jumpSpeedLimit/5){
- jumpSpeed *= -1;
- }
- }
- if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit) {
- jumpSpeed *= 1 + jumpSpeedLimit/50;
- }
- main_mc.y += jumpSpeed;
- //if main hits the floor, then stop jumping
- //of course, we'll change this once we create the level
- if(main_mc.y >= stage.stageHeight - main_mc.height) {
- mainJumping = false;
- main_mc.y = stage.stageHeight - main_mc.height;
- }
- }
- }
- trace("mainJump is good");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement