Go Back   CodingForums.com > :: Client side development > Flash & ActionScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-13-2012, 04:09 AM   PM User | #1
Tristan49
New to the CF scene

 
Join Date: Jun 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Tristan49 is an unknown quantity at this point
Improving Code

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

PS: Total beginner in the coding world.

Code:
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();
Tristan49 is offline   Reply With Quote
Reply

Bookmarks

Tags
code cpu usage too high

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:47 AM.


Advertisement
Log in to turn off these ads.