0

我试图制作一个简单的游戏,僵尸从屏幕边缘随机生成,一旦它们生成,它们将获得玩家的最后一个位置并继续朝那个方向移动,直到它们撞上舞台并被移除。但是,我似乎遇到了追逐代码的问题。即使在阅读了大量提供相同代码块的文章之后,我仍然无法使其工作(我做错了 ARRGH)。

这是主要课程

package 
{
import flash.display.*;
import flash.events.*;
import flash.utils.*;
import flash.ui.*;

public class main extends MovieClip
{
    private var left,right,up,down,fire,mouseRight,mouseLeft:Boolean;
    private var speedX,speedY,mobPosX,mobPosY:int;
    private var num:Number;
    private var wizard:Player;
    private var crosshair:Crosshair;
    private var mobArray,magicArray:Array;

    public function main()
    {
        //Hide mouse for crosshair
        Mouse.hide();
        crosshair = new Crosshair();
        addChild(crosshair);
        crosshair.x = stage.stageWidth;
        crosshair.y = stage.stageHeight;

        //Set initial speed
        speedX = 0;
        speedY = 0;

        //Wizard stuff
        fire = false;
        wizard = new Player();
        addChild(wizard);
        wizard.x = stage.stageWidth / 2;
        wizard.y = stage.stageHeight / 2;

        //Mob stuff
        mobArray = new Array();
        magicArray = new Array();


        //Initialize bool so movement doesn't get stuck on startup
        up = false;
        down = false;
        left = false;
        right = false;

    }

    public function startGame()
    {



        addEventListener(Event.ENTER_FRAME,update);
        stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
        stage.addEventListener(MouseEvent.CLICK, myClick);


    }

    private function keyDownHandler(evt:KeyboardEvent)
    {

        if (evt.keyCode == 37)
        {
            left = true;
        }

        if (evt.keyCode == 38)
        {
            up = true;

        }

        if (evt.keyCode == 39)
        {
            right = true;
        }

        if (evt.keyCode == 40)
        {
            down = true;
        }

        if (evt.keyCode == 67)
        {
            trace(mobArray.length);
        }

    }

    private function keyUpHandler(evt:KeyboardEvent)
    {
        if (evt.keyCode == 37)
        {
            left = false;
            speedX = 0;
        }

        if (evt.keyCode == 38)
        {
            up = false;
            speedY = 0;
        }

        if (evt.keyCode == 39)
        {
            right = false;
            speedX = 0;
        }

        if (evt.keyCode == 40)
        {
            down = false;
            speedY = 0;
        }

    }

    function myClick(eventObject:MouseEvent)
    {
        fire = true;
    }


    public function update(evt:Event)
    {
        //UI
        crosshair.x = mouseX;
        crosshair.y = mouseY;

        //Start player
        if (left && right == false)
        {
            speedX = -8;
        }

        if (up && down == false)
        {
            speedY = -8;
        }

        if (down && up == false)
        {
            speedY = 8;
        }

        if (right && left == false)
        {
            speedX = 8;
        }

        wizard.x +=  speedX;
        wizard.y +=  speedY;

        for (var i = mobArray.length - 1; i >= 0; i--)
        {
            //Start mob                                 //if X is zero and Y > 10, spawn.

            var m:Zombie = new Zombie();
            m.chase((Math.atan2(mobArray[i].y - wizard.y,mobArray[i].x - wizard.x)/Math.PI * 180)); //applies trigo
            num = Math.random();
            if (num < 0.1)
            {
                //when x = 0
                mobPosX = 10;
                mobPosY = Math.floor(Math.random() * 590) + 10;
            }
            else if (num < 0.3)
            {
                //when y = 0
                mobPosX = Math.floor(Math.random() * 790) + 10;
                mobPosY = 10;
            }
            else if (num < 0.6)
            {
                mobPosX = 800;
                mobPosY = Math.floor(Math.random() * 590) + 10;
                //when x width of screen
            }
            else if (num < 0.9)
            {
                //y is height of screen
                mobPosX = Math.floor(Math.random() * 790) + 10;
                mobPosY = 590;
            }
            m.x = mobPosX;
            m.y = mobPosY;
            mobArray.push(m);
            addChild(m);
        }




    }

    private function gameOver()
    {
        removeEventListener(Event.ENTER_FRAME,update);
        stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
        stage.removeEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
    }



}//end class
 }//end package

这是僵尸类

package 
  {
import flash.display.*;
import flash.events.*;

public class Zombie extends MovieClip
{
    private var zombSpd:Number;
    private var angle:Number;

    public function Zombie()
    {
        zombSpd = 10;
    }

    public function chase(chaseAngle:Number)
    {
        angle = chaseAngle;
    }


    public function update()
    {
        this.x+=Math.cos(angle*Math.PI/180)*zombSpd;
        this.y+=Math.sin(angle*Math.PI/180)*zombSpd;
    }


}//end class
   }//end package

谢谢 :)

4

2 回答 2

1

我只是重读了您的代码,发现在尝试创建这样的游戏之前,您必须阅读并了解更多有关编程的知识。

你的僵尸没有产生的原因是你永远不会进入 for 循环。mob 数组长度一开始就为零,所以 for 循环永远不会发生。

把那个 for 循环出来并创建一个函数来在你的主类中生成敌人。您可以使用您在 for 循环中使用的代码作为此函数示例的代码:

function spawn():void{
    //code directly from your for loop, with a small change            
        num = Math.random();
        if (num < 0.1)
        {
            //when x = 0
            mobPosX = 10;
            mobPosY = Math.floor(Math.random() * 590) + 10;
        }
        else if (num < 0.3)
        {
            //when y = 0
            mobPosX = Math.floor(Math.random() * 790) + 10;
            mobPosY = 10;
        }
        else if (num < 0.6)
        {
            mobPosX = 800;
            mobPosY = Math.floor(Math.random() * 590) + 10;
            //when x width of screen
        }
        else if (num < 0.9)
        {
            //y is height of screen
            mobPosX = Math.floor(Math.random() * 790) + 10;
            mobPosY = 590;
        }
        var m:Zombie = new Zombie();
        m.x = mobPosX;
        m.y = mobPosY;
        m.chase((Math.atan2(m.y - wizard.y,m.x - wizard.x)/Math.PI * 180)); //applies trigo
        mobArray.push(m);
        addChild(m);
}

现在在您的更新功能中,您必须确定何时要生成敌人。您可以使用计数器来做到这一点。我会让你弄清楚那部分,但如果你想让僵尸不断产生,(你在更新函数中的 forloop 所在的位置)添加这个:

//use a counter variable to determine when to execute spawn
spawn();
//loop through all zombies
for(var i:int = 0; i < mobArray.length; i++){
    mobArray[i].update();
}
于 2012-08-15T05:24:57.553 回答
-1

AdvanceEDActionScriptAnimation 中的转向行为示例,具有抽象接口和编组实现。Github:https ://github.com/yangboz/as3SteeringBehavior

于 2013-02-17T15:58:26.740 回答