touch event help needed

Hey,

I’m having a problem detecting if event has occurred, I’ve been trying to find a solution for a couple of days but google doesn’t seem to help so I will post here.

Basically what I’m trying to do is when user touches object1 it turns green (this one works) and then he can touch object2 and it will turn green too. Now, the problem comes if user doesn’t touch object1, in that case the second object (object2 in this case) should turn red on touch and I have no idea how to do so with separate functions…

for example;

function touchObject1()

object1:setFillColor(0, 128, 0)

end

object1:addEventListener( “touch”, touchObject )

function touchObject2()

if touchObject1 == true then

object2:setFillColor(0, 128, 0)

else

object2:setFillColor(0, 255, 0)

end

end

object2:addEventListener( “touch”, touchObject2 )


This first function works fine as you can see, but second one, the one with IF statement inside doesn’t work for whatever reason. I know it may be easier to put everything in the same function but it makes making the rest of the game 100 times… so if anyone knows how to solve this it’d be much appreciated.

Thanks!

Hopefully I didn’t misunderstand your question, but in any case I think this example will help you:

FULL CODE:  https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/11/sequentialSelection.zip

https://www.youtube.com/watch?v=urNZVKmfpaQ&feature=youtu.be

-- ============================================================= io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- ============================================================= local cx = display.contentCenterX local cy = display.contentCenterY local buttons = {} local function onTap( self ) -- Get number of this object local myNum = self.num -- Check if order of selection has been violated local violatedOrder = false for i = myNum-1, 1, -1 do local button = buttons[i] if( not button.selected ) then violatedOrder = true end end -- Handle violated or not violated if( violatedOrder ) then for i = 1, myNum do local button = buttons[i] if( button.tap ) then buttons.tap = nil button:removeEventListener("tap") end button:setFillColor(1,0,0) button.selected = false end else local button = buttons[myNum] buttons.tap = nil button:removeEventListener("tap") button:setFillColor(0,1,0) button.selected = true end return true end local function prepExample() for i = 1, #buttons do if( buttons[i].tap ) then buttons[i]:removeEventListener("tap") end buttons[i].num = i buttons[i].selected = false buttons[i]:setFillColor( 1,1,1 ) buttons[i].tap = onTap buttons[i]:addEventListener("tap") end end -- Button 1 local button = display.newImageRect( "1.png", 100, 100 ) button.x = cx - 200 button.y = cy buttons[#buttons+1] = button -- Button 2 local button = display.newImageRect( "2.png", 100, 100 ) button.x = cx button.y = cy buttons[#buttons+1] = button -- Button 3 local button = display.newImageRect( "3.png", 100, 100 ) button.x = cx + 200 button.y = cy buttons[#buttons+1] = button prepExample() --- To simplify 'reseting' example... display.newText("Press Escape To Reset Example", cx, cy + 200) local function onKey( event ) if(event.keyName == "escape" and event.phase == "up" ) then prepExample() end end Runtime:addEventListener("key", onKey )

Thanks for the reply!

Yeah this code might do the work, but is it possible to get it done the way I did in the example, with only few lines of code using “IF” statement like: IF this event happened then do this, else do that… asking because I’m new to this so just by looking at the code you’ve posted it looks like it’s gonna be complicated to edit the code so it suits my app.

Anyways, thanks again for the help if nothing I will try to somehow understand your code and hopefully make it work the way I had in mind! :slight_smile:

Unfortunately that’s the best, and often only way to learn. Even as an experienced developer, you’ll be stuck with a problem and find something on stackoverflow that gets you half way there, maybe it’s not quite what you need but it’s in the right direction. It’s up to you then to take the ball and run with it.

The basic answer is to set a flag at the top of your file.

[lua]

local object1WasTouched = false

[/lua]

Set this to true within function touchObject1.

Test whether it is true within function touchObject2. The reason your attempt didn’t work is this line:

[lua]

if touchObject1 == true

[/lua]

Here you are asking 'Hey, is the function reference ‘touchObject1’ equal to true?

And the answer is, ‘TouchObject1 is a reference to a function that exists, so yes, it that sense it’s true. But that’s the case whether or not I’ve ever been run before. I’m telling you I exist, not whether or not I’ve ever been touched. So I will never be false.’

Sorry,  This is the simplest and most robust way I could think to do it.  

I’ve added it and it works good, just changed thing or two to suit my objects better. I have one more question though. My objects spawn with timer so not all of them are spawned as soon as the app starts but second or two after, depends on the object.

Now, if I just set them to spawn that way, the function will only work on the first object as it spawns immediately, but won’t work on others so I decided to spawn all the objects outside of the screen and then added timer so they show up later and it works that way, but I’m not sure if it’s gonna overload the app later and make it crash because of too many objects spawned?

As for now, it works fine because there are only 3 objects, but I will probably add more and I hope it won’t make the app laggy or something…

And once again, thanks a lot, you saved me 1000 hours!

Hopefully I didn’t misunderstand your question, but in any case I think this example will help you:

FULL CODE:  https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/11/sequentialSelection.zip

https://www.youtube.com/watch?v=urNZVKmfpaQ&feature=youtu.be

-- ============================================================= io.output():setvbuf("no") display.setStatusBar(display.HiddenStatusBar) -- ============================================================= local cx = display.contentCenterX local cy = display.contentCenterY local buttons = {} local function onTap( self ) -- Get number of this object local myNum = self.num -- Check if order of selection has been violated local violatedOrder = false for i = myNum-1, 1, -1 do local button = buttons[i] if( not button.selected ) then violatedOrder = true end end -- Handle violated or not violated if( violatedOrder ) then for i = 1, myNum do local button = buttons[i] if( button.tap ) then buttons.tap = nil button:removeEventListener("tap") end button:setFillColor(1,0,0) button.selected = false end else local button = buttons[myNum] buttons.tap = nil button:removeEventListener("tap") button:setFillColor(0,1,0) button.selected = true end return true end local function prepExample() for i = 1, #buttons do if( buttons[i].tap ) then buttons[i]:removeEventListener("tap") end buttons[i].num = i buttons[i].selected = false buttons[i]:setFillColor( 1,1,1 ) buttons[i].tap = onTap buttons[i]:addEventListener("tap") end end -- Button 1 local button = display.newImageRect( "1.png", 100, 100 ) button.x = cx - 200 button.y = cy buttons[#buttons+1] = button -- Button 2 local button = display.newImageRect( "2.png", 100, 100 ) button.x = cx button.y = cy buttons[#buttons+1] = button -- Button 3 local button = display.newImageRect( "3.png", 100, 100 ) button.x = cx + 200 button.y = cy buttons[#buttons+1] = button prepExample() --- To simplify 'reseting' example... display.newText("Press Escape To Reset Example", cx, cy + 200) local function onKey( event ) if(event.keyName == "escape" and event.phase == "up" ) then prepExample() end end Runtime:addEventListener("key", onKey )

Thanks for the reply!

Yeah this code might do the work, but is it possible to get it done the way I did in the example, with only few lines of code using “IF” statement like: IF this event happened then do this, else do that… asking because I’m new to this so just by looking at the code you’ve posted it looks like it’s gonna be complicated to edit the code so it suits my app.

Anyways, thanks again for the help if nothing I will try to somehow understand your code and hopefully make it work the way I had in mind! :slight_smile:

Unfortunately that’s the best, and often only way to learn. Even as an experienced developer, you’ll be stuck with a problem and find something on stackoverflow that gets you half way there, maybe it’s not quite what you need but it’s in the right direction. It’s up to you then to take the ball and run with it.

The basic answer is to set a flag at the top of your file.

[lua]

local object1WasTouched = false

[/lua]

Set this to true within function touchObject1.

Test whether it is true within function touchObject2. The reason your attempt didn’t work is this line:

[lua]

if touchObject1 == true

[/lua]

Here you are asking 'Hey, is the function reference ‘touchObject1’ equal to true?

And the answer is, ‘TouchObject1 is a reference to a function that exists, so yes, it that sense it’s true. But that’s the case whether or not I’ve ever been run before. I’m telling you I exist, not whether or not I’ve ever been touched. So I will never be false.’

Sorry,  This is the simplest and most robust way I could think to do it.  

I’ve added it and it works good, just changed thing or two to suit my objects better. I have one more question though. My objects spawn with timer so not all of them are spawned as soon as the app starts but second or two after, depends on the object.

Now, if I just set them to spawn that way, the function will only work on the first object as it spawns immediately, but won’t work on others so I decided to spawn all the objects outside of the screen and then added timer so they show up later and it works that way, but I’m not sure if it’s gonna overload the app later and make it crash because of too many objects spawned?

As for now, it works fine because there are only 3 objects, but I will probably add more and I hope it won’t make the app laggy or something…

And once again, thanks a lot, you saved me 1000 hours!