Flash游戏速度变慢

时间:2011-04-06 08:40:47

标签: flash actionscript-3

我几乎已经完成了这个Flash游戏,但是当我测试它时游戏会加速减速。我一遍又一遍地阅读剧本。试图从脚本中评论部分,但它仍然会减慢速度。

这是我的整个剧本:

//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();
        powerupscreen.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];
}

如果您有一些优化提示或甚至在我的代码中发现错误,请发表评论。提前致谢! :)

1 个答案:

答案 0 :(得分:3)

我无法确切地说,你的代码很长,但你是在ENTER_FRAME的事件监听器中为ENTER_FRAME添加一个事件监听器到一个在每次迭代时重新定义的函数吗? / p>

如果是这样的话:

  • 从监听器中获取ctrl_birdie(将其放在与其他方法相同的级别),
  • 只打电话addEventListener(Event.ENTER_FRAME,ctrl_birdie)一次
  • 当你不再需要它时将它移除(如果你不再需要它的话)。

如果它无法解决您的内存泄漏问题,至少会使您的代码更容易阅读。