如何使我的代码更加优化?

时间:2013-12-21 16:30:42

标签: android actionscript-3 flash

这是我试图制作的安卓游戏的代码:

import flash.events.MouseEvent;
import flash.media.SoundChannel;
import flash.display.MovieClip;


var STATE_INIT_GAME:String = "STATE_INIT_GAME";
var STATE_PLAY_GAME:String = "STATE_PLAY_GAME";
var STATE_END_GAME:String = "STATE_END_GAME";
var gameState:String;
var hearts:Array;
var atoms:Array;
var bombs:Array;
var enemies:Array;
var level:Number;
var score:Number;
var lives:Number;
var tempHeart:MovieClip;
var tempBomb:MovieClip;
var tempAtom:MovieClip;
var tempEnemy:MovieClip;
var rSound:rMusic = new rMusic  ;
var Rchannel:SoundChannel;
var offset:int = 20;
var enemyBaseSpeed:int = 4;
var speedLevelInc:Number = 3;
var MAX_SPEED:Number = 100;
score = 0;
roachLevel.score_txt.text = String(score);

function gameLoopR(e:Event):void
{
    switch (gameState)
    {
        case STATE_INIT_GAME :
            initGame();
            break;
        case STATE_PLAY_GAME :
            playGame();
            break;
        case STATE_END_GAME :
            endGame();
            break;
    }
}
function initGame():void
{

    Rchannel = rSound.play(0,9999);
    level = 1;
    roachLevel.level_txt.text = String(level);
    lives = 3;
    roachLevel.lives_txt.text = String(lives);
    hearts = new Array();
    bombs = new Array();
    atoms = new Array();
    enemies = new Array();
    gameState = STATE_PLAY_GAME;
}
function playGame():void
{

    makeEnemies();
    moveEnemies();
    makeHearts();
    moveHearts();
    makeBombs();
    moveBombs();
    makeAtoms();
    moveAtoms();
    testForEnd();
}
function makeBombs():void
{
    var chance:Number = Math.floor(Math.random() * 6000);
    if (chance <=  +  level)
    {
        tempBomb.scaleX = 1.5;
        tempBomb.scaleY = 1.5;
        tempBomb = new Bomb();
        tempBomb.x = Math.round(Math.random() * 480);
        tempBomb.cacheAsBitmap = true;
        addChild(tempBomb)
        bombs.push(tempBomb);
        tempBomb.speed = 1;
    }
}

function moveBombs():void
{
    var tempBomb:MovieClip;

    for (var h:int =bombs.length-1; h>=0; h--)
    {

        tempBomb = bombs[h];
        if (tempBomb.dead)
        {
            Rchannel.stop();
            lives = 0;
            roachLevel.level_txt.text = String(lives);
            bombs.splice(h,1);
        }
        else
        {
            tempBomb.rotation += (Math.round(Math.random()*.4));
            tempBomb.y +=  (Math.cos((Math.PI/180)*tempBomb.rotation))*tempBomb.speed;
            if (tempBomb.x < 10)
            {
                tempBomb.x = 11;
            }
            if (tempBomb.x > stage.stageWidth - offset)
            {
                tempBomb.x = stage.stageWidth - offset;
            }
            if (tempBomb.y > stage.stageHeight)
            {
                removeBomb(h);

            }
        }
    }
}
function makeEnemies():void
{
    var chance:Number = Math.floor(Math.random() * 150);
    if (chance <= level && enemies.length < 4)
    {
        tempEnemy = new Enemy();
        tempEnemy.x = Math.round(Math.random() * 480);
        tempEnemy.cacheAsBitmap = true;
        addChild(tempEnemy);
        tempEnemy.scaleX = 1.5;
        tempEnemy.scaleY = 1.5;
        enemies.push(tempEnemy);

        tempEnemy.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);
        if (tempEnemy.speed > MAX_SPEED)
        {
            tempEnemy.speed = MAX_SPEED;

        }


    }

}

function moveEnemies():void
{
    var tempEnemy:MovieClip;


    for (var i:int =enemies.length-1; i>=0; i--)
    {
        tempEnemy = enemies[i];
        if (tempEnemy.dead)
        {
            score++;
            score++;
            roachLevel.score_txt.text = String(score);
            enemies.splice(i,1);

        }
        else
        {

            tempEnemy.rotation += (Math.round(Math.random()*.4));
            tempEnemy.y +=  (Math.cos((Math.PI/180)*tempEnemy.rotation))*tempEnemy.speed;
            if (tempEnemy.x < 10)
            {
                tempEnemy.x = 11;
            }
            if (tempEnemy.x > stage.stageWidth - offset)
            {
                tempEnemy.x = stage.stageWidth - offset;
            }
            if (tempEnemy.y > stage.stageHeight)
            {
                removeEnemy(i);

                lives--;
                roachLevel.lives_txt.text = String(lives);
            }
        }
    }
}
function makeHearts():void
{
    var chance:Number = Math.floor(Math.random() * 8000);
    if (chance <=  +  level)
    {
        tempHeart = new Heart();
        tempHeart.x = Math.round(Math.random() * 480);
        tempHeart.cacheAsBitmap = true;
        addChild(tempHeart);
        tempHeart.scaleX = 1.5;
        tempHeart.scaleY = 1.5;
        hearts.push(tempHeart);

        tempHeart.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);
    }
}
function moveHearts():void
{
    var tempHeart:MovieClip;

    for (var k:int =hearts.length-1; k>=0; k--)
    {

        tempHeart = hearts[k];
        if (tempHeart.dead)
        {
            lives++;
            roachLevel.lives_txt.text = String(lives);
            hearts.splice(k,1);
        }
        else
        {
            tempHeart.rotation += (Math.round(Math.random()*.4));
            tempHeart.y +=  (Math.cos((Math.PI/180)*tempHeart.rotation))*tempHeart.speed;
            if (tempHeart.x < 10)
            {
                tempHeart.x = 11;
            }
            if (tempHeart.x > stage.stageWidth - offset)
            {
                tempHeart.x = stage.stageWidth - offset;
            }
            if (tempHeart.y > stage.stageHeight)
            {
                removeHeart(k);
            }
        }
    }
}

function makeAtoms():void
{
    var chance:Number = Math.floor(Math.random() * 7500);
    if (chance <=  +  level)
    {
        tempAtom = new Atom();
        tempAtom.x = Math.round(Math.random() * 480);
        tempAtom.cacheAsBitmap = true;
        addChild(tempAtom);
        tempAtom.scaleX = 1.5;
        tempAtom.scaleY = 1.5;
        atoms.push(tempAtom);
        tempAtom.speed = enemyBaseSpeed + ((level - 1) * speedLevelInc);
    }
}


function moveAtoms():void
{
    var tempAtom:MovieClip;

    for (var c:int =atoms.length-1; c>=0; c--)
    {

        tempAtom = atoms[c];
        if (tempAtom.dead)
        {
            score++;
            score++;
            score++;
            score++;
            score++;
            roachLevel.score_txt.text = String(score);
            atoms.splice(c,1);
        }
        else
        {
            tempAtom.rotation += (Math.round(Math.random()*.4));
            tempAtom.y +=  (Math.cos((Math.PI/180)*tempAtom.rotation))*tempAtom.speed;
            if (tempAtom.x < 10)
            {
                tempAtom.x = 11;
            }
            if (tempAtom.x > stage.stageWidth - offset)
            {
                tempAtom.x = stage.stageWidth - offset;
            }
            if (tempAtom.y > stage.stageHeight)
            {
                removeAtom(c);

            }
        }
    }
}
function removeEnemy(id:int)
{

    removeChild(enemies[id]);
    enemies.splice(id,1);
}
function removeHeart(kd:int)
{

    removeChild(hearts[kd]);
    hearts.splice(kd,1);
}
function removeBomb(hd:int)
{

    removeChild(bombs[hd]);
    bombs.splice(hd,1);
}
function removeAtom(cd:int)
{

    removeChild(atoms[cd]);
    atoms.splice(cd,1);
}
function testForEnd():void
{
    if (score > level * 20)
    {
        level++;
        roachLevel.level_txt.text = String(level);
    }
    if (lives == 0)
    {
        gameState = STATE_END_GAME;
    }

}
function endGame():void
{
    removeGame();
    roachLevel.visible = false;
    Menu_mc.visible = false;
    endscreen_mc.visible = true;
    removeEventListener(Event.ENTER_FRAME, gameLoopR);
    showresults();

}
function removeGame():void
{
    for (var i:int = enemies.length-1; i >=0; i--)
    {
        removeEnemy(i);
    }
    for (var h:int = bombs.length-1; h >=0; h--)
    {
        removeBomb(h);
    }
    for (var k:int = hearts.length-1; k >=0; k--)
    {
        removeHeart(k);
    }
    for (var c:int = atoms.length-1; c >=0; c--)
    {
        removeAtom(c);
    }
}

游戏在Android上完美运行,一切正常。但是有一个问题。当敌人的速度增加时,游戏变得迟钝并且敌人变得更加难以点击。 这是敌人的代码。这是一个由动作脚本添加到舞台上的影片剪辑。

import flash.events.MouseEvent;
import flash.display.MovieClip;
import fl.motion.Animator;
import flash.events.*;
play();
var mysound:squish = new squish(); 
this.addEventListener(MouseEvent.CLICK, kill);
this.dead = false;
function kill(e:MouseEvent):void 
{
    this.dead=true;
    mouseChildren=false
    mysound.play();
    gotoAndPlay(21);
    this.removeEventListener(MouseEvent.CLICK, kill);
    flash.utils.setTimeout(removeSelf,2000);

}

function removeSelf():void
{
    this.parent.removeChild(this);
}

1 个答案:

答案 0 :(得分:1)

查看你的代码我猜你最大的表现就是为在游戏循环中创建的实体实现'Object Pooling'。

两个最昂贵的操作是创建对象(使用'new'操作符)和垃圾收集(当你删除某些内容并且app决定清除它正在使用的内存时)。

对象池背后的想法是预先创建一堆对象并在整个游戏中重用它们,以便消除对象创建和垃圾收集。

因此,在你的情况下,例如你创建并摧毁敌人的地方,每次你召唤'新敌人()'时都会受到性能下降,而当旧敌人被移除时,你会受到另一次性能下降。

使用对象池,您基本上可以在开始屏幕上执行几次'new Enemy()',然后在整个游戏中重复使用这些相同的对象。你将取代tempEnemy = new Enemy(); with tempEnemy = ObjectPool.get(Enemy);或类似的,当你做removeChild(敌人[id]);你也会做ObjectPool.dispose(enemies [id]);这样它就会被放回池中以便重复使用。

听起来很复杂但实际上它实际上非常简单易行。我刚刚发现了一个旧的Lee Brimelow视频,应该解释如何实现它:http://www.gotoandlearn.com/play.php?id=160