I'm trying to create a side scrolling platform game in Action script 3 by using flash CC and flash Develop.
Here is the code that I have implemented.
private function startLevel1():void
{
stage.removeEventListener(Event.ENTER_FRAME, mainGameLoop)
//adds event listener to loop
stage.addEventListener(Event.ENTER_FRAME, level1)
}
private function level1(e:Event):void
{
stage.focus = this
processCollision();
var mem:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 );
//trace( mem ); // eg traces “24.94MbLv1”
backBtn.addEventListener(MouseEvent.CLICK, fromLevtoStart)
scrollwithPlayer();
}
private function scrollwithPlayer():void
{
}
private function processCollision():void
{
if (ground.hitTestPoint(character.x, character.y, true))
{
//trace("hits ground");
player.yGravity = 0
player.touchingGround = true;
character.y -= 1;
}
else
{
player.touchingGround = false;
}
/* while (character.y > ground.y)
{
character.y=ground.y;
player.yGravity = 0;
//character.incrementUp();
}*/
character.movementChar();
character.keyListner();
}
This code processes collision and things.
I have a class called player and this moves the player
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
/**
* ...
* @author Moynul Hussain
*/
public class player extends MovieClip
{
public static var yGravity:int = 0;
public static var gravity:Boolean;
public static var xSpeed:int;
public var ySpeed:int;
private var rightKey:Boolean;
private var leftKey:Boolean;
private var upKey:Boolean;
public static var touchingGround:Boolean;
public function player()
{
xSpeed = 3;
ySpeed = 6;
addEventListener(Event.ADDED_TO_STAGE, init)
addEventListener(Event.REMOVED_FROM_STAGE, reset)
}
private function reset(e:Event):void
{
//removeEventListener(Event.REMOVED_FROM_STAGE, reset);
trace("removed");
this.x = 300;
this.y = 500;
yGravity = 0;
//do something
}
private function init(e:Event):void
{
//removeEventListener(Event.ADDED_TO_STAGE, init);
trace("added");
this.x = 500;
this.y = 400;
}
public function keyListner():void
{
this.y += yGravity++;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
}
public function movementChar()
{
if (leftKey)
{
this.x -= xSpeed;
this.gotoAndStop("run");
this.scaleX = -1;
this.x += stage.x ;
}
if (rightKey)
{
this.x += xSpeed;
this.gotoAndStop("run");
this.scaleX = 1;
}
if (upKey)
{
this.y -= 10;
this.gotoAndStop("jump");
//this.scaleX = -1;
}
if (!upKey && !leftKey && !rightKey && touchingGround)
{
this.gotoAndStop("stop");
}
}
private function keyUp(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
leftKey = false;
}
if (e.keyCode == 39)
{
rightKey = false;
}
if (e.keyCode == 38)
{
upKey = false;
}
}
private function keyDown(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
leftKey = true;
}
if (e.keyCode == 39)
{
rightKey = true;
}
if (e.keyCode == 38)
{
upKey = true;
}
}
}
}
My intentions is to scroll the level when the players moves right/ or left. What I don't want to do is move the level when the player presses control keys.
I have attempted to implement a vCam, but this makes the game very laggy, since vCam is fore animation, a I hear.
Sorry if this is a lot to take in.