Author Topic: How do I use a callback Function with parameters Properly?  (Read 1486 times)

Zapleaf

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Code: [Select]
public function FlxButton(X:Number=0,Y:Number=0,Label:String=null,OnClick:Function=null)
{
    // .... Flixel Code
}

Hard to word this so I'm going to be very straight forward I do not wish to come off as rude or ungrateful.

var newButton:FlxButton = new FlxButton( ... , someFunction );    // This will pass the function
var newButton:FlxButton = new FlxButton( ... , someFunction( 15 ) );    // This will trigger the function to run

My problem:  I need the function that gets passed in to have a parameter associated with it.

Not sure what my problem is, thus, Google is zero help.  Been pushing this off for days now working with other parts of the code, but this one really bugs me and I'd like to fix it and move on.

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
You can't do it directly, but you can wrap it in an anonymous function to do what you want:

Code: [Select]
new FlxButton(..., function():void { someFunction(15); });
That passes a function without a parameter which will get called, and when it runs it will call the other function and pass 15. You can have it call multiple functions and whatnot this way by puttinv everything you need in the anonymous function:

Code: [Select]
new FlxButton(..., function():void {
  doSomething();
  doSomethingElse(25);
});

Zapleaf

  • Member
  • **
  • Posts: 11
  • Karma: +0/-0
    • View Profile
You can't do it directly, but you can wrap it in an anonymous function to do what you want:

Code: [Select]
new FlxButton(..., function():void { someFunction(15); });
That passes a function without a parameter which will get called, and when it runs it will call the other function and pass 15. You can have it call multiple functions and whatnot this way by puttinv everything you need in the anonymous function:

Code: [Select]
new FlxButton(..., function():void {
  doSomething();
  doSomethingElse(25);
});

I actually already had one function in my game doing something like this.. it just never ticked in my head that I could make that function call a function.  Thank you again so much!  Also I was on Kongergate last night and was browsing though a few of my old favorite games for some ideas and one of the first ever games I added to my favorites list was Diamond Hollow 2.  I almost died when I noticed the developer was you!  I'm a fan, and having you help me with these silly newbie questions is a huge honor!

Arkeus

  • Contributor
  • ****
  • Posts: 321
  • Karma: +1/-0
    • View Profile
    • I, Arkeus
I actually already had one function in my game doing something like this.. it just never ticked in my head that I could make that function call a function.  Thank you again so much!  Also I was on Kongergate last night and was browsing though a few of my old favorite games for some ideas and one of the first ever games I added to my favorites list was Diamond Hollow 2.  I almost died when I noticed the developer was you!  I'm a fan, and having you help me with these silly newbie questions is a huge honor!

Haha, no problem. I got a lot of help here when I started using Flixel, so I've tried to stick around and help people as much as possible.