如何摆脱滞后并优化我的Android游戏?

时间:2013-12-27 06:35:25

标签: android actionscript-3 flash air flash-cs5

我刚刚用flash cs5构建我的Android游戏。我添加了Admob广告,我发现在游戏中,只要广告移动或更改,游戏就会“跳跃”或抖动。有什么方法可以优化我的代码,这样我可以减少迟钝和紧张吗?以下是我的游戏内代码示例:

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 = 23;
var enemyBaseSpeed:int = 4;
var speedLevelInc:Number = 3;
var MAX_SPEED:Number = 22;
score = 0;
var identityMatrix:Matrix=new Matrix()
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 = new Bomb();
        tempBomb.x = Math.round(Math.random() * 480);
        addChild(tempBomb)
        tempBomb.scaleX = 1.5;
        tempBomb.scaleY = 1.5;
        tempBomb.cacheAsBitmapMatrix=identityMatrix;
        bombs.push(tempBomb);
        tempBomb.speed = 5;
    }
}

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 = 14;
            }
            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);
        addChild(tempEnemy);
        tempEnemy.scaleX = 1.5;
        tempEnemy.scaleY = 1.5;
        tempEnemy.cacheAsBitmapMatrix=identityMatrix;
        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 = 14;
            }
            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);
        addChild(tempHeart);
        tempHeart.scaleX = 1.5;
        tempHeart.scaleY = 1.5;
        tempHeart.cacheAsBitmapMatrix=identityMatrix;       
        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 = 14;
            }
            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);
        addChild(tempAtom);
        tempAtom.scaleX = 1.5;
        tempAtom.scaleY = 1.5;
        tempAtom.cacheAsBitmapMatrix=identityMatrix;
        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 = 14;
            }
            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;
    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);
    }
}

这是另一个示例代码,说明单击每个按钮时要执行的操作以及要显示的内容:

import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.events.MouseEvent;
import flash.media.SoundChannel;
flash.media.SoundMixer

stage.quality = StageQuality.MEDIUM
startMenu.play_btn.addEventListener(MouseEvent.CLICK, Clicked);
startMenu.help_btn.addEventListener(MouseEvent.CLICK, HelpClick);
bugsMenu.roach_btn.addEventListener(MouseEvent.CLICK, RClick);
bugsMenu.spider_btn.addEventListener(MouseEvent.CLICK, SClick);
bugsMenu.ants_btn.addEventListener(MouseEvent.CLICK, AClick);
bugsMenu.back_btn.addEventListener(MouseEvent.CLICK, BClick);
bugsMenu.fly_btn.addEventListener(MouseEvent.CLICK, FClick);
bugsMenu.visible = false;
roachLevel.visible = false;
extras_mc.visible = false;
endscreen_mc.visible = false;
endscreenA_mc.visible = false;
endscreenS_mc.visible = false;
endscreenF_mc.visible = false;
extras_mc.visible = false;
information.visible = false;
medals_mc.visible = false;
medals2_mc.visible = false;
help_mc.visible = false;
spiderLevel.visible = false;
AntLevel.visible = false;
flyLevel.visible = false;
Menu_mc.visible = false;
menuA_mc.visible = false;
menuS_mc.visible = false;
menuF_mc.visible = false;
var theme:Intro = new Intro;
var tchannel:SoundChannel;
tchannel = theme.play(0,9999);

function Clicked(event:MouseEvent):void
{
    bugsMenu.visible = true;
    startMenu.visible = false;
}

function HelpClick(event:MouseEvent):void
{
    startMenu.visible = false;
    help_mc.visible = true;
}
function BClick(event:MouseEvent):void
{
startMenu.visible = true;
help_mc.visible = false;
}
function RClick(event:MouseEvent):void
{
    tchannel.stop();
    bugsMenu.visible = false;
    roachLevel.visible = true;
    roachLevel.gotoAndPlay(1);
    gameState = STATE_INIT_GAME;
    addEventListener(Event.ENTER_FRAME, gameLoopR);

}
function SClick(event:MouseEvent):void
{
    tchannel.stop();
    bugsMenu.visible = false;
    spiderLevel.visible = true;
    gameStateS = STATE_INIT_GAMES;
    addEventListener(Event.ENTER_FRAME, gameLoopS);
}
function AClick(event:MouseEvent):void
{
    tchannel.stop();
    bugsMenu.visible = false;
    AntLevel.visible = true;
    gameStateA = STATE_INIT_GAMEA;
    addEventListener(Event.ENTER_FRAME, gameLoopA);
}
function FClick(event:MouseEvent):void
{
    tchannel.stop();
    bugsMenu.visible = false;
    flyLevel.visible = true;
    gameStateF = STATE_INIT_GAMEF;
    addEventListener(Event.ENTER_FRAME, gameLoopF);
}

我已经尝试了一切以使其更加优化,但我不明白我还应该做些什么,请帮忙!

1 个答案:

答案 0 :(得分:0)

第一个建议 - 不要发布超过30-40行代码。您需要隔离问题来源。

第二 - 使用分析工具。 Adobe发布了一个名为Scout的精彩分析工具,用它来查看游戏的性能,内存分配,渲染过程等 在您的情况下,实际上将隔离问题源,因为您将看到您的应用程序在CPU,GPU,内存使用率上的位置 链接到Adobe Scout:http://gaming.adobe.com/technologies/scout/

第三 - 阅读一些关于手机游戏开发的文章。看看你的代码,很明显你甚至没有读过基础知识 例如 - 总是尝试预先计算,池/重用对象,缓存必要的图形,但不要滥用内存。例如 - 您的代码中的Math.PI / 180可以计算一次并存储在变量中,无需每帧计算。

基础:http://gaming.adobe.com - 有很多非常好的文章。

第四,使用OOP方法的框架脚本是专业自杀。考虑使用OOP重构代码,其好处是无数的。

您的问题没有一个非常明确的解决方案 - 您基本上需要编写更好的代码并在此过程中进行测试。