How to display or move sprite in as3 side scroller?

36 Views Asked by At

I have a sprite in my library called myRocket but it won't show up.

I made my sprite a movie clip then I deleted it off the actual page so it was just in the library. I thought using this code, the sprite would appear once I started up the game but it does not.

import flash.display.MovieClip;

stop() ;

           removeChild(myButton);
           var myReturn:Return=new Return();
           addChild(myReturn);
           myReturn.x=390;
           myReturn.y=10;
           myReturn.addEventListener(MouseEvent.CLICK, return1Function);

           var up:Boolean;
           var down:Boolean;
           var left:Boolean;
           var right:Boolean;

           var speed:int;


           function return1Function(evt:MouseEvent):void{
               gotoAndStop("menu");
           }

           var myRocket:MovieClip;
           addChild(myRocket);
           myRocket.x=200;
           myRocket.y=150;

            function KeyboardDemo() {

                 myRocket.x = 200;
                 myRocket.y = 100;
                 addChild(myRocket);

                 stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);

               }

function keyPressedDown(event:KeyboardEvent):void {
  var key:uint = event.keyCode;
  var step:uint = 5
  switch (key) {
      case Keyboard.LEFT :
        myRocket.x -= step;
        break;
      case Keyboard.RIGHT :
        myRocket.x += step;
        break;
      case Keyboard.UP :
        myRocket.y -= step;
        break;
      case Keyboard.DOWN :
        myRocket.y += step;
        break;
  }
}
1

There are 1 best solutions below

0
Vesper On

You need to initialize your object before you place it to the stage. You have only declared a variable at the line "var myRocket:MovieClip;" instead it should at least (not sure about whatever logic you have there" read like above with "Return" whatever it was: var myRocket:MovieClip=new Rocket(); Here "Rocket" is the library name of the sprite.