Touch Function Problem

First, please post your code using code formatting. The easiest way is to click the blue <> button in the editing tool bar.

Next, the error points to line 97, which can’t generate that error. That tells me the code you posted is different than what’s generating the error. Since these don’t align correctly I’m going to have to guess where the real error is and it’s probably in this block of code:

local function camisolaTouch(event) local touchedObject = event.target if event.phase == "began" then touchedObject.previousX = touchedObject.x touchedObject.previousY = touchedObject.y elseif event.phase == "moved" then touchedObject.x = (event.x - event.xStart) + touchedObject.previousX --\<---- this is the line failing touchedObject.y = (event.y - event.yStart) + touchedObject.previousY end return true end

touchedObject.x = (event.x - event.xStart) + touchedObject.previousX

Why is touchedObject at this point not have a previousX value?

Are you by any chance dragging an object over top of another? I feel that you are likely getting a touch event to an object that you didn’t actually touch or else your “began” would trigger which initializes your .previousX. It’s possible that the object being dragged over might get a “moved” touch event which case .previousX and .previousY would not be set.

Rob

local composer = require( "composer" ) local scene = composer.newScene() local function buttonHit(event) composer.gotoScene( event.target.destination ) return true end local physics = require "physics" physics.start(); physics.pause() local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 function scene:create( event ) local sceneGroup = self.view local background = display.newImage( "Campo1F.tif", true ) background.x = display.contentWidth / 2 background.y = display.contentHeight / 2 sceneGroup:insert( background ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then local backBtn = display.newImageRect("botaoV.tif",50,25) backBtn.x= 500 backBtn.y= 296 backBtn.destination = "menu" backBtn:addEventListener("tap", buttonHit) composer.removeScene(true) sceneGroup:insert(backBtn) local camisola1 = display.newImageRect("Camisola-F1.tif",35,60) camisola1.x = 45 camisola1.y = 50 sceneGroup:insert(camisola1) local camisola2 = display.newImageRect("Camisola-F2.tif",35,60) camisola2.x=45 camisola2.y=110 sceneGroup:insert(camisola2) local camisola3 = display.newImageRect("Camisola-F3.tif",35,60) camisola3.x=45 camisola3.y=160 sceneGroup:insert(camisola3) local camisola4 = display.newImageRect("Camisola-F4.tif",35,60) camisola4.x=45 camisola4.y=210 sceneGroup:insert(camisola4) local camisola5 = display.newImageRect("Camisola-F5.tif",35,60) camisola5.x=45 camisola5.y=260 sceneGroup:insert(camisola5) local function camisolaTouch(event) local touchedObject = event.target if event.phase == "began" then touchedObject.previousX = touchedObject.x touchedObject.previousY = touchedObject.y elseif event.phase == "moved" then touchedObject.x = (event.x - event.xStart) + touchedObject.previousX touchedObject.y = (event.y - event.yStart) + touchedObject.previousY end return true end camisola3:addEventListener( "touch" , camisolaTouch) camisola4:addEventListener( "touch" , camisolaTouch) camisola5:addEventListener( "touch" , camisolaTouch) camisola2:addEventListener( "touch" , camisolaTouch) camisola1:addEventListener( "touch" , camisolaTouch) elseif phase == "did" then physics.start() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then physics.stop() elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

I want to move the images to where I want, moving up one another , freely …
But when they approach or give error then they change positions alone …

You could put in an if statement in something like this:

local function camisolaTouch(event) local touchedObject = event.target if event.phase == "began" then touchedObject.previousX = touchedObject.x touchedObject.previousY = touchedObject.y elseif event.phase == "moved" then if touchedObject.previousX == nil then touchedObject.previousX = 0 end if touchedObject.previousY == nil then touchedObject.previousY = 0 end touchedObject.x = (event.x - event.xStart) + touchedObject.previousX touchedObject.y = (event.y - event.yStart) + touchedObject.previousY end return true end

Thank you no longer gives this error !!

But unfortunately when images touch change alone position :frowning:

I apply the same function to all objects (camisola) may be that?

The code I added to your function serves a soul purpose of keeping the error from crashing your app. You still need to figure out why you’re getting moved events without ever getting a began event. I can’t see what you’re building. I don’t know what you’re expecting to happen.

You could make a video of what’s happening and post that.

Rob

this is what happens:

 https://www.youtube.com/watch?v=SZtTxbyEuys

Try adding these lines inside your touch handler’s “began” phase:

 if "began" == event.phase then -- Make target the top-most object local parent = touchedObject.parent parent:insert( touchedObject ) display.getCurrentStage():setFocus( touchedObject ) -- Spurious events can be sent to the target, e.g. the user presses -- elsewhere on the screen and then moves the finger over the target. -- To prevent this, we add this flag. Only when it's true will "move" -- events be sent to the target. touchedObject.isFocus = true

Then you should handle the “ended” phase too:

 elseif "ended" == event.phase or "cancelled" == event.phase then display.getCurrentStage():setFocus( nil ) touchedObject.isFocus = false

See if that helps.

YES!!! WORK’S!!!

Thank you !!!

Great news!

Hello again …
I wanted to help me if possible to limit the screen so that the images do not they left the same

If you’re using Physics, then you create invisible walls with display.newRect() just off screen along the edges you want to block, make them static physics bodies and it will keep things on screen.

If you’re not using physics, in whatever code you use to move an object, check the .x value of the object and if it’s off screen stop the movement.

elseif event.phase == "moved" then if touchedObject.previousX == nil then touchedObject.previousX = 0 end if touchedObject.previousY == nil then touchedObject.previousY = 0 end touchedObject.x = (event.x - event.xStart) + touchedObject.previousX touchedObject.y = (event.y - event.yStart) + touchedObject.previousY if touchedObject.x \< (touchedObject.width \* 0.5) then touchedObject.x = touchedObject.width \* 0.5 end if touchedObject.x \> ( display.actualContentWidth - touchedObject.width \* 0.5 ) then touchedObject.x = display.actualContentWidth - touchedObject.width \* 0.5 end if touchedObject.y \< (touchedObject.height \* 0.5) then touchedObject.y = touchedObject.heigth \* 0.5 end if touchedObject.y \> (display.actualContentHeight - touchedObject.heigth \* 0.5) then touchedObject.y = display.actualContentHeight - touchedObject.heigth \* 0.5 end

or something like that.

Rob

I have this for background

local background = display.newImageRect( "Campo1F.tif", 512,300 ) background.x = display.contentWidth / 2 background.y = display.contentHeight / 2

Work’s like newrect () ?

I don’t think we support .tif files, but yes, it’s similar to newRect, except that you don’t give it a x and y as a parameter. It works like you have it.

All my images are in .tif and work xD
I have this code and they you continue to exit the screen

Help pls
I now after moving the images you intended to make a line that begins at the initial position where the user moved …