Touch and Hold Event

I’ve tried multiple methods of trying to get things to move constantly when the screen is touched and held. Nothing is working for me. Can someone please help?

I’ve tried this and variations and it still has not worked:

local holding = false local function enterFrameListener() if holding then -- Holding button -- Code here -- Code here -- Code here else -- Not holding -- Code here -- Code here -- Code here end end local function touchHandler( event ) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) event.target.isFocus = true Runtime:addEventListener( "enterFrame", enterFrameListener ) holding = true elseif event.target.isFocus then if event.phase == "moved" then elseif event.phase == "ended" then holding = false Runtime:removeEventListener( "enterFrame", enterFrameListener ) display.getCurrentStage():setFocus( nil ) event.target.isFocus = false end end return true end

Any suggestions?

Hi,

Unless you are not showing the code in your example, there is nothing moving your object. Take a look at this sample code:

[lua]-- create object
local myObject = display.newRect( 0, 0, 100, 100 )
myObject:setFillColor( 255 )

– touch listener function
function myObject:touch( event )
  if event.phase == “began” then

    self.markX = self.x – store x location of object
    self.markY = self.y – store y location of object

  elseif event.phase == “moved” then

    local x = (event.x - event.xStart) + self.markX
    local y = (event.y - event.yStart) + self.markY

    self.x, self.y = x, y – move object based on calculations above
  end

  return true
end

– make ‘myObject’ listen for touch events
myObject:addEventListener( “touch”, myObject )[/lua]

Cheers.

Thanks, but I don’t think that’s what I need. I’m trying to make a game in which if you touch the left side of the screen you fly to the left, and if you touch the right side of the screen, you fly to the right (like the controls of Super Hexagon). The main character is stationary in the middle, and the background, stars, and other objects move.

That’s not really how cameras works in general.  

Typically the character moves and the game moves the rest of the world around the player to make it seem like the player is staying stationary.

I have made several examples for Corona Geek w/ cameras, one may be useful to you:

http://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/ICanMakeThat

Here is one example of a camera:

http://github.com/roaminggamer/CoronaGeek/blob/master/Hangouts/ICanMakeThat/ZigZagBoomClone/app/standalone/scripts/camera.lua

All ‘cameras’ depend on a group/layering system to achieve the mechanic.  The layer system for the game above is here:

http://github.com/roaminggamer/CoronaGeek/blob/master/Hangouts/ICanMakeThat/ZigZagBoomClone/app/standalone/scripts/layers.lua

In fact, the whole game (standalone version) is a fairly easy example to follow if you need to learn about layering and cameras.

Get the code here if you want it:

http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/ICanMakeThat/ZigZagBoomClone.zip

I understand that, but I’m not sure I’d know how to implement that, when the character is moving through infinite space constantly. Off-topic, but that doesn’t really help me with a touch and hold function.

I hate to double-post, but could someone please help?

I think everything you needed was in the links I gave, but this (kinda bad) example shows it all together:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/02/constantMover.zip

Not a great way to do this, but the fundamentals are all there.

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

Hi,

Unless you are not showing the code in your example, there is nothing moving your object. Take a look at this sample code:

[lua]-- create object
local myObject = display.newRect( 0, 0, 100, 100 )
myObject:setFillColor( 255 )

– touch listener function
function myObject:touch( event )
  if event.phase == “began” then

    self.markX = self.x – store x location of object
    self.markY = self.y – store y location of object

  elseif event.phase == “moved” then

    local x = (event.x - event.xStart) + self.markX
    local y = (event.y - event.yStart) + self.markY

    self.x, self.y = x, y – move object based on calculations above
  end

  return true
end

– make ‘myObject’ listen for touch events
myObject:addEventListener( “touch”, myObject )[/lua]

Cheers.

Thanks, but I don’t think that’s what I need. I’m trying to make a game in which if you touch the left side of the screen you fly to the left, and if you touch the right side of the screen, you fly to the right (like the controls of Super Hexagon). The main character is stationary in the middle, and the background, stars, and other objects move.

That’s not really how cameras works in general.  

Typically the character moves and the game moves the rest of the world around the player to make it seem like the player is staying stationary.

I have made several examples for Corona Geek w/ cameras, one may be useful to you:

http://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/ICanMakeThat

Here is one example of a camera:

http://github.com/roaminggamer/CoronaGeek/blob/master/Hangouts/ICanMakeThat/ZigZagBoomClone/app/standalone/scripts/camera.lua

All ‘cameras’ depend on a group/layering system to achieve the mechanic.  The layer system for the game above is here:

http://github.com/roaminggamer/CoronaGeek/blob/master/Hangouts/ICanMakeThat/ZigZagBoomClone/app/standalone/scripts/layers.lua

In fact, the whole game (standalone version) is a fairly easy example to follow if you need to learn about layering and cameras.

Get the code here if you want it:

http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/ICanMakeThat/ZigZagBoomClone.zip

I understand that, but I’m not sure I’d know how to implement that, when the character is moving through infinite space constantly. Off-topic, but that doesn’t really help me with a touch and hold function.

I hate to double-post, but could someone please help?

I think everything you needed was in the links I gave, but this (kinda bad) example shows it all together:

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/02/constantMover.zip

Not a great way to do this, but the fundamentals are all there.

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