1

I have been stuck on this for a long time and have looked at past questions on here about this similar issue such as this post: How do I access a movieClip on the stage using as3 class?.

I use the constructor to listen for the ADDED_TO_STAGE event, and then initiate the main function to set up the eventListeners from the ADDED_TO_STAGE handler. Within the same handler I also try to get a MovieClip from the stage using the following code:

player = stage.getChildByName("player") as MovieClip;

player is defined globally as a MovieClip.

Within another handler (after the class has been added to the stage) I set the player to go to an particular frame label using player.gotoAndStop("jump");. However an output warning shows up saying "Cannot access a property or method of a null object reference".

Here is the code which I use:

public var player:MovieClip;

public function PlayerControl():void {      
    this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);          
}

private function addedToStage(event:Event):void{
    removeEventListener(Event.ADDED_TO_STAGE, addedToStage);

    this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

    stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);

    player = stage.getChildByName("player") as MovieClip;

}

private function enterFrameHandler(event:Event):void{   
   if(up == true && moviePlaying == false){
      player.gotoAndStop("jump");
      moviePlaying = true;
   }
}
Community
  • 1
  • 1
Human
  • 545
  • 2
  • 7
  • 22
  • 2
    Check if the instance of the MC is actually named "player". Also try tracing all stage's children by their names: `for (var i:int=0;i – Vesper Aug 01 '13 at 16:31
  • Thanks for the comment. I tried the for loop and it shows me these two names: root1, and instance3. When I remove the player MovieClip from the stage and re-run the for loop, instance3 is replaced by instance2. However after changing "player" within the getChildByName to instance3 I get another output error message: "Frame label jump not found in scene jump". – Human Aug 01 '13 at 16:52
  • What do you get when you `trace(player);` or `trace(typeof player);` ? – Pier Aug 01 '13 at 17:05
  • I did `trace(player)` within the external class and it showed: `[object PlayerControl]` (note: PlayerControl is the class name). `trace(typeof player);` showed me: `object`. – Human Aug 01 '13 at 17:09
  • are you sure that 'player' is a `name`, not an `id`? – user1875642 Aug 01 '13 at 17:57
  • I have given the instance name of 'player' to the MovieClip within the stage which I am referencing within the class. This MovieClip has various frames with labels containing other MovieClips which play when a keyDown event is received. – Human Aug 01 '13 at 18:02
  • 4
    Save yourself the headache and drop all timeline animation. Objects are removed/added to the timeline by the Flash runtime when you move from frame to frame, and it causes madness for code. Go to your publish settings and enable "Permit Debugging" to get explicit info, and compile in Debug mode with control-shift-enter. This will give you the call-stack and variable scope list. In all likelihood, you're looking for a property that existed in frame 1 (when you instantiated some objects), and no longer exist at frame label "jump" (where flash removed them to match the defined state). – Atriace Aug 01 '13 at 18:24
  • @Atriace Mm-shouldn'a done that, he's jessa boy. – Code Whisperer Aug 02 '13 at 17:53

0 Answers0