Don’t worry
I already tried printing but it doesn’t work as well because the value of the _21pass variable isn’t changing , log is clean.
I am just staring at this for 1 hour and can’t figure out lol. Thank you for your help mate.
Don’t worry
I already tried printing but it doesn’t work as well because the value of the _21pass variable isn’t changing , log is clean.
I am just staring at this for 1 hour and can’t figure out lol. Thank you for your help mate.
Okay, next step!
In the function confirm21, as the first line, print the value of _21pass. Does it print nil, 0 or 1. Or 0 and 1?
Funny thing happening, I put a string variable tracking the _21pass values and it (tracker) remains 0 all the time. But the printing commands are now working good (each time I click it prints 0 then 1 then 0 then 1… as it was supposed to) , how come the print works and the transition doesn’t?
here’s a sample of the new code :
local \_21pass = 0 local tracker = display.newText( \_21pass, centerX, centerY, 40,40, "Gotham", 30 ) function confirm21( event ) \_21pass = 1 - \_21pass if \_21pass == 0 then print("0") transition.to( \_21button, { alpha = 0, time = 500 } ) end if \_21pass == 1 then print("1") transition.to( \_21button, { alpha = 1, time = 500 } ) end local buttonsound = audio.loadSound( "rightp.mp3" ) audio.play( buttonsound ) end local \_21button = display.newImageRect( "okgreen@2x.png", 17.5, 17.5) \_21button.x = centerX - 99 \_21button.y = centerY + 67 \_21button:addEventListener( "tap", confirm21 ) \_21button.alpha = 0 \_21button.isHitTestable = true
Hi,
You are not tracking the variable _21pass with your text object. You are only setting it once. Text objects don’t dynamically update their content in Lua. If you want the text of “tracker” to change, you need to change tracker.text to a new string yourself.
Are you getting a clean, single 0 or 1 variable logged, or are the following eachother almost instantly? And is the sound playing? For the record, you should declare buttonsound before your function confirm21.
Great! now my tracking text is working properly but no transition though… Any more clues?
local \_21pass = 0 local tracker = display.newText( \_21pass, centerX, centerY, 40,40, "Gotham", 30 ) function confirm21( event ) \_21pass = 1 - \_21pass local buttonsound = audio.loadSound( "rightp.mp3" ) audio.play( buttonsound ) if \_21pass == 0 then print("0") tracker.text = \_21pass transition.to( \_21button, { alpha = 0, time = 500 } ) end if \_21pass == 1 then tracker.text = \_21pass print("1") transition.to( \_21button, { alpha = 1, time = 500 } ) end end local \_21button = display.newImageRect( "okgreen@2x.png", 17.5, 17.5) \_21button.x = centerX - 99 \_21button.y = centerY + 67 \_21button:addEventListener( "tap", confirm21 ) \_21button.alpha = 0 \_21button.isHitTestable = true
yes , single 0 and 1 each time I click. Sound is playing.
I’m stumped! The transition code is correct, because if I place it elsewhere it works, and the print logging shows that the code goes to the right places… Wow! It’s been a while since such a simple piece of code was a stumbling block…
Anyone else care to try? I stripped down the code to the essentials here:
local pass = 0 function confirm( event ) pass = 1 - pass if pass == 0 then transition.to(button, {time = 500, alpha = 0}) print("0") end if pass == 1 then transition.to(button, {time = 500, alpha = 1}) print("1") end end local button = display.newRect(100,100,100,100) button:addEventListener( "tap", confirm ) button.alpha = .5
I feel your pain, it’s frustrating lol hopefully tomorrow someone will come up with an answer. That’s enough for the day to me. Good night friend, thanks for trying , appreciate it!
You’re not observing scope and visibility rules with regards to _21button.
Also, why is alpha 1 in both transitions? I changed it below.
Try this small four-line change:
local \_21pass = 0 local \_21button -- (forward) declare it here so it is visible to the following code local tracker = display.newText( \_21pass, centerX, centerY, 40,40, "Gotham", 30 ) function confirm21( event ) \_21pass = 1 - \_21pass local buttonsound = audio.loadSound( "rightp.mp3" ) audio.play( buttonsound ) if \_21pass == 0 then print("0") tracker.text = \_21pass transition.to( \_21button, { alpha = 0, time = 500 } ) -- alpha 0 end if \_21pass == 1 then tracker.text = \_21pass print("1") transition.to( \_21button, { alpha = 1, time = 500 } ) -- alpha 1 end end \_21button = display.newImageRect( "okgreen@2x.png", 17.5, 17.5) -- define it here \_21button.x = centerX - 99 \_21button.y = centerY + 67 \_21button:addEventListener( "tap", confirm21 ) \_21button.alpha = 0 \_21button.isHitTestable = true
Hey, I put alpha = 1 on both transitions to see if would fade in regarding the _21pass value. The original code has 0 and 1 as yours. Sadly this didn’t work either , starting to think that maybe it’s a bug.
Does simply transitioning the _21button object from 0 to 1 after you create it work? If so, then it’s not a bug with transitions, it’s something else in your scope or logic handling.
Best regards,
Brent
Roaminggamer is (as always) correct: it is a simple scoping issue!
You are writing a function that does a transition on the button, but for Lua this button “does not exist yet” because it hasn’t been declared in the code yet.
Just declaring the image at the top of the code, before the tap handler, works perfectly:
local pass = 0 local button = display.newRect(100,100,100,100) function confirm( event ) pass = 1 - pass if pass == 0 then transition.to(button, {time = 500, alpha = 0.2}) print("0") end if pass == 1 then transition.to(button, {time = 500, alpha = 1}) print("1") end end button:addEventListener( "tap", confirm ) button.alpha = .5
Also, for your info: I didn’t catch the error because I always code my modules using one main table variable. Every variable declared in the code is an element of this table - for some reason this is a way to circumvent the order of declaration of variables.
As soon as I discovered the problem by attempts I read yours lol , either way I am too happy , thank you and every other friends that helped 
here it go in a final look ( working 100% ) :
local \_21pass = 0 local \_21button local \_21button = display.newImageRect( "okgreen@2x.png", 17.5, 17.5) \_21button.x = centerX - 99 \_21button.y = centerY + 67 \_21button.alpha = 0 \_21button.isHitTestable = true function confirm21( event ) \_21pass = 1 - \_21pass local buttonsound = audio.loadSound( "rightp.mp3" ) audio.play( buttonsound ) if \_21pass == 0 then print("0") transition.to( \_21button, { alpha = 0, time = 200 } ) elseif \_21pass == 1 then print("1") transition.to( \_21button, { alpha = 1, time = 200 } ) end end \_21button:addEventListener( "tap", confirm21 )
Alright! Big success!
Just a quick question, why the _21 prefix? Watch out with weird prefix because variables have some restrictions regarding naming (e.g. no numbers at the start).
I wanted to relate the variable to the number 21 to remember me what it was. Since 21pass wouldn’t work neither pass21 I added the underscore. If it had put just “pass” I probably wouldn’t remember this in the future. I have a weird way to remember things haha, anyways, thank you again friend, hopefully I won’t be finding problems with this code again 
Strange… 21pass doesn’t work, that’s normal because variable names can’t start with a number in Lua (among others). But pass21 should work perfectly.