Liam
Liam

Reputation: 65

Actionscript in timeline not working as intended

I had some AS3 code that I wanted to move to the timeline rather than having an external file but it's not working at all, whereas it did in the .as file:

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();

        }

That is the code and I've tried adapting it to work in the timeline but nothing happens. I tried adding the code to the EnemyShip movieclip itself as well as adding it to the in game scene but it doesn't work. Any suggestions?

Upvotes: 0

Views: 353

Answers (1)

Kodiak
Kodiak

Reputation: 5978

This is a bit sad, using an external class is a cleaner way to do things, but you decide.

Remove every class wrapper and put this code on the first frame of your symbol:

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();
}

Upvotes: 1

Related Questions