Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使我的代码更优化?_Android_Actionscript 3_Flash - Fatal编程技术网

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

Android 如何使我的代码更优化?,android,actionscript-3,flash,Android,Actionscript 3,Flash,这是我尝试制作的android游戏的代码: 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 = "STAT

这是我尝试制作的android游戏的代码:

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);
    }
}

看看你的代码,我想你最大的性能胜利应该是为在游戏循环中创建的实体实现“对象池”

最昂贵的两个操作是创建对象(使用“new”操作符)和垃圾收集(当您删除某些内容,应用程序决定清除它使用的内存时)

对象池背后的思想是预先创建一组对象,并在整个游戏中重用它们,这样就可以消除对象创建和垃圾收集

因此,在您的情况下,例如,当您创建并摧毁敌人时,每次调用“new敌军()”时,您都会得到性能惩罚,而当旧敌人被移除时,您会得到另一个性能惩罚

通过对象池,你可以在开始屏幕上执行几次“new Equire()”,然后在整个游戏中重复使用这些对象。您将替换tempfound=新的敌人();使用tempEngly=ObjectPool.get(敌人);或者类似的,当你做removeChild(敌人[id]);您还将执行ObjectPool.dispose(敌人[id]);这样就可以把它放回池中重新使用

这听起来很复杂,但实际上很容易实现。我刚找到一段Lee Brimelow的老视频,该视频应该解释如何实现它:

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);
}