Dragging and dropping objects onto container

I have images like scrabble tiles which I can drag and drop in my container, but I’m duplicating a lot of code. I also want to know how to be able to drop a tile in any number of containers instead of one specific container. Here is a snippet of the code. Can someone please help, because I want to avoid duplicating code and I need to work out how to drop a tile on multiple containers

[lua]display.setStatusBar( display.HiddenStatusBar )

local posX, posY = 100, 200
local sizeX, sizeY = 40, 40
local posX1, posY1 = 150, 200
local posx, posy = 10, 50

local container = display.newRoundedRect( posX, posY, sizeX, sizeY, 3 )
container:setFillColor( 0,0,255 )
container.strokeWidth = 3
container:setStrokeColor(100, 100, 100)

local container2 = display.newRoundedRect( posX1, posY1, sizeX, sizeY, 3 )
container:setFillColor( 0,0,255 )
container2.strokeWidth = 3
container2:setStrokeColor(100, 100, 100)

local myObject = display.newImage(“letters/B.png” , 150, 10)
local myObject2 = display.newImage(“letters/a.png” , 10, 10)
local myObject3 = display.newImage(“letters/C.png” , 200, 10)

– 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

if (((x >= ((sizeX/2) + posX - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/2) * sizeY))))) then
container:setFillColor( 0,0,255 )
else
container:setFillColor( 0,0,255 )
end

elseif event.phase == “ended” then

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

– main condition: I calculated 3 areas to atract the object to the target container, 2 areas that atract it when it’s 1/3 in the target and 1 area that atract it when it’s 1/4 in the target
if (((x >= ((sizeX/2) + posX - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/2) * sizeY))))) then
self.x, self.y = posX + (sizeX/2), posY + (sizeY/2);
end

end

return true
end


--------------------------test---------------------------------------------

– touch listener function
function myObject2: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
if (((x >= ((sizeX/2) + posX1 - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/2) * sizeY))))) then
container2:setFillColor( 0,0,255 )
else
container2:setFillColor( 0,0,255 )
end

elseif event.phase == “ended” then

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

– main condition: I calculated 3 areas to atract the object to the target container, 2 areas that atract it when it’s 1/3 in the target and 1 area that atract it when it’s 1/4 in the target
if (((x >= ((sizeX/2) + posX1 - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/2) * sizeY))))) then
self.x, self.y = posX1 + (sizeX/2), posY1 + (sizeY/2);
end

end

return true
end


function myObject3: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
if (((x >= ((sizeX/2) + posX1 - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/2) * sizeY))))) then
container2:setFillColor( 0,0,255 )
else
container2:setFillColor( 0,0,255 )
end

elseif event.phase == “ended” then

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

– main condition: I calculated 3 areas to atract the object to the target container, 2 areas that atract it when it’s 1/3 in the target and 1 area that atract it when it’s 1/4 in the target
if (((x >= ((sizeX/2) + posX1 - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX1 - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY1 - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX1 + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY1 + ((1/2) * sizeY))))) then
self.x, self.y = posX1 + (sizeX/2), posY1 + (sizeY/2);

end

end

return true
end

myObject:addEventListener( “touch”, myObject )
myObject3:addEventListener( “touch”, myObject3 )
myObject2:addEventListener( “touch”, myObject2) [import]uid: 208811 topic_id: 34508 reply_id: 334508[/import]

I think you need to provide a diagram of what it is you want to achieve here. [import]uid: 8271 topic_id: 34508 reply_id: 137214[/import]

Thanks for replying, I think I might have jumped the gun and posting my problem before having a really good crack at it. I have my code working now without duplication. If you can imagine the game scrabble were the player gets a jumble of random letters, I am dragging the letters into spaces that are containers, these attract the tile when it is near. My question now is can I assign a string value to these objects and get each container to read that value and append it to a string to make a word. I’m having a lot of problems trying to figure this out [import]uid: 208811 topic_id: 34508 reply_id: 137215[/import]

I think all you really need is a single touch function which is attached to each piece on the board. It moves the piece being dragged and can detect when it is close to a droppable location by calculating the column/row location. This is pretty straightforward - I think the code you have above is overly complex. It should not be more than a few lines and the excessive if’s you have definitely don’t need to be there.

You could also do it with physics and sensors, but that might be overkill. Would give you some nice effects as the positioning of pieces is not that different in maths as your solution requires. [import]uid: 8271 topic_id: 34508 reply_id: 137216[/import]

I think you need to provide a diagram of what it is you want to achieve here. [import]uid: 8271 topic_id: 34508 reply_id: 137214[/import]

Thanks for replying, I think I might have jumped the gun and posting my problem before having a really good crack at it. I have my code working now without duplication. If you can imagine the game scrabble were the player gets a jumble of random letters, I am dragging the letters into spaces that are containers, these attract the tile when it is near. My question now is can I assign a string value to these objects and get each container to read that value and append it to a string to make a word. I’m having a lot of problems trying to figure this out [import]uid: 208811 topic_id: 34508 reply_id: 137215[/import]

I think all you really need is a single touch function which is attached to each piece on the board. It moves the piece being dragged and can detect when it is close to a droppable location by calculating the column/row location. This is pretty straightforward - I think the code you have above is overly complex. It should not be more than a few lines and the excessive if’s you have definitely don’t need to be there.

You could also do it with physics and sensors, but that might be overkill. Would give you some nice effects as the positioning of pieces is not that different in maths as your solution requires. [import]uid: 8271 topic_id: 34508 reply_id: 137216[/import]