Touch Function Problem

I have this code on my app for move images :

 – create object
local camisola1 = display.newImage(“Camisola-F1.tif”,45,60)
 – create object
local camisola2 = display.newImage(“Camisola-F2.tif”,45,110)
– create object
local camisola3 = display.newImage(“Camisola-F3.tif”,45,160)
– create object
local camisola4 = display.newImage(“Camisola-F4.tif”,45,210)
 – create object
local camisola5 = display.newImage(“Camisola-F5.tif”,45,260)

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)

But when the images touch, run away from each other…

Can someone help me pls ?

Hello David,

unfortunetly I am not sure what you want to achieve.

Do you want to move an image according to yor touch position, or do you want it to move on it’s own after a touch?

Additional information would be necessary to understand the questian and suggest a solution.

Perhaps try this:

-- create object local camisola1 = display.newImage("Camisola-F1.tif",45,60) -- create object local camisola2 = display.newImage("Camisola-F2.tif",45,110) -- create object local camisola3 = display.newImage("Camisola-F3.tif",45,160) -- create object local camisola4 = display.newImage("Camisola-F4.tif",45,210) -- create object local camisola5 = display.newImage("Camisola-F5.tif",45,260) local function camisolaTouch(event) local touchedObject = event.target if event.phase == "began" then display.getCurrentStage():setFocus( touchedObject ) 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 elseif event.phase == "cancelled" then display.getCurrentStage():setFocus( nil ) end return true end camisola3:addEventListener( "touch" , camisolaTouch) camisola4:addEventListener( "touch" , camisolaTouch) camisola5:addEventListener( "touch" , camisolaTouch) camisola2:addEventListener( "touch" , camisolaTouch) camisola1:addEventListener( "touch" , camisolaTouch)

I want to move the images on top of each other , but this function is not working. My project is a tactical framework for coaches, with so he could move the images , which are the players , where desired .

This is my app : ExBkK7.png

JonPM with this function can only drag one image at a time I wish I could drag more

We support multi-touch. However devices may limit how many touches can be active at once. We support a maximum of 10 touches. The Kindle Fire for instance will only let you have two touches at once.

Rob

You need to be able to move more than 1 image at the exact same time?  Or can the user just move each player one by one

One by one, the coach moves one image at a time

So what is the problem with my code exactly?  You said it moves one image at a time

yes but when the images touch they flee each other and sometimes gives error RoVYQi.png

Help Pls :c

Can you post your current code?

Rob

local composer = require( “composer” )
local scene = composer.newScene()
local function buttonHit(event)
    composer.gotoScene( event.target.destination )
    return true
end

– include Corona’s “physics” library
local physics = require “physics”
physics.start(); physics.pause()


– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

function scene:create( event )

    local sceneGroup = self.view

    – create a grey rectangle as the backdrop
    local background = display.newImage( “Campo1F.tif”, true )
 background.x = display.contentWidth / 2
 background.y = display.contentHeight / 2

    
    
    – all display objects must be inserted into group
    sceneGroup:insert( background )

end

function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase
    
    
    if phase == “will” then
    
–Voltar
    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)
    
    
 – Inserir a Camisola
local camisola1 = display.newImageRect(“Camisola-F1.tif”,35,60)
camisola1.x = 45
camisola1.y = 50
sceneGroup:insert(camisola1)
    
 – Inserir a Camisola
local camisola2 = display.newImageRect(“Camisola-F2.tif”,35,60)
camisola2.x=45
camisola2.y=110
sceneGroup:insert(camisola2)
    
 – Inserir a Camisola
local camisola3 = display.newImageRect(“Camisola-F3.tif”,35,60)
camisola3.x=45
camisola3.y=160
sceneGroup:insert(camisola3)
    
 – Inserir a Camisola
local camisola4 = display.newImageRect(“Camisola-F4.tif”,35,60)
camisola4.x=45
camisola4.y=210
sceneGroup:insert(camisola4)
    
 – Inserir a Camisola
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

–Botao back Android - Runtime:addEventListener( “key”, onKeyEvent )
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 )


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.