Coroutine issue

Star crunch did a great tutorial on coroutines

http://coronalabs.com/blog/2015/02/10/tutorial-using-coroutines-in-corona/

I want to continue here a discussion I started in the blog.

I was hoping coroutine might help give the illusion of speeding things up in response to a buttonclick.

In my code, I put up a dialogbox then load a large graphic.
The dialogbox doesn’t show ordinarily until the file is loaded. So no illusion here.

I tried firing part 1 of my script on ‘began’ phase (dialog box) and part 2 on ‘ended’ phase. (load graphic)
Seems not to work.

If the coroutine did as I expected, the dialogbox would show instantly and then the file would load while user is distracted.

Also tried firing twice within one function, but that doesn’t work either.
Any thoughts  on how to achieve this effect?

Hi.

Is this a regular image, versus say an image sheet? If it’s the former, you could break the image up into more light-weight images, then load them in one by one. (To some extent, this could be automated simulator-side.) In the coroutine, then, you could load the images in a loop, yielding between loads.

If you need to be able to do stuff on the image as a unit you could achieve that via a group or capturing it afterward.

If it’s a sheet or a mask I’m stumped.  :frowning:

It’s possible I’ll be doing that (breaking up image) anyway. Just not sure how I can trigger it other than via a button-press.

I presume coroutine be called from a function that’s called from the button, but i’d really like to initiate on ‘press’ and continue via release.

The main concern is that you’ll be at the mercy of how quickly the image (or images)  can be loaded. If it takes longer than an average click, doing just two calls won’t get you much.

In pseudo-code, I’m thinking you’d want something along the lines of:

local InUse CreateButton(function(event) if event.phase == "release" and not InUse then InUse = true CoroPerformWithDelay(function() ShowDialog() for i = 1, #SubImages do local data = SubImages[i] local image = display.newImage(ImagesLayer, data.name, data.x, data.y) -- Fade in image or make invisible until all are ready, etc. TryToYield() -- Use normal yield or one of the alternatives from the blog post end AllDone() -- Tell your app that you're ready InUse = false end) end end)

Hi.

Is this a regular image, versus say an image sheet? If it’s the former, you could break the image up into more light-weight images, then load them in one by one. (To some extent, this could be automated simulator-side.) In the coroutine, then, you could load the images in a loop, yielding between loads.

If you need to be able to do stuff on the image as a unit you could achieve that via a group or capturing it afterward.

If it’s a sheet or a mask I’m stumped.  :frowning:

It’s possible I’ll be doing that (breaking up image) anyway. Just not sure how I can trigger it other than via a button-press.

I presume coroutine be called from a function that’s called from the button, but i’d really like to initiate on ‘press’ and continue via release.

The main concern is that you’ll be at the mercy of how quickly the image (or images)  can be loaded. If it takes longer than an average click, doing just two calls won’t get you much.

In pseudo-code, I’m thinking you’d want something along the lines of:

local InUse CreateButton(function(event) if event.phase == "release" and not InUse then InUse = true CoroPerformWithDelay(function() ShowDialog() for i = 1, #SubImages do local data = SubImages[i] local image = display.newImage(ImagesLayer, data.name, data.x, data.y) -- Fade in image or make invisible until all are ready, etc. TryToYield() -- Use normal yield or one of the alternatives from the blog post end AllDone() -- Tell your app that you're ready InUse = false end) end end)