Ok so I have a problem that I don’t quiet under stand… Which is the focus of dragging many objects… In my code I was able to click on an object which then changed to another object and I can drag that other object any where on the screen… When I release the mouse it seems to loose its focus and it just sits there on the screen… I can no longer drag that other object…
I studying some example code which does the same thing but when I add it seems to mess things up… I know I am missing this bit of code in the “began” phase[lua] – Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y[/lua]
and this in the “moved” phase[lua]t.x = event.x - t.x0
t.y = event.y - t.y0[/lua]
and this at the “end” phase[lua]display.getCurrentStage():setFocus( nil )
t.isFocus = false
[/lua]
When I add that it seems to bring the second object on the top left of the screen. and even when I add local t = event.target It doesn’t seem to like that either… Here is my code
[lua]–[[
PROJECT OBJECTIVES
OBJECTIVE 1:
Pick weeds and make them appear to be up rooted.
OBJECTIVE 2:
Spreed good seeds to grow
OBJECTIVE 3:
Water plants and watch them grow
]]
local _W = display.contentWidth
local _H = display.contentHeight
local weedTable = {}
local unroot
local speed = 5
local background = display.newImageRect(“background.png”, 1024, 768)
background.x = _W/2; background.y = _H/2
local t1 = {
{ x = 650, y = 365 },
{ x = 350, y = 380 },
{ x = 550, y = 480 },
{ x = 400, y = 550 },
{ x = 700, y = 600 },
}
local hitSpot = display.newImageRect(“basket.png”, 224, 165)
hitSpot.x = 800
hitSpot.y = 300
–hitSpot:setFillColor(255,0,0)
function hitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)
end
local function drag_unroot(event)
if event.phase == “moved” then
–display.getCurrentStage():setFocus( unroot )
unroot.x = event.x
unroot.y = event.y
elseif “ended” == event.phase or “cancelled” == event.phase then
if hitTestObjects(unroot, hitSpot) == true then
transition.to( unroot, { time= 500, alpha=0, y= 350 } )
end
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
local function drag_weed(event)
if event.phase == “began” then
print(event.target.id)
event.target:removeSelf()
event.target = nil
unroot = display.newImageRect(“picked.png”, 236, 221)
–display.getCurrentStage():setFocus(target )
unroot.isFocus = true
unroot.touchID = event.id
–target.isFocus = true
unroot.x = event.x
unroot.y = event.y
–event.target.isFocus
unroot:addEventListener(“touch”, drag_unroot)
– if event.phase == “end” then
– display.getCurrentStage():setFocus( nil )
– unroot.isFocus = false
–end
end
return true
end
for i=1,5 do
local weed = display.newImageRect(“weed.png”, 200, 150 )
weedTable[i] = weed
weedTable[i].id = "weed #: "…i
weedTable[i]:setFillColor(0,255,0)
weedTable[i].x = t1[i].x;
weedTable[i].y = t1[i].y;
weedTable[i]:addEventListener(“touch”, drag_weed)
end
for i,v in pairs(weedTable) do
print(weedTable[i].id)
end[/lua]
Thanks for any help!!! [import]uid: 51459 topic_id: 17209 reply_id: 317209[/import]

