Question about touch event

Hi, I have a question about touch events. I’ll just post an example of what i’m trying to do, to make it easy to comprehend. I need to launch a “touch” event whenever the player presses the background, even if it presses is 1, 2,3 times or keeps the finger on the screen, i’m having problems trying to make it so if he keeps the finger there, without moving it, the event is still running:

local background = display.newRect(0,0,360,480) local cooldown = false local function CDok() cooldown = false end local function spawn(event) if cooldown = false then cooldown = true timer.performWithDelay(200,CDok,1) arrow = display.newImage("arrow.png") end end background:addEventListener("touch",spawn)

The code includes physics and other stuff, but this is the part im stuck at. Obviously, when i press the background and leave the mouse there, nothing happens. 

Anyone know any idea to make it happen?

Thanks in advance.

You’re missing the event.phase portion of the event listenet model. Check the docs to see which would be appropriate in your case.

docs.coronalabs.com/api/event/touch/phase.html

Hi, thanks for the answer.

I’ve used the event.phases but still, it’s not a problem about phases, it’s a problem about the touch event not being called because the finger is still pressed and there is “nothing” happening in the game. 

Event’s do not continuously generate.  You will get one event on press, one on release and one for each move.  You have to code a flag like isPressed = true and then use some other method like a Runtime:addEventListener(“enterFrame”, someFunction) handler that would process continuously while the flag is true.

Rob

Alright, thanks for the answer Rob, i’ll work on that.

edit: i’ve tested it like this :

local background = display.newRect(0,0,360,480) local cooldown = false local isPressed = false local function CDok() cooldown = false end local function spawn(event) if event.phase == "true" or event.phase == "moved" then isPressed = true end if event.phase == "ended" then isPressed = false end if cooldown = false then cooldown = true timer.performWithDelay(200,CDok,1) arrow = display.newImage("arrow.png") print(event.y) end end background:addEventListener("touch",spawn) local function checkPress() if isPressed == true then spawn() end end Runtime:addEventListener("enterFrame",checkPress)

And i have an error when i let the finger pressed because event.y is nil… 

I’ve used the print example to resume what i have, but i basically have physics interaction (it’s an arrow and it has a force whenever i press the screen)

edit2: it looks like whenever i call spawn() outside of the spawn function, event.x/y are nil because the touch isnt registered. Is there a way to save event.y and event.y from the last touch event and use it until “ended” moded happens? I tried to store event.y and event.x with a normal variable, but obviously when i call spawn from checkpress() it saves “nil”.

Ok, it’s working fine now, i had to do something like this:

local eventx0 = 0 local eventy0 = 0 local function spawn(event) if event.phase == "began" or event.phase == "moved" then eventy0 = event.y eventx0 = event.x end

And its working as intended.

Thanks for the help.

I would not have used the same function for the touch handler and the function used by the enterFrame.  Realistically your touch handler needs to just set the isPressed flag.  I wouldn’t have any spawn code in that function.

Rob

So how could you make it, to work like i did before? is that code going to mess my game in the future if i use it too much? 

Thanks

local background = display.newRect(0,0,360,480) local cooldown = false local isPressed = false local function CDok()     cooldown = false end local function spawn(event)     if event.phase == "true" or event.phase == "moved" then         isPressed = true     end     if event.phase == "ended" then         isPressed = false     end     return true end   local function spawn()     if cooldown = false then         cooldown = true         timer.performWithDelay(200,CDok,1)         arrow = display.newImage("arrow.png")         print(event.y)     end   background:addEventListener("touch",spawnHandler) local function checkPress()     if isPressed == true then         spawn()     end end Runtime:addEventListener("enterFrame",checkPress)

or something like that.

Oh i got it now, tested it and it worked just fine, looks pretty cool.

So basically whats the difference between the 2 methods? does yours take less memory or something?

Thanks a lot for the help.

Your method executes the coolDown block not only every frame (30 or 60 times per second) but on every touch down, release of touch and move event that gets generated.   It’s also more code organization.  Generally you want functions to do what they need to do and in this case, the enterFrame listener just needs to manage the actions while the touch is going on.  In your way, you have a function that is both doing the action in addition to handling the touch event when logically in they are different.  This might be better stated, that it’s not logical to have enterFrame call a touch handler of that makes any sense.

You’re missing the event.phase portion of the event listenet model. Check the docs to see which would be appropriate in your case.

docs.coronalabs.com/api/event/touch/phase.html

Hi, thanks for the answer.

I’ve used the event.phases but still, it’s not a problem about phases, it’s a problem about the touch event not being called because the finger is still pressed and there is “nothing” happening in the game. 

Event’s do not continuously generate.  You will get one event on press, one on release and one for each move.  You have to code a flag like isPressed = true and then use some other method like a Runtime:addEventListener(“enterFrame”, someFunction) handler that would process continuously while the flag is true.

Rob

Alright, thanks for the answer Rob, i’ll work on that.

edit: i’ve tested it like this :

local background = display.newRect(0,0,360,480) local cooldown = false local isPressed = false local function CDok() cooldown = false end local function spawn(event) if event.phase == "true" or event.phase == "moved" then isPressed = true end if event.phase == "ended" then isPressed = false end if cooldown = false then cooldown = true timer.performWithDelay(200,CDok,1) arrow = display.newImage("arrow.png") print(event.y) end end background:addEventListener("touch",spawn) local function checkPress() if isPressed == true then spawn() end end Runtime:addEventListener("enterFrame",checkPress)

And i have an error when i let the finger pressed because event.y is nil… 

I’ve used the print example to resume what i have, but i basically have physics interaction (it’s an arrow and it has a force whenever i press the screen)

edit2: it looks like whenever i call spawn() outside of the spawn function, event.x/y are nil because the touch isnt registered. Is there a way to save event.y and event.y from the last touch event and use it until “ended” moded happens? I tried to store event.y and event.x with a normal variable, but obviously when i call spawn from checkpress() it saves “nil”.

Ok, it’s working fine now, i had to do something like this:

local eventx0 = 0 local eventy0 = 0 local function spawn(event) if event.phase == "began" or event.phase == "moved" then eventy0 = event.y eventx0 = event.x end

And its working as intended.

Thanks for the help.

I would not have used the same function for the touch handler and the function used by the enterFrame.  Realistically your touch handler needs to just set the isPressed flag.  I wouldn’t have any spawn code in that function.

Rob

So how could you make it, to work like i did before? is that code going to mess my game in the future if i use it too much? 

Thanks

local background = display.newRect(0,0,360,480) local cooldown = false local isPressed = false local function CDok()     cooldown = false end local function spawn(event)     if event.phase == "true" or event.phase == "moved" then         isPressed = true     end     if event.phase == "ended" then         isPressed = false     end     return true end   local function spawn()     if cooldown = false then         cooldown = true         timer.performWithDelay(200,CDok,1)         arrow = display.newImage("arrow.png")         print(event.y)     end   background:addEventListener("touch",spawnHandler) local function checkPress()     if isPressed == true then         spawn()     end end Runtime:addEventListener("enterFrame",checkPress)

or something like that.

Oh i got it now, tested it and it worked just fine, looks pretty cool.

So basically whats the difference between the 2 methods? does yours take less memory or something?

Thanks a lot for the help.