Author Topic: Ok code, wait in this line till mama comes back.  (Read 868 times)

Patoto

  • Member
  • **
  • Posts: 23
  • Karma: +0/-0
    • View Profile
Hello, Its'a me... Patoto. I'm back and making a new game!  :D

This new game I'm making uses a lot of time related gameplays... which by I mean... "wait 7 seconds", "make a countdown: 5, 4, 3, 2, 1, next level", etc...

So I have this problem. Let's suppose I have this code:

Code: [Select]
line1   private var timer:FlxTimer = new FlxTimer();
line2   
line3   public function thingONE():void{
line4         timer.start(1,1,thingTWO);
line5         trace("RADIOACTIVE PRETZEL"); //I want this to be traced after thingTWO is finished
line6   }
line7   
line8   private function thingTWO(timer:FlxTimer):void{
line9         trace("NUCLEAR DONKEY"); //I want this to be traced first
line10 }

What I want to happen is that when you call thingONE(), it will start the timer, wait 1 second, then go to thingTWO, then trace "NUCLEAR DONKEY", then go back to thingONE, and trace "RADIOACTIVE PRETZEL".

Something like:
line4 (wait 1 second)
line 9
line 5

So it prints:
NUCLEAR DONKEY
RADIOACTIVE PRETZEL

BUT what is actually happening is that it's starting the timer, it starts to count 1 second, and then go to the next line, then, after that 1 second is counted it goes to thingTWO.

Something like:
line 4
line 5
line 9

So it prints:
RADIOACTIVE PRETZEL
NUCLEAR DONKEY

Do you guys understand what I need? If you do, do you know of any way to achieve this? ???

THANK YOU SO MUCH!  :D

Gama11

  • Contributor
  • ****
  • Posts: 390
  • Karma: +0/-0
    • View Profile
Re: Ok code, wait in this line till mama comes back.
« Reply #1 on: Thu, Mar 28, 2013 »
Nuclear Donkey and Radioactive Pretzel? Haha, you have a sense of humour for sure. I'm wondering what that game looks like. :D

Either way, it's quite obvious that your code won't do what you want it to. 

I think your idea is that you call thingONE, the timer starts, and nothing happens until it's finished and thingTWO is called and "NUCLEAR DONKEY" is traced. However, that's not how it works. The timer is running in the background, and thingTWO will just be called when the timer is finished, which doesn't stop line 5 from being reached before. It would be quite impractical if all code stopped executing if a timer is running. ;)

So, what you really need is something like this:

Code: [Select]
private var timer:FlxTimer = new FlxTimer();

public function startTimer(Callback:Function):void
{
     timer.start(1, 1, Callback);
}

private function eventOne(t:FlxTimer):void
{
     trace("NUCLEAR DONKEY");
     t.start(eventTwo);
}

private function eventTwo(t:FlxTimer):void
{
     trace("RADIOACTIVE PRETZEL");
}

You start by calling startTimer(eventOne); somewhere in your code. You don't need to do it like this btw, having a whole function just to start a timer which only takes a single line is kinda overkill. Either way, after one second, when the timer finishes for the first time, eventOne is called, traces "NUCLEAR DONKEY" and starts the timer again, but this time, the callback function is eventTwo. So after one more second, "RADIOACTIVE PRETZEL" is traced.