Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How do I stop a timed event from happening?

New Here ,
Nov 28, 2013 Nov 28, 2013

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?

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 28, 2013 Nov 28, 2013

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

yourinsect.parent.removeChild(yourinsect);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 28, 2013 Nov 28, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 29, 2013 Nov 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 02, 2013 Dec 02, 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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 03, 2013 Dec 03, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 04, 2013 Dec 04, 2013

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

function removeSelf():void
{

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

}
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 05, 2013 Dec 05, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 05, 2013 Dec 05, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 05, 2013 Dec 05, 2013

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);

                    }

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 05, 2013 Dec 05, 2013
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines