Pause execution

Hi everyone, I have a project in which I have a png file that I want to display and then show a button using the native.showalert function. However, I want to pause execution while the png file is on the screen. The whole png image is a listener and touching anywhere on the image closes it. My problem is that while all these items work, I cannot pause execution while the png is on the screen. I have tried timers and even a while … do loop which only ends when a flag within the listener gets changed. None of my attepnts work - I’m probably really stupid.

here’s an example:

function display\_test()  
display\_destination\_card() -- call the function that displays the card  
local alert = native.showAlert( "Message Header", "Well done.", { "Continue" }) -- message that should not be returned to until the showcard image has been touched  
end  
  
function display\_destination\_card()  
  
dcardpressed = "N" -- flag  
showcard = display.newImageRect("carddisplay.png",400,250)  
showcard.x = \_w/2  
showcard.y = \_h/2  
showcard.alpha = 0  
transition.to( showcard, { time=500, delay = 500,alpha=1 }) -- fade the card in  
showcard:addEventListener ("touch", presscardcontinue) -- set up the listener  
  
tmr = timer.performWithDelay(200, pause\_game,-1) -- infinite timer  
  
-- I do not want to return from this function until the showcard image has been touched!!   
end  
  
function pause\_game()  
while dcardpressed ~= "Y" do -- loop  
 if dcardpressed == "Y" then -- change flag when the event listener has been touched  
 timer.cancel(tmr); -- cancel timer  
 break  
 end  
  
end  
  
end  
function presscardcontinue(event)  
  
if event.phase == "ended" then  
  
 showcard:removeEventListener ("touch", presscardcontinue)  
 showcard:removeSelf()  
 dcardpressed = "Y" -- set flag  
 return  
end  
  
end  
  

What I want to happen is that the display_destination_card() function gets called and then there is no return to the next line (local alert…) until the showcard image has been touched. At the moment, the “local alert” message shows up immediately (I don’t want it to show at all) and the showcard image half shows up behind
the local alert message (presumably halfway through the delay procedure.

Not sure if I’ve explained this correctly - please ask if this doesn’t make sense!

It would be great if there was some function called something like “wait()”.

Please help as its almost the last thing I have to do before my first game is complete!!

Thanks

Adrian [import]uid: 83824 topic_id: 20017 reply_id: 320017[/import]

  1. First, really gotta recommend using the tags < code > and < / code > (without spaces) to encapsulate your code on the forums. Makes it a lot easier for folks to read and help you out. :slight_smile:

  2. Based specifically on the code you’ve got there, presscardcontinue() needs to be before you add the event Listener or it will never work anyway. Although I don’t know when you call the functions or in what order…

  3. This is probably something you’ve already tried, but if I wanted to prevent something from being touched for 200ms, I might do this:

[code] – flag
local canTouch = false

– touch function
local touch = function(event)
if event.phase == “ended” and canTouch == true then – only works if true
dosomething()
end
end

– flagswitch function, just flops canTouch between true and false
local toggleFlag = function()
if canTouch == false then
canTouch = true
else
canTouch = false
end
end

timer.performWithDelay(200, toggleFlag, 1)[/code]

Does that help at all? Or am I just retreading old ground? [import]uid: 41884 topic_id: 20017 reply_id: 78109[/import]

Hi Richard, thanks for the reply. Firstly apologies on the difficult to read code. I wondered how people got to make their code look so clear!! have gone back and used the HTML tags (first bit of HTML I have ever done!!) and it is much much clearer!!

I’ll experiment with your ideas - my problem with your solution of 200 milliseconds is that I don’t want any more execution at all until the image is touched. I suppose I could could get ridiculous and use an hour but seems daft!! It just seems strange that I cannot prevent the code from returning from the display_destination_card().

Moving the location of presscardcontinue didn’t make any difference. I have to say that the order in which I put the functions in my code above was the actual order in my program and the press continue WAS reached (I put in terminal print statements as traces).
Thank you so much for your reply and I’ll let you know how I get on.

Cheers

Adrian [import]uid: 83824 topic_id: 20017 reply_id: 78118[/import]

No, I wouldn’t use anything crazy like a super timer length. (And I really have no idea how presscard works coming later, but anyway…)

Wait, so what you’re saying is that after executing display_destination_card() you don’t want the next line to execute until the user touches the image? That should be relatively easy to solve - just throw everything into an event function.

[code]function display_test()
local displaySomething = display_destination_card()

local onTouch = function(event)
if event.phase == “ended” then
local alert = native.showAlert( “Message Header”, “Well done.”, { “Continue” })
– if you wanted EVERYTHING after line 2 from your example to execute you could just pile it all in here, I suppose

displaySomething:addEventListener(“touch”, onTouch)
end --/if
end --/onTouch()

end --/ display_test()
[/code]

The only thing not covered here is that it assumes the image you load via display_destination_card() is local and that you end the function with return image.

(So, bear with my tutorialness…)

[code]local dostuff = function()
local myImage = display.newImage(“awesome.png”) – I can’t touch this outside of the function because it’s local!!
return myImage – now I can.
end

local theImage = dostuff() – theImage is now treated as your image[/code]

So with the first example you would never proceed from display_destination_card() to the next line unless the user touches the image. They could touch it right away though, which is where you might want to use flag timers to set a length of time before the touch is accepted. [import]uid: 41884 topic_id: 20017 reply_id: 78125[/import]

Hi Richard,

set up a standalone project with just a small piece of code in it based on your reply. I feel quite embarrassed because I am sure I am missing something incredibly simple!

  
local \_w = display.contentWidth  
local \_h = display.contentHeight  
  
function display\_destination\_card()  
print "DDC1"   
local showcard = display.newImageRect("testcard.png",400,250)  
showcard.x = \_w/2  
showcard.y = \_h/2  
showcard.alpha = 0  
  
print "DDC2"   
  
transition.to( showcard, { time=500, delay = 50,alpha=1 })  
  
print "DDC3"   
  
return showcard  
end  
  
  
function display\_test()  
local displaySomething = display\_destination\_card()  
 print "dt1"  
local onTouch = function(event)  
 print "dt2"  
if event.phase == "ended" then  
 print "dt3"  
 local alert = native.showAlert( "Message Header", "Well done.", { "Continue" })  
-- if you wanted EVERYTHING after line 2 from your example to execute you could just pile it all in here, I suppose  
 print "dt4"  
displaySomething:addEventListener("touch", onTouch)  
 print "dt5"  
end --/if   
end --/onTouch()  
   
end --/ display\_test()  
  
display\_test()  
  

The image displays ok but when I press on the image there is no reaction at all. As you can see I have put in “print” statements to see where we get to. The terminal receives DDC1, DDC2, DDC3 and dt1 ok but stops at dt1. I presume this is where the listener should be waiting for a response from the user, but there is no response when the image is clicked.

Should I be doing something more in the display_destination_card function?

Thanks again for your help

Adrian

[import]uid: 83824 topic_id: 20017 reply_id: 78296[/import]

No need to apologize. I was posting as much as anyone six months ago, and I’m still making dumb threads I solve myself from time to time. (And yes! Starting up a standalone project is the best way to solve this stuff. Bravo!)

Okay, so let me look at this…

display\_destination\_card() -- Loads the image as invisible and then fades into view (0.5s) display\_test() -- 1. executes display\_destination\_card() -- 2. You define a function but it's never called.

Basically you are adding the event listener in the wrong place. Within display_test() you define onTouch(). Within onTouch() is your if statement. Within the if statement is your addEventListener! Which means it will never be added because it’s stuck inside the listener.

Easy solution: move addEventListener to line 39 - after onTouch but before the end of display_test()

:slight_smile: [import]uid: 41884 topic_id: 20017 reply_id: 78347[/import]

Richard, you are a saint!. That works in my standalone (although haven’t quite worked out why yet!) and now I have to work out how to put it into my project. Will work out how its working because I hate having code which I don’t really understand inside out.

Once again thank you so much for your help - I’d never have worked it out on my own!

Adrian [import]uid: 83824 topic_id: 20017 reply_id: 78483[/import]

Sorry to bother you again but its not quite working how I had hoped. The background processing keeps carrying on. To illustrate this I have put an extra line into the standalone project:

  
local \_w = display.contentWidth  
local \_h = display.contentHeight  
  
function display\_destination\_card()  
print "DDC1"   
local showcard = display.newImageRect("testcard.png",400,250)  
showcard.x = \_w/2  
showcard.y = \_h/2  
showcard.alpha = 0  
  
print "DDC2"   
transition.to( showcard, { time=500, delay = 50,alpha=1 })  
print "DDC3"   
return showcard  
end  
  
  
function display\_test()  
local displaySomething = display\_destination\_card()  
 print "dt1"  
local onTouch = function(event)  
 print "dt2"  
if event.phase == "ended" then  
 print "dt3"  
local alert = native.showAlert( "Message Header", "Well done.", { "Continue" })  
-- if you wanted EVERYTHING after line 2 from your example to execute you could just pile it all in here, I suppose  
 print "dt4"  
  
 print "dt5"  
end --/if   
end --/onTouch()  
 displaySomething:addEventListener("touch", onTouch)  
end --/ display\_test()  
  
display\_test()  
  
print ("should not get here") -- do not want it to reach here until the testcard.png has been touched  

What I want is the display_test() to be invoked and then no return until the image is pressed. As you can see the “should not get here” message appears in the terminal.

I have loads of things that happen after the showing of the card so the ideal would be purely to wait until the card is pressed before continuing.

Thanks

Adrian [import]uid: 83824 topic_id: 20017 reply_id: 78487[/import]

Hey Adrian,

Well, the code is working as it should. (Although I admit it took my hazy mind some time to see that much.)

  1. display_test() calls the card.
  2. the card fades in.
  3. An event listener is added

That’s pretty much all the code needs to just continue on it’s merry way. It can’t wait for you to tap or touch, else there would be no way to have a moving game. (My fault there, I probably blabbed on about in-order execution, but it’s doing just that anyway)

If you don’t want something to process until the card has been pressed you need to move that functionality all within the listener (after that if event.phase == “ended” line). It doesn’t necessarily all need to be in the same lua file though, it can be functions within functions or whatnot. :slight_smile: [import]uid: 41884 topic_id: 20017 reply_id: 78653[/import]

Ok - I think this is beginning to click with me!! I am actually writing a simple board game, so there is no physics involved. For long periods of time, there is nothing happening - just waiting for the user to press a button. I have been so used to pure sequential programming in languages where it easy to put in some kind of “wait” statement that will stop at that point until some trigger sets it off again.

With your suggestion of moving to the onTouch function, it suddenly seems obvious!! My big problem is that I have acres of code after this point (within a players turn, so before the next stop), that could branch in several directions. All this code would have to go into the eventlistener area. I will attempt it but it will mean quite a rewrite. I had planned my game in a traditional programming sequence - Corona seems to work differently in that much of the code can carry on in background (as you said to allow moving items to carry on).

Obviously I am going to have to think and plan differently for my next projects - this isn’t necessarily a bad thing as I think I have become a bit set in my ways!!!

Many many thanks for your help - at last the fog has lifted - but I will still probably submit posts that ask the simplest of questions!!

As an aside I miss the old continuous print outs in which you could easily trace your code. Might have to invest in one if they still exist!!
Thanks again

Adrian
[import]uid: 83824 topic_id: 20017 reply_id: 78665[/import]

Yeah, the thing to remember is that is executing in order, it’s just that you can’t expect the code to stop and wait for a button press.

The best way to handle the nesting is by creating functions elsewhere and then calling them in the desired order. (Helps keeps things understandable.)

[code]
– Include other lua files you need
local ui = require (“ui”)
local otherfunctions = require (“otherfunctions”)

local myFunction = function()
print(“My function!”)
end

local myListener = function(event)
if event.phase == “began” then
myFunction()
ui.dosomething()
otherfunctions.bakeaCookie()
end
end[/code]

This way, it’s very clear to you while writing the code (and debugging!) what you’re doing. If you pile all of the code inside of the listener it can be really tough to tell what is going on after awhile.

If you’re having trouble seperating functions, remember that you can always pre-declare. Good luck! :slight_smile: [import]uid: 41884 topic_id: 20017 reply_id: 78727[/import]