动作脚本 3 - AS3 射弹移动不正确



所以我目前正在尝试为子弹地狱游戏制作原型,但我遇到了一些死胡同。

到目前为止,我可以完美地移动我的玩家,老板可以按照他应该的方式来回移动,但是射弹有一些有趣的行为。基本上,当老板向左/向右移动时,射弹也会像粘在他身上一样。他们按照应有的方式继续前进,除了他们停在玩家附近并且不再移动,所以我希望任何人都可以看看我的代码并帮我了解正在发生的事情。

注意:忽略轮换的东西,这是为了以后的实现,我只是在奠定基础。

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的当前中心发射。

另一个未触及的问题是弹丸在某一点后停止移动并留在屏幕上的速度。

另一个编辑:

用计时器注释掉代码后,我发现弹丸完全停止移动。它在一定时间后停止的原因是由于计时器,当计时器过去时,弹丸停止并且另一个会发射。

所以现在我需要弹丸不断射击和移动,直到它击中屏幕边缘,有什么想法吗?

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

this.parent.addChild(projectile);

这会将射弹添加到老板的父对象中。虽然这句话对我来说似乎有点像作弊。

最新更新