Author Topic: Executing a timed function that takes an argument?  (Read 1195 times)

Apophenia

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
I was looking at my (failed) LD48 project from the last time around, which was my first attempt at a solo programming project. Now that I'm reviewing my code, pretty much all of its issues boil down to the fact that I was using some very, very ugly workarounds to get the main mechanic to work. Here's a simplified, concrete example of what I was trying to do:

In the game, the player is a magical color-changing turtle. Every ten seconds, a bell chimes. When the bell chimes, the game determines what color turtle the player should be for the next ten second interval of game play, based on the player's performance. (For example, if the player ate lots of watermelon during the last ten seconds, they will turn into a pink turtle for the next ten seconds when the bell chimes.) Determining what kind of turtle the player should be is handled by getTypeOfTurtle(), which returns a variable value.

What I want to do is create a timer that counts out ten seconds, and then executes makePlayerTurtle, which, in my perfect world, would take a variable argument that it gets from getTypeOfTurtle(). So makePlayerTurtle runs every ten seconds, using the variable returned from getTypeOfTurtle().

tl;dr: the built-in timer class really confuses me. All I want to do is have a function that takes a variable as an argument, and have it execute every ten seconds. But for some reason, I canít work out how to do that with FlxTimer.

This seems like it should be incredibly obvious, but yet, I can't figure out a clean solution.

Iíve seen some suggestions and custom timer classes floating around, but I think most of them predate the addition of FlxTimer.

Esti

  • Active Member
  • ***
  • Posts: 104
  • Karma: +0/-0
    • View Profile
Re: Executing a timed function that takes an argument?
« Reply #1 on: Sun, Dec 9, 2012 »
Something like this:


Code: [Select]
private var rechargeTimer:Number = 0
then in the update:

Code: [Select]
rechargeTimer -= FlxG.elapsed;

if (rechargeTimer <= 0)
{
something
rechargeTimer = 10;
}

Check it out on Mochi!

auriplane

  • Snails!!
  • Contributor
  • ****
  • Posts: 497
  • Karma: +1/-0
  • Snails!!
    • View Profile
Re: Executing a timed function that takes an argument?
« Reply #2 on: Sun, Dec 9, 2012 »
This looks like a very similar question:

http://forums.flixel.org/index.php/topic,6941.0.html

Apophenia

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
That was exactly what I needed, thank you! I wouldn't have thought to phrase it that way when searching.