How to make an object that I click on be removed from one location and pop up to a different location I click on?
[import]uid: 127842 topic_id: 22351 reply_id: 322351[/import]
Hi.
Here is a very basic way of doing it :
[code]
local myObject = display.newRect(100, 100, 50, 50)
local function moveObject(event)
myObject.x = event.x
myObject.y = event.y
return true
end
Runtime:addEventListener(“tap”, moveObject) [import]uid: 84637 topic_id: 22351 reply_id: 89074[/import]
Thanks.
I tried this and I either get an error or it doesn’t work at all.
I removed the “local myObject = display.newRect(100, 100, 50, 50)”
since I already have an object.
The error I’m getting has to do with the Runtime
If it makes a difference, I have items that are spawned when I click a button. I want to be able click on one of the spawned items and then have it move somewhere else that i choose. [import]uid: 127842 topic_id: 22351 reply_id: 89088[/import]
The error is probably simply you not updating your target image.
This was only an example. If you want to click on a spawned item and move it somewhere else that will take a little more work obviously.
I’l post up an example of that in a few mins [import]uid: 84637 topic_id: 22351 reply_id: 89090[/import]
Ok try this (works)
local myObject = display.newRect(100, 100, 50, 50)
local myObject2 = display.newRect(250, 100, 50, 50)
local targetToMove = nil
local function setTarget(event)
local target = event.target
targetToMove = target
return true
end
myObject:addEventListener("tap", setTarget)
myObject2:addEventListener("tap", setTarget)
local function moveObject(event)
if targetToMove ~= nil then
targetToMove.x = event.x
targetToMove.y = event.y
end
return true
end
Runtime:addEventListener("tap", moveObject)
You tap the square you want to move, that square continues to be the active target until you tap another.
If you want to only be able to move a target once at a time (after selecting it) do this :
[code]
local myObject = display.newRect(100, 100, 50, 50)
local myObject2 = display.newRect(250, 100, 50, 50)
local targetToMove = nil
local function setTarget(event)
local target = event.target
targetToMove = target
return true
end
myObject:addEventListener(“tap”, setTarget)
myObject2:addEventListener(“tap”, setTarget)
local function moveObject(event)
if targetToMove ~= nil then
targetToMove.x = event.x
targetToMove.y = event.y
targetToMove = nil
end
return true
end
Runtime:addEventListener(“tap”, moveObject) [import]uid: 84637 topic_id: 22351 reply_id: 89104[/import]
That’s great! Thanks for the code. Now I can dissect it and learn how it actually works.
Thanks again. [import]uid: 127842 topic_id: 22351 reply_id: 89124[/import]
Well, I looked at it and I understand the concepts but I don’t know how to incorporate it into my code. I’ve been racking my brain for hours and I just can’t grasp the concept of what I’m doing wrong…
At the moment, this is how my code works:
[code]
local function spawnDisk( event )
local phase = event.phase
if “ended” == phase then
audio.play( popSound )
randImage = diskGfx[math.random( 1, 4 )]
allDisks[#allDisks + 1] = display.newImage( randImage )
local disk = allDisks[#allDisks]
disk.x = 60
disk.y = 50
disk.xScale = 1.8; disk.yScale = 0.8
transition.to(disk, { time = 500, xScale = 1.0, yScale = 1.0, transition = easingx.easeOutElastic })
physics.addBody( disk, { density=0.5, friction=0.9, radius=20.0, bounce=0.2 } )
disk.linearDamping = 0.4
disk.angularDamping = 0.6
disk:addEventListener( “touch”, dragBody )
end
return true
end
pipe:addEventListener( “touch”, spawnDisk ) – touch the red disk to spawn objects
Runtime:addEventListener( “enterFrame”, removeOffscreenItems ) – clean up offscreen disks [import]uid: 127842 topic_id: 22351 reply_id: 89182[/import]
I figured it out! ( I think) Thanks for the help. [import]uid: 127842 topic_id: 22351 reply_id: 89281[/import]