FlxButton callback

62 views
Skip to first unread message

kinflo

unread,
May 5, 2016, 10:00:07 AM5/5/16
to HaxeFlixel
Hi there,

I'm trying to give information about the button that just was clicked through the callback but this is not possible.
Is there any way that I can get information of the button I just clicked?
It's a button created in a for loop in the create function, there are multiple buttons and they all callback to one function.
When I clicked a button I want only that one to be deleted.

thanks!

sano98

unread,
May 5, 2016, 11:34:40 AM5/5/16
to HaxeFlixel
Hey kinflo,

maybe this forum post helps.

Basically, they offer 3 different solutions:
  1. Write your own modified button class to accept the required parameter
  2. Modify FlxButton
  3. Use FlxButtonPlus, where you can use parameter arrays.

Dlean Jeans

unread,
May 5, 2016, 1:07:42 PM5/5/16
to HaxeFlixel
Consider binding button index to the callback function like this:

So you said that you want to delete a button:
var buttons = new Array<Button>(); // buttons will be stored here

function deleteButton(buttonID:Int) {
   
var button = buttons[buttonID]; // get the button from the array
    button
= FlxDestroyUtil.destroy(button); // destroy the button
}

Assume that the buttons are created like below
// This will create 10 buttons labeled "Button 0", "Button 1", ...
for (i in 0...10) {
   
var button = new FlxButton(i * 100, i * 50, 'Button $i', deleteButton.bind(i)); // bind the index i to pass the button ID to deleteButton:Int->Void, deleteButton now becomes Void->Void
    buttons
.push(button);
}

kinflo

unread,
May 6, 2016, 4:44:44 AM5/6/16
to HaxeFlixel
Thank you a lot! the bind helped me!!

Reply all
Reply to author
Forward
0 new messages