PDA

View Full Version : hiTest bug, or am I just stupid?



ikazuchi
September 16th, 2010, 09:27 PM
Long story short, I created a plane shooting @ the boss. When the bullet hits Boss, it is removed. When the boss' life hits 0, it is removed. Both are removed via this.parent.removeChild(this).

The problem is that after the boss "dies" (no syntax/runtime errors), the hitTestObject still seem to be active. I now have this blank spot where the Boss used to be that is detecting and removing the bullet.

hitTest code:

if (this.hitTestObject(target)){
this.removeEventListener(Event.ENTER_FRAME, moveBullet);
MovieClip(this.parent).removeChild(thisBullet);
target.hit(25);
}

remove code:

this.removeEventListener(Event.ENTER_FRAME, eFrameEvents);
if(this.parent){
this.parent.removeChild(this);
}

Maybe i am overlooking something so minute?

Maqrkk
September 17th, 2010, 07:49 AM
You only removed it from the stage, it is still active.
I'd handle this by sending an event to the parent, like this:


dispatchEvent(new Event("removeMe"));

In the parent, set an eventListener on the boss when creating it like so:


boss.addEventListener("removeMe", removeBoss);

Then create the function to completely remove the boss:


private function removeBoss(e:Event):void
{
var boss:Boss is e.target;
removeChild(boss);
boss = null;
}

kadaj
September 17th, 2010, 08:09 AM
Its called inertia. :trout:

ikazuchi
September 17th, 2010, 12:03 PM
You only removed it from the stage, it is still active.
I'd handle this by sending an event to the parent, like this:


dispatchEvent(new Event("removeMe"));

In the parent, set an eventListener on the boss when creating it like so:


boss.addEventListener("removeMe", removeBoss);

Then create the function to completely remove the boss:


private function removeBoss(e:Event):void
{
var boss:Boss is e.target;
removeChild(boss);
boss = null;
}

I coudn't quite grasp your method, but I will try on a fresh code to see if it works for me. Thanks for the tip though, 'dispatchEvent' is something new I didnt know existed!

As for my code, I got it to work by placing a 'hit' mc inside the Boss class and it was able to delete the hitTest after removing the child 'hit' mc.
Maybe it was because I used addChild to call the Boss class onto the stage, and not into a container.

SticksStones
October 5th, 2010, 08:20 PM
Personally I wouldn't use listeners when I don't need them. They sit in memory mostly doing nothing....I'm not a fan....don't get me started on timers!
...although, I know most people probably disagree...after all, flash is an event driven language!

Maqrkk is right though "you only removed it from the stage, it is still active." - it must be.
I'm not 100% sure why though without seeing more code. I'd double check all the listeners have been removed and keep what you had before but set the object to null afterwards.


this.removeEventListener(Event.ENTER_FRAME, eFrameEvents);
if(this.parent){
this.parent.removeChild(this);
this = null;
}