PDA

View Full Version : Check if object is on stage


jeangj
06-15-2008, 02:34 PM
hi

I'm working on my AS3 shooting game class. The targets come down the stage randomly within a timer. timer = new Timer(Math.random() * 6000);

If i shoot before the target objects are on the stage, I get this error:
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display:DisplayObject/flash.display:DisplayObject::_hitTest()
at flash.display:DisplayObject/hitTestObject()
at UfoGame/:moveBullet()

What I need to do is a check, if a target object is on the stage or not.
Has somebody and idea or example how to do that?

GMaker0507
06-15-2008, 05:03 PM
You could check if itis null before you hitTest

if(TargetMc!=null)
{
if(this.hitTestObject(TargetMc))
{
// Collision Code
}
}

If your looping through an array of targets then you could wait until a target is on stage to add it to that array. You could use the 'ADDED_TO_STAGE' Event.

public function Constructor()
{
this.addEventListener(Event.ADDED_TO_STAGE,OnAdded ToStage)
}

public function OnAddedToStage(e:Event)
{
ArrayOfTargets.push(this)
}

Besides that, i would need to see some of your code to give a better answer

jeangj
06-15-2008, 05:17 PM
yep, that works.
I didn't know that you can create a statement checking for "null".
Thanks a lot for your help.

if(TargetMc!=null) {
if(this.hitTestObject(TargetMc)){
// Collision Code } }