AS3弹丸移动不正确

时间:2014-04-03 14:31:32

标签: actionscript-3

所以我现在正试图制作一个Bullet Hell游戏的原型,而且我已经陷入了一些死胡同。

到目前为止,我可以完美地移动我的球员,老板按照他应该来回移动,但是射弹有一些有趣的行为。基本上,当老板向左/向右移动时,射弹也就像他们被困在他身上一样。他们按照预期的那样继续前进,除了他们只是停在玩家身边并且不再向前移动,所以我希望任何人都可以看看我的代码然后帮我看一下这些内容。继续。

注意:忽略旋转的东西,以便以后实施,我只是在铺设地面工作。

Projectile.as

package  
{
import flash.display.Stage;
import flash.display.MovieClip;
import flash.events.Event;

public class Projectile extends MovieClip 
{
    private var stageRef:Stage;
    private var _xVel:Number = 0;
    private var _yVel:Number = 0;
    private var rotationInRadians = 0;
    private const SPEED:Number = 10;


    public function Projectile(stageRef:Stage, x:Number, y:Number, rotationInDegrees:Number) 
    {
        this.stageRef = stageRef;
        this.x = x;
        this.y = y;
        this.rotation = rotationInDegrees;
        this.rotationInRadians = rotationInDegrees * Math.PI / 180;
    }

    public function update():void
    {
        this.y += SPEED;;

        if(x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0)
        {
            //this.removeChild(this); <- Causing a crash, will fix later
        }
    }
}
}

Boss.as

package  
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class Boss extends MovieClip
{
    private var stageRef:Stage;
    private var _vx:Number = 3;
    private var _vy:Number = 3;
    private var fireTimer:Timer;
    private var canFire:Boolean = true;
    private var projectile:Projectile;
    public var projectileList:Array = [];

    public function Boss(stageRef:Stage, X:int, Y:int) 
    {
        this.stageRef = stageRef;
        this.x = X;
        this.y = Y;

        fireTimer = new Timer(300, 1);
        fireTimer.addEventListener(TimerEvent.TIMER, fireTimerHandler, false, 0, true);
    }

    public function update():void
    {
        this.x += _vx;
        if(this.x <= 100 || this.x >= 700)
        {
            _vx *= -1;
        }

        fireProjectile();
        projectile.update();
    }

    public function fireProjectile():void
    {
        if(canFire)
        {
            projectile = new Projectile(stageRef, this.x / 200 + this._vx, this.y, 90);
            addChild(projectile);
            canFire = false;
            fireTimer.start();
        }
    }

    private function fireTimerHandler(event:TimerEvent) : void
    {
        canFire = true;
    }
}

}

编辑:目前的建议是执行以下操作: stage.addChild(projectile);this.parent.addChild(projectile);两者都有从左上角(0,0)射出的射弹,而不是从当前的Boss中心不断射击。

另一个未被触及的问题是,射弹在某一点之后停止移动并保持在屏幕上的速度很快。

另一个编辑:

用计时器评出代码后我发现弹丸完全停止移动。它在一定时间后停止的原因是由于计时器,当计时器经过时,弹丸停止而另一个会发射。

所以现在我需要射弹不断射击并移动直到它碰到屏幕边缘,任何想法?

1 个答案:

答案 0 :(得分:0)

问题是你是将你的弹丸“添加”给你的老板而不是舞台(或与你的老板相同的显示水平)。当你的Boss移动时,你的射弹将相对于他移动(即,当他向侧面移动时,它们也将移动)。
当你的老板发射一个射弹时,使用一个自定义事件来触发你的Boss'显示父级的类中的fireProjectile方法。在那里实例化你的射弹并将它们添加到你添加的同一对象中你的老板(可能是舞台?)。
或者,如果您不想使用自定义事件,请在当前的fireProjectile方法中将addChild行更改为:

this.parent.addChild(projectile);

这会将射弹添加到Boss的父对象中。虽然这条线似乎有点像欺骗我。