Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Is there a way to make the main script stop until an if statement is met then continue with the script?
Started by judewilson2024 Dec 02 2019 06:16 PM

1 reply to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

judewilson2024

[GLOBAL: userInfoPane.html]
judewilson2024
  • Observer

  • 2 posts
  • Corona SDK

Hello, I am making a pen clicking simulator that will have updates to the pens when you click it x amount of times. In the upgrade one part I am having trouble. It skips over the if statement at the bottom. is there a way to make the script stop until the if statement is met? Or maybe an else function to send it back to a certain line so it will eventually hit the if statement? Thanks in advance y'all are great.

-- Upgrade1.0 function
local function upgrade1(event)
	if(event.phase == "ended") then 
        audio.play(click)
        penclicklevel = penclicklevel - 1
        penbodyrect = "one/pen.png"
    end
end

-- Upgrade1.0 button
upgrades1 = widget.newButton {
	width = 45,
	height = 35,
	defaultFile = "images/upgrade1.0.png",
    onEvent = upgrade1,
    alpha = 0,
}
upgrades1.x = 4000
upgrades1.y = 200 

if (penclicklevel > 1) then
    upgrades1.x = 200
    penclicklevel = penclicklevel * 2
end


  • judewilson2024 likes this

[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,867 posts
  • Corona SDK

Corona is event-based, there is no 'main loop' built in, you have to create one via timers or listeners.

 

What this means is that your code that checks the penclicklevel runs once, unless you tell it to run again. 

 

However, in this case, the simplest solution is to put that code inside a function, and call that function whenever penclicklevel is updated:

 
local upgrades1 = widget.newButton {
    width = 45,
    height = 35,
    defaultFile = "images/upgrade1.0.png",
onEvent = upgrade1,
alpha = 0,
}
 
upgrades1.x = 4000
upgrades1.y = 200
 
local checkPenClickLevel = function ()
 
  if (penclicklevel > 1) then
    upgrades1.x = 200
    penclicklevel = penclicklevel * 2
  end
 
end   -- this code does not run until called with checkPenClickLevel()
 
 
local function upgrade1(event)
if(event.phase == "ended") then 
        audio.play(click)
        penclicklevel = penclicklevel - 1
        penbodyrect = "one/pen.png"
        checkPenClickLevel()
    end
end
 

  • roaminggamer likes this


[topic_controls]
[/topic_controls]