Actionscript 3 时间线中的Actionscript未按预期工作

Actionscript 3 时间线中的Actionscript未按预期工作,actionscript-3,flash-cs6,Actionscript 3,Flash Cs6,我有一些AS3代码,我想移动到时间轴,而不是有一个外部文件,但它根本不起作用,而它在.as文件中起了作用: public class EnemyShip extends MovieClip { var speed:Number; var shot = new ShotSound(); function EnemyShip() { this.x = 800; this.y = Math.random() * 275 + 75;

我有一些AS3代码,我想移动到时间轴,而不是有一个外部文件,但它根本不起作用,而它在.as文件中起了作用:

public class EnemyShip extends MovieClip
{
    var speed:Number;
    var shot = new ShotSound();

    function EnemyShip()
    {
        this.x = 800;
        this.y = Math.random() * 275 + 75;
        speed = Math.random()*5 + 9;
        addEventListener("enterFrame", enterFrame);
        addEventListener(MouseEvent.MOUSE_DOWN, mouseShoot);
    }

        function enterFrame(e:Event)
        {
            this.x -= speed;
            if(this.x < -100)
            {
                removeEventListener("enterFrame", enterFrame);
                Main.gameLayer.removeChild(this);
            }
        }

        function kill()
        {
            var explosion = new Explosion();
            Main.gameLayer.addChild(explosion);
            explosion.x = this.x;
            explosion.y = this.y;
            removeEventListener("enterFrame", enterFrame);
            Main.gameLayer.removeChild(this);
            Main.updateScore(1);
            shot.play();
        }

        function mouseShoot(event:MouseEvent)
        {
            kill();

        }
public-class-EnemyShip扩展了MovieClip
{
var速度:数字;
var shot=新的ShotSound();
函数Enemmyship()
{
这个.x=800;
this.y=Math.random()*275+75;
速度=Math.random()*5+9;
addEventListener(“enterFrame”,enterFrame);
addEventListener(MouseEvent.MOUSE_DOWN,mouseShoot);
}
函数enterFrame(e:事件)
{
这个.x-=速度;
如果(此.x<-100)
{
removeEventListener(“enterFrame”,enterFrame);
Main.gameLayer.removeChild(this);
}
}
函数kill()
{
var爆炸=新爆炸();
Main.gameLayer.addChild(爆炸);
爆炸.x=这个.x;
explosion.y=这个.y;
removeEventListener(“enterFrame”,enterFrame);
Main.gameLayer.removeChild(this);
主更新核心(1);
shot.play();
}
函数mouseShoot(事件:MouseEvent)
{
杀死();
}

这就是代码,我试着在时间轴中调整它,但什么都没有发生。我试着将代码添加到
EnemyShip
movieclip本身,以及添加到游戏场景中,但都不起作用。有什么建议吗?

这有点令人伤心,使用外部类是一种更干净的方式,但你决定了

删除每个类包装器,并将此代码放在符号的第一帧上:

var speed:Number;
var shot = new ShotSound();

this.x = 800;
this.y = Math.random() * 275 + 75;
speed = Math.random()*5 + 9;
addEventListener("enterFrame", enterFrame);
addEventListener(MouseEvent.MOUSE_DOWN, mouseShoot);

function enterFrame(e:Event)
{
   this.x -= speed;
   if(this.x < -100)
   {
       removeEventListener("enterFrame", enterFrame);
       Main.gameLayer.removeChild(this);
   }
}

function kill()
{
    var explosion = new Explosion();
    Main.gameLayer.addChild(explosion);
    explosion.x = this.x;
    explosion.y = this.y;
    removeEventListener("enterFrame", enterFrame);
    Main.gameLayer.removeChild(this);
    Main.updateScore(1);
    shot.play();
}


function mouseShoot(event:MouseEvent)
{
    kill();
}
var速度:数字;
var shot=新的ShotSound();
这个.x=800;
this.y=Math.random()*275+75;
速度=Math.random()*5+9;
addEventListener(“enterFrame”,enterFrame);
addEventListener(MouseEvent.MOUSE_DOWN,mouseShoot);
函数enterFrame(e:事件)
{
这个.x-=速度;
如果(此.x<-100)
{
removeEventListener(“enterFrame”,enterFrame);
Main.gameLayer.removeChild(this);
}
}
函数kill()
{
var爆炸=新爆炸();
Main.gameLayer.addChild(爆炸);
爆炸.x=这个.x;
explosion.y=这个.y;
removeEventListener(“enterFrame”,enterFrame);
Main.gameLayer.removeChild(this);
主更新核心(1);
shot.play();
}
函数mouseShoot(事件:MouseEvent)
{
杀死();
}

谢谢。代码看起来是正确的,但屏幕上没有任何东西移动。嗯……你把代码放在符号itslef中了吗?如果没有,你应该用你的剪辑名称替换每个
这个
。我现在可以用了,但遇到了另一个问题;当我点击敌舰(杀死它们)时,战舰并没有从舞台上消失(它只是静止不动)爆炸动画在循环中播放。我可以用我的代码更新我的注释,或者发送.fla文件给你看(如果你不介意的话)?大约2MB。这是另一个问题,请尝试自己解决一点,如果找不到解决方案,请发布另一个问题。是的,错误消息是
ArgumentError:error#2025:提供的DisplayObject必须是调用方的子对象。在flash.display::DisplayObjectContainer/removeChild()在Eneyship/kill()处在Eneyship/mouseShoot()上