My sprite will not display, I am making a side scroller on as3

38 Views Asked by At

I am making a game where my rocket ship has to avoid asteroids. However, my rocket ship sprite will not display. Any helps is appreciated.

          var myRocket:MovieClip;
          addChild(myRocket);
          myRocket.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
          myRocket.addEventListener(KeyboardEvent.KEY_UP, keyUp);
          myRocket.x=200;
          myRocket.y=150;
1

There are 1 best solutions below

0
Organis On

Ok, that's how you instantiate things from Library.

First, the Library object must have an AS3 class. You assign it in the Library object's properties. Let's say the class name is SpaceRocket.

Then you do the following:

// Instantiate the object by its class.
var rocket:SpaceRocket = new SpaceRocket;

// Assign coordinates.
rocket.x = 200;
rocket.y = 150;

// Add the instance to the display list.
addChild(rocket);

Also, I don't recommend listening the rocket for the keyboard events. It means the rocket should have the keyboard focus, which can be lost quite easily. You should rather listen it at the stage because all the keyboard events go there eventually:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);