Button to control boolean - (Im new)

Hi all, i am new the corona and im dont know why i am having such a hard time with what i think should be simple…

i am trying to use a button to control boolean such as change “gameIsActive” to true

this is what i have done and its not working…

thanks for the help in advance

function touchBox:tap( event )
if ( gameIsActive == false ) then
gameIsActive = true
end
end

touchBox:addEventListener( “tap”, touchBox )

[import]uid: 39705 topic_id: 29187 reply_id: 329187[/import]

Hi apbrooks,

I have modified your example a little bit and it works - just run the code from the example and check terminal - you should see how gameIsActive changes when you press the button (circle in the example)

[lua]local someButton
local gameIsActive = false

someButton = display.newCircle( 200, 200, 50)

local function touchBox( event )

if ( gameIsActive == false ) then

gameIsActive = true

else
gameIsActive = false

end

– printing BOOL state in Terminal
print(string.format("–> gameIsActive = %s",tostring(gameIsActive)))
end

someButton.tap = touchBox
someButton:addEventListener( “tap”, someButton )[/lua]

Hope this helps.

Cheers,
Patryk
@PatskyPat [import]uid: 51816 topic_id: 29187 reply_id: 117379[/import]

Thanks, it looks like it is running in the terminal (shows it moving between true and false) but it is not starting my animation on the screen. I think i need to tweek some other items! thanks for your help!!

this is my entire code to make a scrolling background

display.setStatusBar(display.HiddenStatusBar) – hides the status bar

– loads the physics engine into the game

local physics = require “physics”
physics.start()
physics.setGravity(0,9.8)

– gets the screen size of the game
local screenW = display.contentWidth
local screenH = display.contentHeight
local backgroundSpeed = 100

– game is nto active until it is tapped
local gameIsActive = false

– this object is to get the background the scroll
– create 2 background images to ensure they scroll

local BGspeed = 10

local background1
background1 = display.newImage(“images/mountainBG.png”)
background1.xReference = -480
background1.x = 0

local background2
background2 = display.newImage(“images/mountainBG.png”)
background2.xReference = -480
background2.x = 960
–background2.alpha = 0.5

– this function is what controls the background position and creates the movement
function updateBackgrounds()
if (gameIsActive == true) then
background1.x = background1.x - (BGspeed/5)
if(background1.x <= -960) then
background1.x = 960
end
background2.x = background2.x - (BGspeed/5)
if(background2.x <= -960) then
background2.x = 960
end
end
end

–this is how we call the update function, make sure that this line comes after the
–actual function or it will not be able to find it
–timer.performWithDelay(how often it will run in milliseconds, function to call, how many times to call(-1 means forever))
if gameIsActive == true then
timer.performWithDelay(1, updateBackgrounds, -1)
end

local touchBox = display.newRect(0, 0, 960, 640)
touchBox.alpha = 0

local someButton
local gameIsActive = false

someButton = display.newCircle( 200, 200, 50)

local function touchBox( event )

if ( gameIsActive == false ) then

gameIsActive = true

else
gameIsActive = false

end

– printing BOOL state in Terminal
print(string.format("–> gameIsActive = %s",tostring(gameIsActive)))
end

someButton.tap = touchBox
someButton:addEventListener( “tap”, someButton ) [import]uid: 39705 topic_id: 29187 reply_id: 117382[/import]

ok - first advice - when you post any code in lua - use syntax highlighting so before code write < lua >
and after code < /lua > without spaces :slight_smile: so your code will look nice on the page :slight_smile:

To fix your code you need to remove condition checking before timer so :

Delete these 2 lines
[lua]if gameIsActive == true then – DELETE THIS LINE
timer.performWithDelay(1, updateBackgrounds, -1)
end --DELETE THIS LINE AS WELL[/lua]

Why? You need to understand your runtime. So in your example your game:

  • starts
  • gameIsActive gets FALSE state
  • before you run timer.performWithDelay you check if your gameIsActive - and because it is false - it does not kick off
  • game ends this block of code
  • touch events are separate things - just at this steps nothing makes your code to run from the beginning and check a condition before timer.performWithDelay

What we have done here is we told timer.performWithDelay to run updateBackrounds all the time - so timer runs updateBackround, then inside that function gameIsActive condition is checked every time timer.performWithDelay invokes this function.

The last line you need to DELETE is:
[lua]local touchBox = display.newRect(0, 0, 960, 640)
touchBox.alpha = 0

local someButton
local gameIsActive = false –
someButton = display.newCircle( 200, 200, 50)[/lua]
Quite often a runtime of a game looks like:

[lua]local function GameLoop()
if gameIsActive then – if gameIsActive is the same as if (gameIsActive == true)

– YOUR CODE THAT RUNS EVERY FRAME
end
end

Runtime:addEventListener(“enterFrame”,GameLoop) --This one kicks off GameLoop function every frame if possible[/lua]

I hope it is clear now :wink:

Let me know if you have any questions

Cheers,
Patryk
@PatskyPat
[import]uid: 51816 topic_id: 29187 reply_id: 117395[/import]

Thanks, i will make sure i post the code correctly next time!

i am going to give this a shot shortly!!

i was told the corona community was great and i really appreciate your help!

Cheers!! [import]uid: 39705 topic_id: 29187 reply_id: 117422[/import]

You can also try

isGameActive = not isGameActive

I think this should work without tests like if then.

Hope this help.

Mo [import]uid: 100814 topic_id: 29187 reply_id: 117430[/import]

Thanks Patryk, it seems to be working now and I see where my code was wrong because it was skipping the "timer.performWithDelay, but I dont seem to be able to switch the “someButton” for the touchBox. What i wanted to do was create an invisible box that on the tap even starts the animation. I tried the following code and it doesnt work

[lua]local touchBox = display.newRect(0, 0, 960, 640)
touchBox.alpha = 0

local function touchBox( event )

if ( gameIsActive == false ) then

gameIsActive = true

else
gameIsActive = false

end

– printing BOOL state in Terminal
print(string.format("–> gameIsActive = %s",tostring(gameIsActive)))
end

touchBox.tap = touchBox
touchBox:addEventListener( “tap”, touchBox )
[lua] [import]uid: 39705 topic_id: 29187 reply_id: 117443[/import]

It is not working because touchBox.alpha = 0 in your example so the box is invisible, therefore it does not respond to any touch events.
What you need to do is to ‘move’ box further to the back, behind your backgrounds. To achieve this you need to write this code

[lua]local touchBox = display.newRect(0, 0, 960, 640)
touchBox.alpha = 1

– if you wish you can set up color to black
touchBox:setFillColor( 0, 0, 0 )[/lua]

BEFORE this code

[lua]local background1
background1 = display.newImage(“images/mountainBG.png”)[/lua]

There is nothing in the front so the first object that will receive any respond to any touch event will be your touchBox.
Your backgrounds do not have any touch listeners.
You need to think about graphic objects like layers painted one on top of another in order from background to foreground.
When you touch a screen - your finger goes from foreground to the background and invokes touch event only on these objects which want to listen to it (hence touch listener).

Cheers,
Patryk [import]uid: 51816 topic_id: 29187 reply_id: 117444[/import]

One more thing I forgot to write in my last example

You need to change name of the function - you cannot have touchBox as an object and touchBox as a function in your example

[lua]local function touchBoxPressed( event )

if ( gameIsActive == false ) then

gameIsActive = true

else
gameIsActive = false
end

– printing BOOL state in Terminal
print(string.format("–> gameIsActive = %s",tostring(gameIsActive)))
end

touchBox.tap = touchBoxPressed
touchBox:addEventListener( “tap”, touchBox )[/lua]

Now it works.

Cheers,
Patryk [import]uid: 51816 topic_id: 29187 reply_id: 117447[/import]

That is perfect!!! that is exactly what I was trying to do! I didn’t know if an objects alpha = 0 it cannot receive touches.

I am coming over to Corona from gamesalad and so i am still pretty green when it comes to this.

Cheers, I appreciate all the help!

Andrew [import]uid: 39705 topic_id: 29187 reply_id: 117451[/import]