I don't know how to describe it? ( change button position in a function)

Hello   :slight_smile:

 

I am planning to make a simple animation to tow buttons. The animation should start after user pressed one of the buttons. The Problem is that the button that gets pressed, calls the handleButtonFuntion. In the function, a transition gets called but the button gets created after the function… I hope my text makes any sense to you.

 

The code will explain it better than the text!

 

Code:

 

local function handleBtnEdit( event )

 

if ( “ended” == event.phase ) then

print( “Button was pressed and released BTNEdit” )

 

–Do something!

 

end

end

 

– Create the widget

local btnEdit = widget.newButton(

{

label = “Bearbeiten”,

fontSize = 20,

labelColor = { default = { 0, 0, 0, 0.6 }, over = { 0, 0, 0, 0.6 } },

onEvent = handleBtnEdit,

emboss = false,

– Properties for a rounded rectangle button

shape = “roundedRect”,

width = 150,

height = 35,

cornerRadius = 5,

fillColor = { default = {1 / 255 * 0, 1 / 255 * 143, 1 / 255 * 236}, over = {1 / 255 * 0, 1 / 255 * 160, 1 / 255 * 236} },

}

)

 

– Center the button

btnEdit.x = display.contentCenterX

btnEdit.y = display.contentCenterY + 20

grpUI:insert(btnEdit)

 

 

 

local function handleBtnBestaetigen( event )

 

if ( “ended” == event.phase ) then

print( “Button was pressed and released Bestätigen” )

 

transition.to( btnEdit, { time = 500, x = display.contentCenterX + 80 } ) – This button is already existing and the transition works

transition.to( btnBestaetigen, { time = 1000, x = display.contentCenterX - 80 } )  – The Problem

 

end

end

 

– Create the widget

local btnBestaetigen = widget.newButton(

{

label = “BESTÄTIGEN”,

fontSize = 20,

labelColor = { default = { 0, 0, 0, 0.6 }, over = { 0, 0, 0, 0.6 } },

onEvent = handleBtnBestaetigen,

emboss = false,

– Properties for a rounded rectangle button

shape = “roundedRect”,

width = 150,

height = 35,

cornerRadius = 5,

fillColor = { default = {1 / 255 * 2, 1 / 255 * 200, 1 / 255 * 115}, over = {1 / 255 * 2, 1 / 255 * 225, 1 / 255 * 150} },

 

}

)

 

– Center the button

btnBestaetigen.x = display.contentCenterX

btnBestaetigen.y = display.contentCenterY + 20

grpUI:insert(btnBestaetigen)

Thanks for helping and sorry for my English skills :DD

Hi, looks like a typical lua scope issue.

Try forward declaring the variable but adding local btnEdit and local btnBestaetigen to the top of your code

The remove the word “local” from the variable further down where you actually creat the objects.

Tip! usually, it is easier to help if you post the actual error code your editor output :slight_smile:

Thank you very much! I already have done this in an other project but I forgot it  :huh:

And there was no error code?! The only issue was the button wasn’t moving. But thanks for your tip! 

I’ve only been programming in Lua for almost 1 year now, with little programming background, so although i dont get the scope issues that often anymore, the one about too many upvalues is still bugging me at times. Found out that this is very often just a forgotten 'end" at some point in code, which makes corona believe the function is insanly large  :slight_smile:

I havent used the button widget yet, and am unlikely to do so, so don’t know it that well.

The only widgets i’ve found useful so far is scrollView and tableView.

It baffles me that lua has this scope issue but I suppose one can get used to dealing with almost anything.

My brother does dot.net for a living, and he cant stop laughing when I tell or show him the issues I sometimes have  :smiley:

Keep it up mate!

Edit - I want to point out that the corona API also has many strong points over dot.net, but scope definitely isnt one.

Oh cool! I have tried Corona the first time in August this year in an internship and since then I was keen! 

Do you have any apps in the app-store / play-store? 

The good thing about C# is that generally the compiler will tell you before you even run the program that you did something stupid, like trying to access something that doesn’t exist or the function doesn’t have access to.

But you’d still have to forward-declare at the top of your class so it’s not that different really. Ok, you could declare all your variables at the bottom and they would still be ‘in scope’ but that would be weird!

Being able to put methods in whatever order you like in a class without worrying if they can see each other is nice, though.

I am working on my first app right now, which will be available on all platforms.

Its not a game though, but related to music streaming.

I have made one release, which started as an internal solution to understanding particle systems.

Its available in the asset store here

https://marketplace.coronalabs.com/asset/radiance-particles-effects-creator-v1-42

There will be a major upgrade to it as soon as I am done with the current project and can get back to focusing on it.

I plan to make it available for all platforms and am implementing adaptive scaling.

A major GUI redesign is already in place.

Hi nick,

I think my brother codes in visual studio but i dont think it is pure C#

Anyway, I’ve been on a few remote sessions and yes you do have to declare variables but the order of functions in the code, for calling eachother, dont seem to matter like in lua. Probably what irritate me the most is that if a function is unique, why would the language prevent me from calling it from wherever i am in the code. Can get tricky at times, especially with a lot of nested network callbacks.

Hi, looks like a typical lua scope issue.

Try forward declaring the variable but adding local btnEdit and local btnBestaetigen to the top of your code

The remove the word “local” from the variable further down where you actually creat the objects.

Tip! usually, it is easier to help if you post the actual error code your editor output :slight_smile:

Thank you very much! I already have done this in an other project but I forgot it  :huh:

And there was no error code?! The only issue was the button wasn’t moving. But thanks for your tip! 

I’ve only been programming in Lua for almost 1 year now, with little programming background, so although i dont get the scope issues that often anymore, the one about too many upvalues is still bugging me at times. Found out that this is very often just a forgotten 'end" at some point in code, which makes corona believe the function is insanly large  :slight_smile:

I havent used the button widget yet, and am unlikely to do so, so don’t know it that well.

The only widgets i’ve found useful so far is scrollView and tableView.

It baffles me that lua has this scope issue but I suppose one can get used to dealing with almost anything.

My brother does dot.net for a living, and he cant stop laughing when I tell or show him the issues I sometimes have  :smiley:

Keep it up mate!

Edit - I want to point out that the corona API also has many strong points over dot.net, but scope definitely isnt one.

Oh cool! I have tried Corona the first time in August this year in an internship and since then I was keen! 

Do you have any apps in the app-store / play-store? 

The good thing about C# is that generally the compiler will tell you before you even run the program that you did something stupid, like trying to access something that doesn’t exist or the function doesn’t have access to.

But you’d still have to forward-declare at the top of your class so it’s not that different really. Ok, you could declare all your variables at the bottom and they would still be ‘in scope’ but that would be weird!

Being able to put methods in whatever order you like in a class without worrying if they can see each other is nice, though.

I am working on my first app right now, which will be available on all platforms.

Its not a game though, but related to music streaming.

I have made one release, which started as an internal solution to understanding particle systems.

Its available in the asset store here

https://marketplace.coronalabs.com/asset/radiance-particles-effects-creator-v1-42

There will be a major upgrade to it as soon as I am done with the current project and can get back to focusing on it.

I plan to make it available for all platforms and am implementing adaptive scaling.

A major GUI redesign is already in place.

Hi nick,

I think my brother codes in visual studio but i dont think it is pure C#

Anyway, I’ve been on a few remote sessions and yes you do have to declare variables but the order of functions in the code, for calling eachother, dont seem to matter like in lua. Probably what irritate me the most is that if a function is unique, why would the language prevent me from calling it from wherever i am in the code. Can get tricky at times, especially with a lot of nested network callbacks.