actionscript-3 - ActionScript 3、角色选择

标签 actionscript-3 flash game-engine

我正在为我的作业制作一个 Flash 游戏。 现在,在开始之前,我不是要求代码,我要求的是看看如何使我的编码高效。 (此时,如果您愿意,可以给我提示)

在我的游戏中,玩家可以选择两个角色。当玩家选择第一个角色时, bool 值变为真,并且该玩家可以玩游戏。

但我讨厌的是,我必须将代码加倍。

    private function keyUp(e:KeyboardEvent):void 
    {

        if (e.keyCode == 38)
        {
            if (playPrincess)
            {
                upKey = false;
                caroline.standStill();
            }
            else
            if (playSirTimmy)
            {
                upKey = false;
                timmy.standStill();             
            }
        }
    }

例如,从这段代码中您可以看到,如果玩家扮演 timmy,则“playSirTimmy”为 true,如果这是 true,那么我希望代码能说明一切。

这是另一个例子。

    private function SirTimmyMovement():void 
    {
        if (playSirTimmy)
        {
            if (rightKey)
            {
                timmy.moveRight();
            }

            if (leftKey)
            {
                timmy.moveLeft();
            }       

            if (upKey)
            {
                timmy.moveUp();
            }
        }

    }
    private function playPrincessMovement():void 
    {
        if (playPrincess)
        {
            if (rightKey)
            {
                caroline.moveRight();
            }

            if (leftKey)
            {
                caroline.moveLeft();
            }       

            if (upKey)
            {
                caroline.moveUp();
            }
        }

    }

现在我不是在提示或提示,我喜欢编码,这很有趣/具有挑战性,我只是想知道是否有另一种方法可以缩短代码并提高效率。

感谢您的宝贵时间。

只是,当遇到碰撞和敌人时,事情就会变得乏味和困难。

编辑:马丁

    private var character:player;
    private var timmy:SirTimmy;
    private var caroline:princess;


    public function Main() 
    {
        //iniation players
        character = new player;
        timmy = new SirTimmy;
        caroline = new princess;
        stage.addEventListener(Event.ENTER_FRAME, mainGameLoop)
    }

    private function choosePrincess(e:MouseEvent):void 
    {
        if (e.target == princessBtn)
        {
            //playPrincess = true;
            level1Check();
            character = caroline;

        }
    }

    private function startLevel1():void 
    {
        if (easyMode)
        {
            //set variables

        }

        if (medMode)
        {
            //you askng for it
        }

        if (hardMode)
        {
            //set variables harder!
        }


        stage.addChild(character);







    }

谢谢你,很有魅力!

最佳答案

使用一个属性来存储您选择的字符,而不是 bool 值:

private var player:Player;

当您选择角色时,只需更新该属性:

player = timmy; // or caroline

这样,您的所有代码只需要引用它,并且可以更加通用:

private function movement():void 
{
    if(player === null)
    {
        // Don't run this function if neither character has been selected yet.
        return;
    }

    if(rightKey) player.moveRight();
    if(leftKey) player.moveLeft();      
    if(upKey) player.moveUp();
}

关于actionscript-3 - ActionScript 3、角色选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22312590/

相关文章:

iphone - Flash cs5 iOS 按住不放?

javascript - 使用 Javascript 重新创建简单的 Flash 动画

apache-flex - Flex 相当于 Google Visualization Geomap(等值线图)?

c++ - OOP 游戏设计理论

c++ - 游戏 Actor 脚本

function - 我正在寻找一种简单的解决方案,使用下面的现有代码单击缩略图,即可加载新的/不同的mp3声音文件

javascript - 使用ExternalInterface打开浏览器窗口,然后在其中编写HTML

actionscript-3 - 在 Actionscript 中显示 unicode 字符

actionscript-3 - 如何访问 ActionScript 中的顶级包?

用于游戏开发的 C++ 嵌入式脚本语言 - 找不到我喜欢的东西