Skip to main content
Known Participant
November 29, 2013
Question

How do I stop a timed event from happening?

  • November 29, 2013
  • 2 replies
  • 1317 views

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?

This topic has been closed for replies.

2 replies

Inspiring
December 3, 2013

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

}

razer65Author
Known Participant
December 4, 2013

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!

razer65Author
Known Participant
December 5, 2013

you should use my suggestion and change your removeSelf function to:

function removeSelf():void
{

if(this.stage){
    this.parent.removeChild(this);

}
}


The killed enemies still appear on the menu and do not disappear until after 2 seconds. I don't understand what's wrong..

kglad
Community Expert
Community Expert
November 29, 2013

use removeChild to remove the insects when the game is paused:

yourinsect.parent.removeChild(yourinsect);

razer65Author
Known Participant
November 29, 2013

If I remove the child, how will the insects return back to their original position when I resume the game?

kglad
Community Expert
Community Expert
November 29, 2013

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.