Dragging many objects in a table. [Solved]

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]

why do you spawn all this threads? cant you wait for response in previous thread?) [import]uid: 16142 topic_id: 17209 reply_id: 64898[/import]

I’m sorry It’s just the one thing that I have been working on and I didn’t know if this was a new question… If I can delete threads I would [import]uid: 51459 topic_id: 17209 reply_id: 64899[/import]

can you please point me in the right direction? [import]uid: 51459 topic_id: 17209 reply_id: 64900[/import]

Usually on my projects that I work on require me to do all of artwork before I get into the code part… But this project that I am currently working on all the artwork is all ready done and so I am just coding and when I don’t get why something is not working I freak out and I do stupid things like post many forum questions… I appreciate all the help you have given me! I wish I had as much knowledge as most of the people on these forums so I can figure things out myself… [import]uid: 51459 topic_id: 17209 reply_id: 64901[/import]

Thats kind of cool!! Actually I just wanted to drag just one object from the table … but this is really cool and it gives me something to work with… :slight_smile:

Thanks for the help… :slight_smile: [import]uid: 51459 topic_id: 17209 reply_id: 64909[/import]

i did not fully understand what you tried to do, but i assumed you tried to make many objects drag with touching one of them

try my piece of code, its actually pretty funny
[lua]local weeds = {}

local function makeNewWeed(x,y)
local weed = display.newRect(x,y,50,50)

local function onTouch(event)
if “began” == event.phase then
–First let us start with the began
event.target.isFocus = true
elseif “moved” == event.phase and event.target.isFocus then
for i=1, #weeds do
weeds[i].x = event.x + math.random(50,200)
weeds[i].y = event.y + math.random(50,200)
if weeds[i] == event.target then
weeds[i].x = event.x
weeds[i].y = event.y
end
end
elseif “ended” == event.phase or “cancelled” == event.phase then
event.target.isFocus = nil
event.target.touchID = nil
end
end

weed:addEventListener(“touch”, onTouch)

return weed
end

local i=0

for i=1,5 do
–Get the rX, rY as random x,y which does not overlap

weeds[i] = makeNewWeed(math.random(50,300),math.random(50,300))
weeds[i].weedID = i
weeds[i]:setFillColor(math.random(0,255), math.random(0,255), math.random(0,255))
end[/lua] [import]uid: 16142 topic_id: 17209 reply_id: 64907[/import]

glad to help really, most people here not so talkative as you [import]uid: 16142 topic_id: 17209 reply_id: 64912[/import]

I work alone so you can only imagine the need for some kind of communication with a human being! [import]uid: 51459 topic_id: 17209 reply_id: 64915[/import]

i’m so understand you, i’m completely alone in this corona programming world too(art done by my brother, though)

its just awesome that we have this forums and all nice people in here) i would never learn a thing without asking questions [import]uid: 16142 topic_id: 17209 reply_id: 64917[/import]

Yea its a great place! Just curious, where are you located? [import]uid: 51459 topic_id: 17209 reply_id: 64918[/import]

Saint-Petersburg, Russia

and you? [import]uid: 16142 topic_id: 17209 reply_id: 64922[/import]

Wow that’s far. I live in the US In North Carolina. [import]uid: 51459 topic_id: 17209 reply_id: 64929[/import]

well, good for you then)
if you have any questions regarding code, post it here [import]uid: 16142 topic_id: 17209 reply_id: 64931[/import]

As long as all objects you want to drag are in some unified table, you can just have a drag event on one of them that loops through the entire table (via a for-loop) and modify their x/y properties accordingly. [import]uid: 52430 topic_id: 17209 reply_id: 64964[/import]

@darkconsoles… Can show me something like the one you just sent but with out the other boxes moving around and the box that you touched or dragged changes color green… So every box that was touched or dragged is now green and can be dragged around the screen.

Thanks [import]uid: 51459 topic_id: 17209 reply_id: 64981[/import]

@Jake,
I am glad that you got the answer to your questions… here’s something else to learn, slightly different than coding, but equally important.

“When I release the mouse it seems to loose its focus and it just sits there on the screen.”

The pants fell to the floor because the belt was *LOOSE* making the person *LOSE* his dignity. I hope you get the difference between LOSE and LOOSE…

Programming and syntax is similar, use the wrong syntax, it won’t work. Hope that is a learning in some way, otherwise you can always ignore this post :wink:

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17209 reply_id: 64983[/import]

It does teach me something. I am well aware that I have bad grammar. I didn’t care much about things like that growing up. But now I am taking an interest in improving myself so THANK YOU Jayant for pointing that out. :slight_smile:

Today at work I am making a mock program that has print statements within functions to see the order in which things work. So that I know when and where to call things. [import]uid: 51459 topic_id: 17209 reply_id: 65027[/import]

Hey Cool,
I like your attitude, unlike others that get all cranky.

BTW, did you see the Corona Code Generator, it’s an old app that allows for creating objects and events related to touch.

The Mac Version
here
and here

The windows version here

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17209 reply_id: 65030[/import]

Looks cool!! I will definitely check that out! [import]uid: 51459 topic_id: 17209 reply_id: 65041[/import]