actionscript 3 - calling movieclip of a class from another class, error#1009 -
i got error: method of null object reference. i'm confused , don't know real cause.
got player movieclip on stage has instance of "player_mc", pass thru document class , player class.
player.as
import flash.display.*; import flash.events.*; public class player extends movieclip { public var myplayer:movieclip; public function player(player:movieclip) { myplayer = player; addeventlistener(event.enter_frame,on_enter); }
document.as
import flash.display.*; import components.player.player; public class game_main extends movieclip { public var player:player; public function game_main() { player = new player(player_mc); } }
now here think problem comes from. have green_enemy movieclip on stage has base class enemy.
enemy.as
import flash.display.movieclip; import components.player.player; import flash.events.event; public class enemy extends movieclip { var theplayer:player; public function enemy() { this.addeventlistener(event.enter_frame,on_enter); } public function on_enter(e:event):void { if (this.hittestobject(theplayer.myplayer)) //calls player_mc player class { trace("hi"); } } }
what on enemy function when enemy collides "player_mc" (which on stage) something. maybe code wrong.
help/tips appreciated. thanks!
in enemy.as see
var theplayer:player;
not initialized. private, can not define outside. means exception comes here
this.hittestobject(theplayer.myplayer)
you trying call myplayer
null.
try define variable while constructing enemy class.
to prevent null exception can check if theplayer null
public function on_enter(e:event):void { if (theplayer && this.hittestobject(theplayer.myplayer)) { trace("hi"); } }
Comments
Post a Comment