Flash game slows down
I am almost finished with this flash game, but when I test it the game slows down overtime. I've read the script over and over. Tried to comment parts out of the script, but it still slows down.
This is my whole script:
//loads keyboard events
import flash.events.KeyboardEvent
var laatstelook:int = 1;
var ironbird:Boolean = false;
var laatstejump = 1;
var movebirdie:int = 2;
var birdfatigue : Boolean = false;
var birdtellery:int = 0;
var birdtelleryvertrager = 0;
var vy:Number=0;
var movement:Boolean=false;
var springen:Boolean=false;
var gv:Number=0.1;
var keyArray:Array = new Array();
var i:Number;
var leftOrrightpressed:Number=0;
var platformraak=false;
var PlatformArray:Array = new Array();
var StudioLightArray:Array = new Array();
var MovingPlatformArray:Array = new Array();
var MovingStudioLightArray:Array = new Array();
var SpeakerArray:Array = new Array();;
var birdieRespawnY = 90.1;
var birdieRespawnX = 40.75;
var campowerupPositionX = 275.3;
var speed : int = -2;
var lightspeed : int = -2;
var setmoveto = false;
var birdieAttack : Boolean = false;
var shitlist : Array = [];
var shit:ShitMissile = new ShitMissile();
var IronManMusic:Sound = new IronManSong();
var IronManChannel:SoundChannel = new SoundChannel();
var backgroundMusic:Sound = new BackgroundMusic();
var myChannel:SoundChannel = new SoundChannel();
myChannel = backgroundMusic.play();
powerupscreen.visible = false;
powerupscreen.stop();
for(i=0;i<222;i++){
keyArray.push([i,false]);
}
//creating multiple objects from 1 object
for (var a:int = numChildren - 1; a >= 0; a--){
var child:DisplayObject = getChildAt(a);
if (child.name == "platform"){
PlatformArray.push(child);
}
}
for (var b:int = numChildren - 1; b >= 0; b--){
var child2:DisplayObject = getChildAt(b);
if (child2.name == "studiolight"){
StudioLightArray.push(child2);
}
}
for (var c:int = numChildren - 1; c >= 0; c--){
var child3:DisplayObject = getChildAt(c);
if (child3.name == "movingplatform"){
MovingPlatformArray.push(child3);
}
}
for (var d:int = numChildren - 1; d >= 0; d--){
var child4:DisplayObject = getChildAt(d);
if (child4.name == "movingstudiolight"){
MovingStudioLightArray.push(child4);
}
}
for (var speaker:int = numChildren -1; speaker >= 0; speaker--){
var child5:DisplayObject = getChildAt(speaker);
if(child5.name == "speaker"){
SpeakerArray.push(child5);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp);
this.addEventListener(Event.ENTER_FRAME, UpdateScreen);
function UpdateScreen(event:Event):void{
/*trace(birdie.x, "birdie");
trace(vcam.x);*/
//moves platform
movingplatform.y += speed;
if(movingplatform.y <= 25){
speed = +2;
}
if(movingplatform.y >= 350){
speed = -2;
}
//moves stodiolight light
if(setmoveto == true){
movingstudiolight.x += lightspeed;
if(movingstudiolight.x <= 2270){
lightspeed = +2;
}
if(movingstudiolight.x >= 3115){
lightspeed = -2;
}
}
//respawn points
if(birdie.y >= 450){
birdie.x = birdieRespawnX;
birdie.y = birdieRespawnY;
vcam.x = campowerupPositionX;
powerupscreen.x = campowerupPositionX;
}
if(birdie.x >= 533 && birdie.x <= 537){
birdieRespawnX = 535.75;
birdieRespawnY = 200.15;
campowerupPositionX = 519.75
}
if(birdie.x >= 2097 && birdie.x <= 2190){
birdieRespawnX = 2141.5
birdieRespawnY = 62.55;
campowerupPositionX = 2155.3;
}
//No multiple jumps in air
if(springen==true){
birdie.gotoAndStop(laatstejump);
birdtelleryvertrager+=0.2;
birdie.y-=15;
birdie.y+=birdtelleryvertrager;
if(birdie.y <= 19){
birdie.y += 15;
}
birdtellery++;
if(isKeyDown(39)==true){
birdie.x += 5;
background.x += 1;
powerupscreen.x += 5
vcam.x += 5;
if(birdie.x <= 261.95){
vcam.x = 275.5;
powerupscreen.x = 275.5;
background.x -= 1;
}
if(ironbird != true){
birdie.gotoAndStop(5);
}else{birdie.gotoAndStop(13);
}
}
if(isKeyDown(37)==true){
birdie.x-=5;
powerupscreen.x-=5;
vcam.x -= 5;
background.x -= 1;
if(birdie.x <= 261.95){
vcam.x = 275.5;
powerupscreen.x = 275.5
background.x += 1;
}
birdie.gotoAndStop(6);
}
//bird falls down
if(birdtellery>25){
birdfatigue == true;
if(isKeyDown(39)==true){
birdie.x-=4;
vcam.x -= 4;
powerupscreen.x-=4;
}
if(isKeyDown(37)==true){
birdie.x += 4;
vcam.x += 4;
}
birdtelleryvertrager+=0.8;
}
//bird can jump again (when on platform)
if(birdtellery>30){
springen=false;
}
}else{
if(birdie.y <= 19){
birdie.y += 15;
}
if(birdfatigue == true){
birdie.gotoAndStop(7);
}
//if right arrow button is pressed
if(isKeyDown(39)==true){
birdie.x += movebirdie;
if(birdie.x >= 261.95){
powerupscreen.x += movebirdie;
vcam.x += movebirdie;
background.x += 0.5;
}
if(ironbird != true){
birdie.gotoAndStop(2);
laatstelook =1;
}else{birdie.gotoAndStop(11);
laatstelook =9;
}
laatstejump =5;
leftOrrightpressed =1;
}
if(isKeyDown(39)==false){
if (leftOrrightpressed ==1){
birdie.gotoAndStop(laatstelook);
}
}
//if left arrow button is pressed
if(isKeyDown(37)==true){
birdie.x -= movebirdie;
background.x -= 0.5;
powerupscreen.x -= movebirdie;
vcam.x -= movebirdie;
if(birdie.x <= 261.95){
powerupscreen.x = 275.5
vcam.x = 275.5;
background.x += 0.5;
}
if(ironbird != true){
birdie.gotoAndStop(3);
laatstelook =4;
}else{birdie.gotoAndStop(12);
laatstelook = 10;
}
laatstejump=6;
leftOrrightpressed =2;
}
if(isKeyDown(37)==false){
if (leftOrrightpressed ==2){
birdie.gotoAndStop(laatstelook);
}
}
//if space button is pressed
if(isKeyDown(32)==true && springen==false){
springen=true;
}
if(isKeyDown(17) == true){
stage.addChild(shit);
shitlist.push(shit);
shit.x = birdie.x;
shit.y = birdie.y;
birdie.gotoAndStop(14);
}
shit.y++;
}
if(birdie.hitTestPoint(ironbirdpickup.x,ironbirdpickup.y,true)){
ironbirdpickup.y = -60;
powerupscreen.play();
p开发者_JAVA技巧owerupscreen.visible = true;
ironbird = true;
IronManChannel = IronManMusic.play();
myChannel.stop();
}
if(ironbird==true){
laatstejump = 9;
}
addEventListener(Event.ENTER_FRAME,ctrl_birdie);
function ctrl_birdie(e:Event){
//when bird touches one of the platforms, bird stops falling
for(var a in PlatformArray){
if(PlatformArray[a].hitTestPoint(birdie.x,birdie.y,true)){
birdtelleryvertrager=0;
birdtellery = 0;
birdie.y-=1;
}
}
for(var b in StudioLightArray){
if(StudioLightArray[b].hitTestPoint(birdie.x,birdie.y,true)){
birdtelleryvertrager=0;
birdtellery = 0;
birdie.y-=1;
}
}
for(var c in MovingPlatformArray){
if(MovingPlatformArray[c].hitTestPoint(birdie.x,birdie.y,true)){
birdtelleryvertrager=0;
birdtellery = 0;
birdie.y-=1;
}
}
for(var d in MovingStudioLightArray){
if(MovingStudioLightArray[d].hitTestPoint(birdie.x,birdie.y,true)){
birdtelleryvertrager=0;
birdtellery = 0;
birdie.y-=1;
birdie.x = movingstudiolight.x;
powerupscreen.x = birdie.x
vcam.x = birdie.x;
setmoveto = true;
}
}
for(var speaker in SpeakerArray){
if(SpeakerArray[speaker].hitTestPoint(birdie.x+2,birdie.y,true)){
birdie.x -= 5;
}
}
}
//gravity
vy = 10;
birdie.y+=vy;
}
//checks if certain key is pressed
function checkKeysDown(event:KeyboardEvent):void{
keyArray[event.keyCode][1]=true;
}
function checkKeysUp(event:KeyboardEvent):void{
keyArray[event.keyCode][1]=false;
}
function isKeyDown(X){
return keyArray[X][1];
}
If you have some optimizing tips or even found the error in my code, please leave a comment. Thanks in advance! :)
I can't really tell, your code is quite long, but are you adding an event listener for ENTER_FRAME
inside an event listener for ENTER_FRAME
to a function which is redefined at each iteration?
If so:
- get
ctrl_birdie
out of the listener (put it at the same level as your other methods), - call
addEventListener(Event.ENTER_FRAME,ctrl_birdie)
only once and - remove it when you don't need it anymore (if you don't need it anymore at a point).
If it doesn't solve your memory leak, at least it will make your code a bit easier to read.
精彩评论