PDA

View Full Version : Null object problem


jeangj
06-18-2008, 07:09 PM
Hi peeps

I'm still working on my spaceship shootergame Class and I've got an error which I coulnd't get rid off:-(
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at UfoGame/moveBullet()

On hitTestObject I want to refer to a textfield on frame 3 in the target ufo01_mc tween. (I add the ufo01_mc on the stage via AS3)
The explosion on the ufo01_mc starts on frame 3, where i set a dynamic textfield called pt_txt (show points).

When I set the textfield on frame 1 of the tween it works. However, that's not what it should be. So how can I refer to the textfield in frame 3, since I already set the ufo01_mc.gotoAndPlay(3)???


public function moveBullet(e:Event):void{

// if ufo is on stage, moveBullet, else stop bullet
if(ufo01_mc !=null) {
e.target.y -= 20;
if(e.target.hitTestObject(ufo01_mc)){
ufo01_mc.gotoAndPlay(3);
ufo01_mc.pt_txt.text = types[count].points;
//ufo01_mc.pt_txt.text = "dkj";

points = points + types[count].points;
countPt();

e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
removeChild(MovieClip(e.target));
}
}else{//no bullet
bullet.x = -400;
}

GMaker0507
06-18-2008, 07:14 PM
try this




function moveBullet (e:Event):void {
// if ufo is on stage, moveBullet, else stop bullet
if (ufo01_mc !=null) {
e.target.y -= 20;
if (e.target.hitTestObject(ufo01_mc)) {
ufo01_mc.gotoAndPlay (3);
var txtChild = ufo01_mc.getChildByName("pt_txt");
if (txtChild!=null) {
txtChild=TextField(txtChild);
txtChild.text = types[count].points;
//txtChild.text = "dkj";
}

points = points + types[count].points;
countPt ();

e.target.removeEventListener (Event.ENTER_FRAME, moveBullet);
removeChild (MovieClip(e.target));
}
} else {//no bullet
bullet.x = -400;
}
}

jeangj
06-18-2008, 08:08 PM
that doesn't work. It doesn't even trace txtChild.text = "test";
or if (txtChild!=null) {
txtChild=TextField(txtChild);
txtChild.text = types[count].points;
//txtChild.text = "dkj";
}
else{
trace("null");
}
... hmmm
if i only trace (txtChild);
it shows null

GMaker0507
06-18-2008, 08:45 PM
doesnt work? Really? I use that all the time.

jeangj
06-18-2008, 08:51 PM
i just testet it again and found out, that it shows the text
AFTER I SHOT 2 on the same target object. That's strange.

I guess it's because it somehow refers to frame 1 of the target movieclip
and not frame 3 (explosion)