actionscript 3 - Error 1009 : Cannot access a property of method of a null object reference -


i don't understand going on main.as

package  {  import flash.display.movieclip; import flash.events.mouseevent;  public class main extends movieclip {      public var pirkles:circles = new circles()      public function main() {          gotoandstop(1)          playbtn.addeventlistener(mouseevent.click,  playscreen)      }      public function playscreen(event:mouseevent):void {          gotoandstop(2)          addchild(pirkles)      } }  } 

and circles.as

package  { import flash.display.movieclip import flash.events.event; import flash.events.keyboardevent; import flash.ui.keyboard import flash.events.mouseevent;  public class circles extends movieclip{      public function circles():void {         stage.addeventlistener(keyboardevent.key_down, move)         this.y = 175         this.x = 10         }      public function move(event:keyboardevent):void {          if (event.keycode == keyboard.right) {              this.x = this.x+10          }         else if (event.keycode == keyboard.left) {                this.x = this.x-10          }         else if (event.keycode == keyboard.up) {              this.y = this.y-10          }         else if (event.keycode == keyboard.down) {              this.y = this.y+10          }      }  }  }  

now error says there problem @ line 11 of circles.as , @ line 8 of main.as. however, @ lines don't understand causing problem. added event listener @ line 11, when take out works. also, @ line 8, defined variable.

you can't access stage in class constructor. line

 stage.addeventlistener(keyboardevent.key_down, move) 

is causing error.

if need access stage, add listener in constructor added_to_stage event, , in callback function able access stage

so:

public function circles():void {         this.addeventlistener (event.added_to_stage, onaddedtostage);         this.y = 175         this.x = 10 }  private function onaddedtostage (evt:event):void {                  stage.addeventlistener(keyboardevent.key_down, move)  } 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -