我有一些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();
}
这是代码,我已经尝试将其调整为在时间轴中工作,但没有任何反应。我尝试将代码添加到EnemyShip
动画片段本身,并将其添加到游戏场景中,但它不起作用。有什么建议吗?
答案 0 :(得分:1)
这有点难过,使用外部类是一种更干净的做事方式,但你决定了。
删除每个类包装器并将此代码放在符号的第一帧:
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();
}