I’m having an issue with a touch event where variables are being defined on began, but it crashes on ended saying that the variables defined in began are nil. This only happens on start up. Anyone care to explain?
Are you doing something like this:
function touch( event ) if( event.phase == "began" ) then local firstX = event.x -- Wrong. This will fall out of scope and not exist later. elseif( event.phase == "ended" ) then print( event.x - firstX ) -- 'firstX' does not exist end end
If the answer is, “Yes”, then you’re not following proper scoping rules. Try something like this instead:
local firstX function touch( event ) if( event.phase == "began" ) then firstX = event.x -- Better elseif( event.phase == "ended" ) then print( event.x - firstX ) -- 'firstX' is still visible end end
If the answer is, “No”, then post your event listener code and please format using code formatting like I’ve done.
This is what I’m working with. I do initialize the variables outside the began phase. \
local startPosX local startPosY local function swipeTouch(e) -- print("swipe",e.phase) if e.phase == "began" then startPosX = e.x startPosY = e.y elseif e.phase == "moved" then if math.abs(e.x-e.xStart) + math.abs(e.y-e.yStart) \> 40 then display.getCurrentStage():setFocus( e.target ) end -- print("START POS "..startPosX) elseif e.phase == "ended" then dragDistanceX = e.x - startPosX dragDistanceY = e.y - startPosY -- print("DRAG DISTANCE "..dragDistanceX) if dragDistanceY \< -40 then gotoFunc("up") elseif dragDistanceY \> 40 then gotoFunc("down") end if dragDistanceX \< -50 then gotoFunc("right") elseif dragDistanceX \> 50 then gotoFunc("left") end end end
I’ve had issues with touching something else (or at least holding down the mouse in the simulator) and then dragging into another display object. And then I end up doing something like
elseif e.x then if e.phase == "moved" then -- stuff else -- ended / cancelled stuff e.x = nil end end
I’ve never been quite sure whether this was a bug (it might allow drag-and-drop, for instance), especially since it’s such an easy thing to cause and yet I never see it mentioned, but it’s certainly awkward.
Are you coming from a title screen or some kind of overlay that you remove when a touch begins? Maybe it’s picking that up? (You also probably want to be returning true from your touch listeners.)
-- Looks like you're trying to detect a swipe(s) -- -- One way to do that would be this: -- local swipeDist = 40 local function swipeDetector( self, e ) -- print("swipe",e.phase) if e.phase == "began" then self.isFocus = true display.getCurrentStage():setFocus( self, e.id ) elseif( self.isFocus) then if( self.\_swipeDetected == nil ) then local dx = e.x - e.xStart local dy = e.y - e.yStart if( dy \< -swipeDist ) then self.\_swipeDetected = "up" elseif( dy \> swipeDist ) then self.\_swipeDetected = "down" elseif( dx \< -swipeDist ) then self.\_swipeDetected = "left" elseif( dx \> swipeDist ) then self.\_swipeDetected = "right" end end if( e.phase == "ended" ) then self.isFocus = false display.getCurrentStage():setFocus( self, nil ) if(self.\_swipeDetected == nil) then -- Skip return true elseif( self.\_swipeDetected == "up" ) then print("Do up action here") self.y = self.y - swipeDist elseif( self.\_swipeDetected == "down" ) then print("Do down action here") self.y = self.y + swipeDist elseif( self.\_swipeDetected == "left" ) then print("Do left action here") self.x = self.x - swipeDist elseif( self.\_swipeDetected == "right" ) then print("Do right action here") self.x = self.x + swipeDist end self.\_swipeDetected = nil end end return true end local tmp = display.newRect( display.contentCenterX, display.contentCenterY, 40, 40 ) tmp.touch = swipeDetector tmp:addEventListener( "touch" )
Sorry I totally forgot about this post. I just implemented your suggestion and it works, but now there is a different problem. I’m using this as touch modal for a lot of my scenes. This is suppose to allow players to swipe between scenes., but on the scenes there are also buttons. So setting the stage/focus to this modal object makes it so I can’t touch buttons on the scene anymore.
Do you have any suggestions on how I could get around that?
Are you doing something like this:
function touch( event ) if( event.phase == "began" ) then local firstX = event.x -- Wrong. This will fall out of scope and not exist later. elseif( event.phase == "ended" ) then print( event.x - firstX ) -- 'firstX' does not exist end end
If the answer is, “Yes”, then you’re not following proper scoping rules. Try something like this instead:
local firstX function touch( event ) if( event.phase == "began" ) then firstX = event.x -- Better elseif( event.phase == "ended" ) then print( event.x - firstX ) -- 'firstX' is still visible end end
If the answer is, “No”, then post your event listener code and please format using code formatting like I’ve done.
This is what I’m working with. I do initialize the variables outside the began phase. \
local startPosX local startPosY local function swipeTouch(e) -- print("swipe",e.phase) if e.phase == "began" then startPosX = e.x startPosY = e.y elseif e.phase == "moved" then if math.abs(e.x-e.xStart) + math.abs(e.y-e.yStart) \> 40 then display.getCurrentStage():setFocus( e.target ) end -- print("START POS "..startPosX) elseif e.phase == "ended" then dragDistanceX = e.x - startPosX dragDistanceY = e.y - startPosY -- print("DRAG DISTANCE "..dragDistanceX) if dragDistanceY \< -40 then gotoFunc("up") elseif dragDistanceY \> 40 then gotoFunc("down") end if dragDistanceX \< -50 then gotoFunc("right") elseif dragDistanceX \> 50 then gotoFunc("left") end end end
I’ve had issues with touching something else (or at least holding down the mouse in the simulator) and then dragging into another display object. And then I end up doing something like
elseif e.x then if e.phase == "moved" then -- stuff else -- ended / cancelled stuff e.x = nil end end
I’ve never been quite sure whether this was a bug (it might allow drag-and-drop, for instance), especially since it’s such an easy thing to cause and yet I never see it mentioned, but it’s certainly awkward.
Are you coming from a title screen or some kind of overlay that you remove when a touch begins? Maybe it’s picking that up? (You also probably want to be returning true from your touch listeners.)
-- Looks like you're trying to detect a swipe(s) -- -- One way to do that would be this: -- local swipeDist = 40 local function swipeDetector( self, e ) -- print("swipe",e.phase) if e.phase == "began" then self.isFocus = true display.getCurrentStage():setFocus( self, e.id ) elseif( self.isFocus) then if( self.\_swipeDetected == nil ) then local dx = e.x - e.xStart local dy = e.y - e.yStart if( dy \< -swipeDist ) then self.\_swipeDetected = "up" elseif( dy \> swipeDist ) then self.\_swipeDetected = "down" elseif( dx \< -swipeDist ) then self.\_swipeDetected = "left" elseif( dx \> swipeDist ) then self.\_swipeDetected = "right" end end if( e.phase == "ended" ) then self.isFocus = false display.getCurrentStage():setFocus( self, nil ) if(self.\_swipeDetected == nil) then -- Skip return true elseif( self.\_swipeDetected == "up" ) then print("Do up action here") self.y = self.y - swipeDist elseif( self.\_swipeDetected == "down" ) then print("Do down action here") self.y = self.y + swipeDist elseif( self.\_swipeDetected == "left" ) then print("Do left action here") self.x = self.x - swipeDist elseif( self.\_swipeDetected == "right" ) then print("Do right action here") self.x = self.x + swipeDist end self.\_swipeDetected = nil end end return true end local tmp = display.newRect( display.contentCenterX, display.contentCenterY, 40, 40 ) tmp.touch = swipeDetector tmp:addEventListener( "touch" )
Sorry I totally forgot about this post. I just implemented your suggestion and it works, but now there is a different problem. I’m using this as touch modal for a lot of my scenes. This is suppose to allow players to swipe between scenes., but on the scenes there are also buttons. So setting the stage/focus to this modal object makes it so I can’t touch buttons on the scene anymore.
Do you have any suggestions on how I could get around that?