Author Topic: [Mod: v1.47+] FlxCore Timed Events  (Read 1891 times)

Markham

  • Guest
[Mod: v1.47+] FlxCore Timed Events
« on: Tue, Dec 22, 2009 »
I'm going to start posting mods for Flixel that I've made that I think others might find useful.  This mod adds timed events to the FlxCore object.

public function addTimer(time:Number,event:Function, repeat:Boolean = false):void
time - The time in seconds before the event triggers.
event - The function that is called when the timer hits 0.
repeat (optional) - Set this to "true" to have this event to occur on a routine basis.

Open FlxCore.as
Find:
Code: [Select]
protected var _flickerTimer:Number;Add after:
Code: [Select]

//@desc Timed events
private var _timerArray:Array;


Find:
Code: [Select]
//@desc Constructor
public function FlxCore()
{
...
_flickerTimer = -1;
Add after:
Code: [Select]
_timerArray = new Array();

Find:
Code: [Select]
virtual public function update():void
{
...
visible = !_flicker;
}
}
Add after:
Code: [Select]

var i:uint;
var tl:uint = _timerArray.length;
for(i = 0; i < tl; i++) {
var tChild:Object = _timerArray[i];
tChild.tRemaining -= FlxG.elapsed;
if (tChild.tRemaining <= 0) {
tChild.e();
if (tChild.r)
tChild.tRemaining = tChild.tStart;
else {
_timerArray.splice(i--,1);
tl--;
}
}
}


Find:
Code: [Select]
virtual public function reset(X:Number,Y:Number):void
{
...
}
Add after:
Code: [Select]

//@desc Adds a timed event
//@param time The time in seconds before the event triggers
//@param event The function that is called when the timer hits 0
//@param repeat Set this to "true" to have this event to occur on a routine basis
function addTimer(time:Number,event:Function, repeat:Boolean = false):void {
_timerArray.push({tStart:time,tRemaining:time,e:event,r:repeat});
}


Open FlxState.as
Find:
Code: [Select]
virtual public function destroy():void { _layer.destroy(); }Add after:
Code: [Select]

//@desc Adds a timed event
virtual public function addTimer(time:Number,event:Function, repeat:Boolean = false):void { _layer.addTimer(time,event, repeat);}

**EDIT: fixed bug that removed the wrong event from the timer array**
« Last Edit: Tue, Dec 22, 2009 by Markham »

bobbybaker82

  • Active Member
  • ***
  • Posts: 155
  • Karma: +0/-0
    • View Profile
Re: [Mod: v1.47+] FlxCore Timed Events
« Reply #1 on: Mon, Dec 28, 2009 »
Cool stuff, I'll have to implement this when I upgrade to the latest version.

Richard Kain

  • Active Member
  • ***
  • Posts: 231
  • Karma: +0/-0
    • View Profile
Re: [Mod: v1.47+] FlxCore Timed Events
« Reply #2 on: Sun, Jan 3, 2010 »
Don't forget that they will need to import the FlxG class into their extended FlxCore class. (in order to get the elapsed variable) I don't think that FlxG is called by default for the root FlxCore class.

Markham

  • Guest
Re: [Mod: v1.47+] FlxCore Timed Events
« Reply #3 on: Sun, Jan 3, 2010 »
This is intended to be a direct modification of FlxCore and FlxState files, so that the function can be called in sprites, buttons, etc. through inheritance.  FlxCore also refers to FlxG variables at least 10 other times by default.