Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Delay or Wait — Gideros Forum

Delay or Wait

xGuiltxGuilt Member
edited July 2013 in General questions
How do you add some kind of way for the system to wait before running the next line of code?

What i'm trying to do is, when the scene enters, the background would already be visible but will wait for let's say 2 or 3 seconds before showing some of the objects. Also i was planning on using the delay or wait to somehow prevent some function from running for 1 or 2 seconds...

Likes: code_monkey

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • OZAppsOZApps Guru
    edited July 2013
    This is something I asked Roberto around January 2012,
    and the response I got, this however does not work with GUI based nor does
    sleep(nn)
    I am also looking for the perfect solution that I am not yet able to get, the logical thing that I can think of is to construct a stack with instructions and execute them accordingly, which would dispatch a DONE event to go to the next instruction.

    The
    while do .. end
    loops can freeze/hang so they are no good, there is no
    DoEvents()
    like in Visual Basic

    If you do not mind, would you be able to help with some code block on how to have a loop that does not go to the next line until it finishes execution?
    --in pseudo code
    --
     
    theStatus = false
    callAFunction()
    while theStatus==false do
     --wait for theStatus to turn to true
    end
    --now we can proceed
    callNextFunction()

    Have a look at "Programming in Lua":

    http://www.lua.org/pil/9.4.html

    -- Roberto
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • MellsMells Guru
    edited July 2013
    @xGuilt how relevant to your question is the following ?
    local function displayObjects()
          -- Do something here
    end
    local timer = Timer.delayedCall(3000, displayObjects)

    Likes: xGuilt

    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
    +1 -1 (+1 / -0 )Share on Facebook
  • petecpetec Member
    edited July 2013
    @xGuilt I do what @Mells suggests when I need to delay something. The only thing you need to do is make sure that you stop the timer when you leave the scene as otherwise, if you leave the scene before the timer has run its time, the timer seems to keep running and the action can happen in the next scene when time is up - at least, that's what I've found.
  • xGuiltxGuilt Member
    edited July 2013
    @xGuilt how relevant to your question is the following ?
    local function displayObjects()
          -- Do something here
    end
    local timer = Timer.delayedCall(3000, displayObjects)
    I've tried it but somehow, what's inside the function is not happening? Did i do something something wrong?

    EDIT: nevermind, found out the problem. btw does this mean i have to create multiple timer if i want several timed event?

  • ar2rsawseenar2rsawseen Maintainer
    @xGuilt if they are in separate time intervals and can't be synced then yes. But Timer class is quite flexible, you can dispatch single Timed events:
    local function displayObjects()
          -- Do something here
    end
    local timer = Timer.delayedCall(3000, displayObjects)
    You can make Timer run indefinitely (until you stop it):
    local function displayObjects()
          -- Do something here
    end
    local timer = Timer.new(3000)
    timer:addEventListener(Event.TIMER, displayObjects)
    timer:start()
    Or you can run Timer specified amount of times
    local function displayObjects()
          -- Do something here
    end
    local timer = Timer.new(3000, 10) --running timer 10 times with 3000 milisecond delays
    timer:addEventListener(Event.TIMER, displayObjects) --is dispatched on every timer tick
    timer:addEventListener(Event.TIMER_COMPLETE, displayObjects) --is dispatched when all 10 times are up
    timer:start()

    Likes: rolfpancake

    +1 -1 (+1 / -0 )Share on Facebook
  • xGuiltxGuilt Member
    edited July 2013
    @Mells i almost forgot to thank you for the code, works like a charm. :D

    @ar2rsawseen oh, but it seems like single timed events works perfectly since i'm only using it for a brief pause before a certain scene starts so it appears as i won't be using as much timer as i thought i would.

    since i'm in the topic of delay or waiting, i've recently encountered a new problem (i'm a problematic guy, sorry for that) So yeah, i have this block of code which is a button that when clicked, it shows a popup that has 2 buttons, yes and no.
    local A = TextButton.new(conf.smallFont, "Wing", "Wing", Bitmap.new(Texture.new("images/answer_up.png", conf.textureFilter)), Bitmap.new(Texture.new("images/answer_down.png", conf.textureFilter)))
     
       A:addEventListener("click", function()
       self.foregroundLayer:addChild(layer)
       yes:addEventListener("click", accept(stage, "a"))	
       no:addEventListener("click", decline)
       layer:show()
       end)
    I got this part cleared already but my problem is that the "yes" button is connected to a function
    	local function accept(x, choice)
    		if x == "4" then
    			if choice == "a" then
    			stage = "5b"
    			self.foregroundLayer:removeChild(menu)
    			print(scene)
    			elseif choice == "b" then
    			stage = "5a"
    			self.foregroundLayer:removeChild(menu)
    			elseif choice == "c" then
    			stage = "5b"
    			self.foregroundLayer:removeChild(menu)
    			elseif choice == "d" then
    			stage = "5b"
    			self.foregroundLayer:removeChild(menu)
    			end
    		end
     
    		return accept
    	end
    What i had in mind is that the user after choosing from a choice, he will be asked for confirmation of his choice by asking yes or no. Once he clicked yes, the function will get the current stage and the choice of the user then run based on the parameter passed. There is no problem with the parameter, the problem is that the event fires even without clicking the yes button. It runs the moment i click the A button. Any idea as to what is wrong?

    EDIT: It seems like this problem only occurs either with functions that takes parameters or if statements. I'm guessing it's the first one.
  • ar2rsawseenar2rsawseen Maintainer
    @xGuilt yes you can't pass any data to even handler like that
    yes:addEventListener("click", accept(stage, "a"))
    But you can do it like that:
    yes:addEventListener("click", accept, {stage, "a"})	
    local function accept(data)
        print(data[1]) --stage
        print(data[2]) --"a"
    end

    Likes: xGuilt

    +1 -1 (+1 / -0 )Share on Facebook
  • xGuiltxGuilt Member
    @ar2rsawseen tried it out and it worked nicely. Only problem is that the 'accept' function takes the parameters, the stage and "a" or whatever value it is even though the yes button hasn't been clicked yet. So if i click the button which adds the event listeners to both yes and no buttons. The yes button already gets the parameters and even if i click the no button, the yes would still have passed the params and run the function it's tied to which causes double or even different instance of the function when i finally click yes. Was wondering is it possible that the param would only be passed if i click yes button?
Sign In or Register to comment.