Hello, I’m doing a platform game, and it allows player to move around and get coins or hit/get hit by enemies. I keep getting this error a little into the game after collecting some coins… I’m not sure what is wrong, i figured its something to do with the EventListener but i cant get it working… could really use some help here please!
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Coin/eFrame()
In Coin/eFrame()
public class Coin extends Sprite{
//_root will signify the root of the document
private var _root:Object;
public function Coin(){
//this code will only be run once
addEventListener(Event.ADDED, beginClass);
//this code will constantly be run
addEventListener(Event.ENTER_FRAME, eFrame);
}
private function beginClass(event:Event):void{
//defining the root of the document
_root = MovieClip(root);
//making an invisible box that will help in placement
this.graphics.beginFill(0x000000,0);
this.graphics.drawRect(0,0,25,25);
//then adding a shape within it that will show the coin
this.graphics.beginFill(0xFFFFFF,1);
this.graphics.drawCircle(12.5,12.5,2.5);
}
private function eFrame(event:Event):void{
//hit testing with the coin and the main guy
//we'll use math for this!
if(_root.mcMain.x <= this.x + _root.lvlHolder.x + 10
&& _root.mcMain.x >= this.x + _root.lvlHolder.x - 10
&& _root.mcMain.y <= this.y + 10
&& _root.mcMain.y >= this.y - 10){
this.parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, eFrame);
//then we update the score!
_root.mainScore += 100;
}
}
}
}