Copy link to clipboard
Copied
I am making a game where a user clicks on insects and tries to get a good high score. When the user clicks on one insect, the insect plays an animation and stays in its spot for 2 seconds in this code:
import flash.events.MouseEvent;
import flash.display.MovieClip;
import fl.motion.Animator;
import flash.events.*;
play();
var mysound:squish = new squish();
this.addEventListener(MouseEvent.CLICK, kill);
this.dead = false;
function kill(e:MouseEvent😞void
{
this.dead=true;
mouseChildren=false
mysound.play();
gotoAndPlay(21);
this.removeEventListener(MouseEvent.CLICK, kill);
flash.utils.setTimeout(removeSelf,2000);
}
function removeSelf():void
{
this.parent.removeChild(this);
}
When the user pauses the game, the enemies are stopped and they turn invisible. The only problem is that when the user clicks on the insects, and hits the pause button, the insects stay there for 2 seconds. How do I remove the timer when the person pauses the game so that no insects are on the screen?
Copy link to clipboard
Copied
use removeChild to remove the insects when the game is paused:
yourinsect.parent.removeChild(yourinsect);
Copy link to clipboard
Copied
If I remove the child, how will the insects return back to their original position when I resume the game?
Copy link to clipboard
Copied
they stay in their positiions when removed. if you need to reset their positions, then use addChild to re-add them add a property to those objects to access their original x, y. if you need add a property to know their parent, do so.
Copy link to clipboard
Copied
according to my understanding your question, you want to remove the timer when your are pressing the pause button right,
Instead of using setTimeout
you can use
var delay:Number
function kill(e:MouseEvent):void
{
this.dead=true;
mouseChildren=false
mysound.play();
gotoAndPlay(21);
this.removeEventListener(MouseEvent.CLICK, kill);
delay=setInterval(removeSelf,2000) //it will call the function after 2 sec
}
function removeSelf():void
{
this.parent.removeChild(this);
}
// function for pause button
pause.addEve...........
function pausegame(e:Event)
{
clearInterval(delay) //it will clear the timer
}
Copy link to clipboard
Copied
When I did that, I got an error message saying this:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Enemy/removeSelf()[Enemy::frame1:22]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at SetIntervalTimer/onTimer()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
When I clicked the enemies they died normally, but when I paused they still showed up!
Copy link to clipboard
Copied
you should use my suggestion and change your removeSelf function to:
function removeSelf():void
{
if(this.stage){
this.parent.removeChild(this);
}
}
Copy link to clipboard
Copied
The killed enemies still appear on the menu and do not disappear until after 2 seconds. I don't understand what's wrong..
Copy link to clipboard
Copied
are you using the code i suggested when the pause button is clicked?
show the code you're using when your pause button is clicked.
Copy link to clipboard
Copied
This is the code when the game is paused:
function PauseGame():void
{
if (gameState == STATE_PLAY_GAME)
{
Rchannel.stop();
removeEventListener(Event.ENTER_FRAME, gameLoopR);
for each(var tempEnemy:MovieClip in enemies)
{
tempEnemy.parent.removeChild(tempEnemy);
}
}
}
Copy link to clipboard
Copied
enemies needs to contains references to all your insects and gameState needs to change in PauseGame() and removeSelf should be changed to the code i showed.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now