不能在AS3中移动类的对象

时间:2015-06-03 02:17:39

标签: actionscript-3 flash flash-builder

我正在制作一个我需要我的英雄射击的游戏,但我无法理解为什么子弹不会移动。他们出现在舞台上,但他们留在他们出现的地方,不动。我找不到我的错误。谢谢!

所以当按下Space时,这是必须出现并移动子弹的类。

public class Nivel_1
{
    public var mc:MainChar = new MainChar();
    public var Ene:Enemigo = new Enemigo();
    public var bullet:Bullet = new Bullet();

    public var Back:MC_Nivel_1 = new MC_Nivel_1()

    public var Spawn_Boss:Boolean =false;
    public var boss:Boss = new Boss();

    public static var balaVector:Vector.<Bullet> = new Vector.<Bullet>();

    public var disparo:Boolean = false;


    public function Nivel_1()
    {   
    }

    public function Iniciar():void
    {
        Main.escenario.addEventListener(Event.ENTER_FRAME, Update);
        Main.escenario.addChild(Back);
        Ene.Init(600,630);
        mc.Init(100,150);

        Main.escenario.addEventListener(KeyboardEvent.KEY_UP, OnKeyUpLv1);
        Main.escenario.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDownLv1);

        trace ("start");
    }

    public function Update(e:Event = null):void
    {
        updateBala();
        mc.Update();
        Coliciones();

        if(Spawn_Boss)
        {
            boss.Update();
        }
    }

    public function OnKeyUpLv1(e:KeyboardEvent):void
    {
        switch (e.keyCode)
        {
            case Keyboard.SPACE:
                disparo = true;
                if (disparo == true) 
                {
                    var bala:Bullet = new Bullet();
                    bala.inicializar(mc.grafica.x, mc.grafica.y - mc.grafica.height/2 , mc.grafica.scaleX);
                }
                break;
        }
    }

    public function OnKeyDownLv1(e:KeyboardEvent):void
    {
        switch (e.keyCode)
        {
            case Keyboard.SPACE:
                disparo = false;
                break;
        }
    }

    public function Coliciones():void
    {
        for(var i:int = 0; i < balaVector.length ;i++)
        {               
            if(balaVector[i].grafica_1.hitTestObject(Ene.grafica))
            {
                Ene.Destroy();
                balaVector[i].Destruir();
                boss.Init(600,650);
                Spawn_Boss = true;
                break;
            }

            if(balaVector[i].grafica_1.hitTestObject(boss.grafica))
            {
                boss.Destroy();
                balaVector[i].Destruir();
                break;
            }
        }

        if(mc.grafica.hitTestObject(Ene.grafica))
            {
                mc.grafica.x = mc.grafica.x - 3;
                mc.vida --;
            }

        if(mc.grafica.hitTestObject(boss.grafica))
            {
                mc.vida -5;
            }
    }

    public function updateBala():void
    {
        for(var i:int=0;i<balaVector.length;i++)
        {
            balaVector[i].BulletUpdate();
        }
    }

    public function EndGame():void
    {
        mc.Destroy();
        Main.escenario.removeEventListener(Event.ENTER_FRAME,Update);
    }
}

这是子弹的类别。

public class Bullet
{
    public var grafica_1:MC_Bala_1;
    public var velocidad_1:int = 15;
    public var damage_1:int = 1;
    public var direccion:int = 0;

    public function Bullet()
    {
    }

    public function BulletUpdate():void
    {
        Mover();
    }

    public function inicializar(posX:int , posY:int , dir:int):void
    {
        grafica_1 = new MC_Bala_1();
        grafica_1.x = posX;
        grafica_1.y = posY;
        this.direccion = dir;
        Game.balaVector.push(this);
        Main.escenario.addChild(grafica_1);

        trace ("bala");
    }

    public function Mover():void
    {
        grafica_1.x += velocidad_1 * direccion;

        if (grafica_1.x < 0 || grafica_1.x > Main.escenario.stageWidth)
        {
            Destruir();
        }
    }

    public function Destruir():void 
    {
        if (Main.escenario.contains(grafica_1)) Main.escenario.removeChild(grafica_1);
        Game.balaVector.splice(Game.balaVector.indexOf(this),1);

        trace ("destruir bala");
    }   
}

1 个答案:

答案 0 :(得分:0)

你的速度*方向乘以15 * 0 = 0,这就是它不动的原因