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

A little help with time for an object

Explorer ,
Apr 29, 2013 Apr 29, 2013

Hi there,

I need some help here: I need to define the code for an object to appear every 3 seconds and do something else 5 seconds after appearing. Do you know how to do that?

Thanks!

TOPICS
ActionScript
514
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 ,
Apr 29, 2013 Apr 29, 2013

if the object is a movieclip, you can use the following but you better do some cleanup or this will eventually crash the flash player:

var appearTimer:Timer=new Timer(3000,0);

appearTimer.addEventListener(TimerEvent.TIMER,appearF);

function appearF(e:TimerEvent):void{

var mc:MovieClip=new WhateverClass();

addChild(mc);

mc.t=new Timer(5000,1);

mc.t.addEventListener(TimerEvent.TIMER,doSomethingElseF);

}

function doSomethingElseF(e:TimerEvent):void{

//do whatever else

}

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
Explorer ,
Apr 29, 2013 Apr 29, 2013

Allright, let me ask you the following:

Lets say I have object 1 and object 2...

in lvl 1 object 1 will appear in 3 secs and will say hi in 5 and object 2 will do the same but in 2,5 secs and 4

in lvl 2 object 1 will appear in 3 secs but will say hi in 4.5 secs and object 2 will appear in 2.5 and will say hi in 3.5

How can you add that into the doSomethingElseF function so when you level up you take out 500 miliseconds from the "do whatever else" thing?

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 ,
Apr 29, 2013 Apr 29, 2013

use a variable for the delay time and assign that variables value using an if-statement that checks the lvl value.

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
Explorer ,
Apr 30, 2013 Apr 30, 2013
LATEST

Great... thanks.... Let me ask you one more thing, to see how can I put this together...

Let me start by saying that instead of coding in classes I am writting directly on the frames...

So frame 1 sends me to frame 2 or 3, depending on what I choose... then, on frame 3 I have:

var enemy1Array:Array = new Array(enem0, enem1, enem2, enem3);

for(var i:int =0; i < 4; i++){

    recycleEnemy(enemy1Array);

}

stage.focus = this;

function removeEnemy(enemy){

   for(var i:int =0; i < 4; i++){

       if(enemy1Array == enemy){

            enemy1Array.splice(i,1);

            removeChild(enemy);

            recycleEnemy(enemy1Array);

        }

    }

}

function touchListener(event:MouseEvent){

    var enemy = event.currentTarget;

    enemy.removeEventListener( MouseEvent.CLICK, touchListener);

    removeEnemy(enemy);

}

function recycleEnemy(enemy:MovieClip):void{

        enemy.x = 50 + Math.random() * (stage.stageWidth - 150);

        enemy.y = Math.random()*stage.stageHeight;

        addChild(enemy);

        enemy.addEventListener( MouseEvent.CLICK, touchListener )

        enemy1Array.push(enemy);

    }

So basically even if it has some erros, where should I put the Timer function here to deal with the enemies?

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