Touch sequence

Hope somebody can help me. I have images inside a table and want to setup a touch sequence the user must follow. So touch image1 first, then image2, then image3. These images have a fixed x/y position (not randomly spawned). And then I want to check if the touched image is correct, followed by if the next touched image is correct and so on. Something like touch alphabet from a to z or number from 1 to 5, just a little different (number of images varies per level (say sometimes 4 other times 3), and x/y postion of images changes per level). Not sure how to do this (event.target == ?) and I’ve been kinda stuck with my code.

Any hints/ code help is greatly appreciated. Thanks.

Hi @travels_w,

This should be relatively easy. You can assign custom properties to objects as needed, so I would suggest you begin by assigning the “order” number which the object is supposed to be touched in. So if you have 4 items, and you know the proper order, assign each on a property like “touchOrder”:

[lua]

objectA.touchOrder = 1

objectB.touchOrder = 2

– etc.

[/lua]

Next, keep a variable counter like “nextToTouch” with a starting value of 1. If the user touches the first object, increment it by 1, so that the code expects the object with “touchOrder=2” will be the next item which should be touched. And then same for going onward to 3, 4, etc. Finally, when the user exceeds the total number, you know that they’ve successfully touched all objects in the proper order.

[lua]

local nextToTouch = 1

local totalItems = 4  – Set to the number of objects (3, 4, 10, whatever)

– Touch handler function

local function onObjectTouch( event )

   if ( event.phase == “began” ) then

      if ( event.target.touchOrder == nextToTouch ) then

         nextToTouch = nextToTouch + 1

         if ( nextToTouch > totalItems ) then

            print( “SUCCESS!!!” )

            – Take whatever action necessary

         end

      end

   end

end

[/lua]

Hope this helps,

Brent

Hi Brent,

Thanks for your reply!

The number of objects (totalItems) varies per level. How would I take that into account?

It depends on how you’re handling levels. Are you using a scene/level management library like Corona’s built-in Composer? Or are you using just one main.lua file and “refreshing” the same core module on a level-based scheme?

Brent

I’m using Composer in my code. The image objects are inside a table, which is inside a levels table.

Thanks,Travels_w

OK, well you could either store a common/shared variable among all scenes, and set its value for each level, or you could just keep that variable local to each scene, resetting its value each time the scene is entered. Remember that, by default, Composer scenes are not completely discarded/removed from internal Lua memory, so you will not always get the “create()” function executed (only the first time it’s created). For things which you want to set/reset/execute each time the user enters the scene, it’s best to do that inside the “show>‘will’” or “show>‘did’” scene event function/phase.

Hope this helps,

Brent

Hi Brent,

Thanks for the info. I’ve been trying to implement it, but I’m getting an error (I’m still a beginner…).

I wanted store a common/shared variable among all scenes, by storing the totalItems variable inside the levels table (for example in level 1 totalItems = 4). And then calling it: myLevel.totalItems = totalItems

When I compare if nextToTouch > totalItems I get this error: attempt to compare nil with number.

Not sure how to do it the right way. Hope you can help me a little more.

Thanks, Travels_w

Truly, I’m a big fan of the Composer set/get variable functions. They provide a simple way to set a variable (or a table of variables even) by “name/tag” common to all scenes, and access that variable in any scene where Composer is require()'d and used.

https://docs.coronalabs.com/api/library/composer/setVariable.html

https://docs.coronalabs.com/api/library/composer/getVariable.html

Hope this helps,

Brent

P.S. - Yeah, you could just attach your own variable to the Composer table and accomplish the same basic concept, but I like the convenience of these functions and the ability to easily search my code later for instances when I use them.

Hi Brent.

Sorry for the late reply. Thanks for the links. I’ll look into it.

Thanks, Travels_w

Hi @travels_w,

This should be relatively easy. You can assign custom properties to objects as needed, so I would suggest you begin by assigning the “order” number which the object is supposed to be touched in. So if you have 4 items, and you know the proper order, assign each on a property like “touchOrder”:

[lua]

objectA.touchOrder = 1

objectB.touchOrder = 2

– etc.

[/lua]

Next, keep a variable counter like “nextToTouch” with a starting value of 1. If the user touches the first object, increment it by 1, so that the code expects the object with “touchOrder=2” will be the next item which should be touched. And then same for going onward to 3, 4, etc. Finally, when the user exceeds the total number, you know that they’ve successfully touched all objects in the proper order.

[lua]

local nextToTouch = 1

local totalItems = 4  – Set to the number of objects (3, 4, 10, whatever)

– Touch handler function

local function onObjectTouch( event )

   if ( event.phase == “began” ) then

      if ( event.target.touchOrder == nextToTouch ) then

         nextToTouch = nextToTouch + 1

         if ( nextToTouch > totalItems ) then

            print( “SUCCESS!!!” )

            – Take whatever action necessary

         end

      end

   end

end

[/lua]

Hope this helps,

Brent

Hi Brent,

Thanks for your reply!

The number of objects (totalItems) varies per level. How would I take that into account?

It depends on how you’re handling levels. Are you using a scene/level management library like Corona’s built-in Composer? Or are you using just one main.lua file and “refreshing” the same core module on a level-based scheme?

Brent

I’m using Composer in my code. The image objects are inside a table, which is inside a levels table.

Thanks,Travels_w

OK, well you could either store a common/shared variable among all scenes, and set its value for each level, or you could just keep that variable local to each scene, resetting its value each time the scene is entered. Remember that, by default, Composer scenes are not completely discarded/removed from internal Lua memory, so you will not always get the “create()” function executed (only the first time it’s created). For things which you want to set/reset/execute each time the user enters the scene, it’s best to do that inside the “show>‘will’” or “show>‘did’” scene event function/phase.

Hope this helps,

Brent

Hi Brent,

Thanks for the info. I’ve been trying to implement it, but I’m getting an error (I’m still a beginner…).

I wanted store a common/shared variable among all scenes, by storing the totalItems variable inside the levels table (for example in level 1 totalItems = 4). And then calling it: myLevel.totalItems = totalItems

When I compare if nextToTouch > totalItems I get this error: attempt to compare nil with number.

Not sure how to do it the right way. Hope you can help me a little more.

Thanks, Travels_w

Truly, I’m a big fan of the Composer set/get variable functions. They provide a simple way to set a variable (or a table of variables even) by “name/tag” common to all scenes, and access that variable in any scene where Composer is require()'d and used.

https://docs.coronalabs.com/api/library/composer/setVariable.html

https://docs.coronalabs.com/api/library/composer/getVariable.html

Hope this helps,

Brent

P.S. - Yeah, you could just attach your own variable to the Composer table and accomplish the same basic concept, but I like the convenience of these functions and the ability to easily search my code later for instances when I use them.

Hi Brent.

Sorry for the late reply. Thanks for the links. I’ll look into it.

Thanks, Travels_w