...

View Full Version : Improving Code



Tristan49
06-13-2012, 04:09 AM
Hi guys! My Actionscript 3 code to make a simple 2D platformer is using almost 100% of my CPU. Can anyone check out my code and see where I could reduce the usage of the processor?

Thanks :D

PS: Total beginner in the coding world.


import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
next.visible=false;

// adding variables to track player speed :)
var speedX=0;
var speedY=0;

// Created a new array for the collectables :D
var level:Array = new Array();
var collectables:Array = new Array();
for (var i=0; i<numChildren; i++) {
if (getChildAt (i) is platform) {
level.push(getChildAt(i).getRect(this));
} else if (getChildAt(i) is Collectable) {
collectables.push(getChildAt(i));
}
}


//make variable to store key strokes
var kUp = false;
var kDown = false;
var kLeft = false;
var kRight = false;

// listen for key presses and releases keys
stage.addEventListener(KeyboardEvent.KEY_DOWN, kD);
stage.addEventListener(KeyboardEvent.KEY_UP, kU);
function kD(k:KeyboardEvent) {
if (k.keyCode==37) kLeft=true;
if (k.keyCode==38) kUp=true;
if (k.keyCode==39) kRight=true;
if (k.keyCode==40) kDown=true;
}
function kU(k:KeyboardEvent) {
if (k.keyCode==37) kLeft=false;
if (k.keyCode==38) kUp=false;
if (k.keyCode==39) kRight=false;
if (k.keyCode==40) kDown=false;
}

//making a looping function
addEventListener(Event.ENTER_FRAME, loop);
function loop(e:Event) {

// lateral movements check
if (kLeft) {
speedX=-10;
player.scaleX=player.scaleY
player.gotoAndStop("run");
player.scaleX=player.scaleY*-1;

} else if (kRight) {
speedX=10;
player.scaleX=player.scaleY
player.gotoAndStop("run");





} else {
speedX*=0.5; //friction
player.gotoAndStop("idle");


}
//move player based on the above
player.x+=speedX;
// sidewards hit tests
for (i=0; i<level.length; i++) {
if (player.getRect(this).intersects(level[i])) {
if (speedX > 0) { //moving right
player.x = level[i].left-player.width/2;
}
if (speedX < 0) { //moving left
player.x = level[i].right+player.width/2;
} //ok!
speedX=0; // kill the speed
}
}
//vertical checks
speedY+=1;
player.y+=speedY;
var jumpable=false;
// hit tests
for (i=0; i<level.length; i++) {
if (player.getRect(this).intersects(level[i])) {
if (speedY > 0) { //moving down
player.y = level[i].top-player.height/2;
speedY=0;
jumpable=true;
}
if (speedY < 0) { //moving up
player.y = level[i].bottom+player.height/2;
speedY*=-0.5; // bounce off the ceiling
} //ok!
speedX=0; // kill the speed
}
}
// jump if possible
if (kUp && jumpable) {
speedY=-20;
}

/* ---------------
THIS IS NEW!
---------------*/
// check for the collectables
for (i=collectables.length-1; i>=0; i--) {
if (player.hitTestObject(collectables[i])) {
// touched a collectable
removeChild(collectables[i]);
collectables.splice(i,1);
}
}
// checking to see if the player "wins"
if (collectables.length<1) {

next.visible=true;
next.addEventListener(MouseEvent.CLICK, endingscene);

}
//-----------------

//move camera with player
this.x=-player.x+(stage.stageWidth/2); //center the player
this.y=-player.y+(stage.stageHeight/2); //center the player

}

function endingscene(event:MouseEvent):void
{
// stop processing key events
stage.addEventListener(KeyboardEvent.KEY_DOWN, kD);
stage.addEventListener(KeyboardEvent.KEY_UP, kU);
// stop the main loop
removeEventListener(Event.ENTER_FRAME, loop);
// reset the camera position
x=0;
y=0;


// Click to go to the next scene. THIS IS FOR YOU CHARLES.
gotoAndStop(1, "End");
}

stop();



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum