Chazara
Chazara

Reputation: 169

Corona widget.newButton passing data to function

I have scoured the documentation and google for a solution but none was found.

Simply: I want to pass data to a function, when a button is pressed. Issue: The buttons are dynamically created, so the data must be presented when the button is built.

The code looks like this;

    myButton = widget.newButton({
        x = width * 0.875,
        y = height * heightChange,
        width = width * 0.18,
        height = height * 0.09,
        defaultFile = "Assets/Images/button_up.png",
        overFile = "Assets/Images/button_down.png",
        onEvent = func_myFunction
    })

But I want to do something like;

    onEvent = func_myFunction("My Data")

or

    onEvent = func_myFunction, myData

neither of which work. Any ideas?

Upvotes: 1

Views: 125

Answers (1)

Mahmoud Yasser
Mahmoud Yasser

Reputation: 26

It's simple you just need to add an eventlistener to the end of your code so when it's clicked your data would execute.

myButton = widget.newButton({
    x = width * 0.875,
    y = height * heightChange,
    width = width * 0.18,
    height = height * 0.09,
    defaultFile = "Assets/Images/button_up.png",
    overFile = "Assets/Images/button_down.png",
    onEvent = func_myFunction
})
myButton:addEventListener("touch",onEvent);

I hope this fixes your problem.

Upvotes: 1

Related Questions