Copy link to clipboard
Copied
hi , how are you all ?
i made game in 1 stage and it works without errors but i have problem and here are some pictures then i will insert the code
first there is 1 hero, and enemy .and 1 portal
when you touch the enemy it will hit you and you gonna lose hp until death
like here
but there is a portal that you can use it for kill the enemy (the pat )
here
but the problem is : the enemy still hitting you ?? i dont know why ? i remove it and all its events
but it,s still in the stage ??
here is the code of the MAIN class and its placein document file
package {
import flash.display.MovieClip;
import flash.events.Event;
public class MAIN extends MovieClip {
public function MAIN() {
addEventListener(Event.ADDED_TO_STAGE,onAddedToStage)
}
public function onAddedToStage(event:Event):void
{
addEventListener(Event.ENTER_FRAME,onEnterFrame )
addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage)
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
trace("player removed");
}
public function onEnterFrame (event:Event)
{
if (hero.hitTestObject(bat1))
{
healthbar1.width -=1
}
if (healthbar1.width >= 80 && healthbar1.width <= 100 ){
zelda1.gotoAndPlay(1)
}
if (healthbar1.width >= 60 && healthbar1.width <= 80){
zelda1.gotoAndPlay(2)
}
if (healthbar1.width >= 40 && healthbar1.width <= 60){
zelda1.gotoAndPlay(3)
}
if (healthbar1.width >= 20 && healthbar1.width <= 40){
zelda1.gotoAndPlay(4)
}
if (healthbar1.width >= 0 && healthbar1.width <= 20){
zelda1.gotoAndPlay(5)
}
if (hero.hitTestObject(portal2)){
var map2:MAP2 = new MAP2
addChild(map2)
if(map1.stage){
removeChild(map1)
}
}
if (hero.hitTestObject(portal3)){
var map4:BACKGROUND = new BACKGROUND
if(bat1.stage){
removeChild(bat1)
bat1.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
}
}
}
and here is the bat class
package {
import flash.display.MovieClip;
import flash.events.Event;
public class BAT1 extends MovieClip {
public function BAT1() {
addEventListener(Event.ADDED_TO_STAGE,onAddedToStage)
}
public function onAddedToStage(event:Event):void
{
addEventListener(Event.ENTER_FRAME,onEnterFrame )
addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage)
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
trace("bat removed");
}
public function onEnterFrame (event:Event)
{
}
}
}
thats all , also iam sure about that(the pat has removed because i saw this in trace message)
bat1 removed
here
thank you
(yeah right the images dont work auto i dont know why , but you can just click it then it will work )
thanks again)
1 Correct answer
again, removing an object from the stage does not invalidate a hittest. your bat object still exists and it still has x,y properties etc.
you should probably clear your bat objects from memory and revamp your code so you're not testing objects that don't exist. but a quick fix would be to use:
if(hero.hitTestObject(bat1)&&bat1.stage)
Copy link to clipboard
Copied
you don't have a trace("bat1 removed") in your code.
Copy link to clipboard
Copied
sorry i mean (bat removed) you can take a look at the last photo and it,s in the bat1 class
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
trace("bat removed");
}
thank you for your replay
Copy link to clipboard
Copied
removing an object from the display list does not stop your bat from existing and does not cause a bat hit test to be false.
Copy link to clipboard
Copied
why? and how can i fix it?
thank you for your replay
Copy link to clipboard
Copied
here
if (hero.hitTestObject(bat1))
{
healthbar1.width -=1
}
you mean this, right?
but i removed it ((the bat object)) also all its events ! so it shouldn,t be in the stage now ( i guess)
thank you
Copy link to clipboard
Copied
again, removing an object from the stage does not invalidate a hittest. your bat object still exists and it still has x,y properties etc.
you should probably clear your bat objects from memory and revamp your code so you're not testing objects that don't exist. but a quick fix would be to use:
if(hero.hitTestObject(bat1)&&bat1.stage)
Copy link to clipboard
Copied
ohh thank you
just 1 question
this command
if(hero.hitTestObject(bat1)&&bat1.stage)
mean : if the hero hit bat AND bat1.stage?does that mean the bat is on the stage ( iguess yeah ) but why the command like that bat1.stage
thank you for replay
Copy link to clipboard
Copied
bat1.stage is null if bat1 is not in the display list so your if-statement resolves to false which is what you want.
p.s. please mark helpful/correct responses.
Copy link to clipboard
Copied
i get it now
thank you alot kgald.
Copy link to clipboard
Copied
you're welcome.

