picking weeds is FUN! [Solved]

I’m working on a garden game for kids… For the first part of the game the kids must pick the weeds out of the ground and toss them. So far in my code I have one picture of a weed in a garden which is attached to a table that tells it to go in different spots. When the weed is tapped a different picture shows up (a weed that has been uprooted)

What I am having trouble with is making the uprooted objects draggable… I have never added a drag event to an image that is connect to a table…Or I guess my case 2 images…

Here is the code I got so far…

[lua]–[[
PROJECT OBJECTIVES

OBJECTIVE 1:

pick weeds and make them appear to be uprooted. Toss them in the trash.

OBJECTIVE 2:

Spreed good seeds to grow

OBJECTIVE 3:

Water plants and watch them grow

]]
display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR
local loqsprite = require(‘loq_sprite’)
math.randomseed(os.time())
math.random()

local _W = display.contentWidth
local _H = display.contentHeight

local objects = {}
local speed = 5
local i
local x0 , y0, x1, x2

local t1 = {

{ x = 650, y = 365 },
{ x = 350, y = 380 },
{ x = 550, y = 480 },
{ x = 400, y = 550 },
{ x = 700, y = 600 },

}

local background = display.newImageRect(“background.png”, 1024, 768)
background.x = _W/2; background.y = _H/2

function pickMe(event)

local phase = event.phase
local x = event.x
local y = event.y
local target = event.target

local saveX = event.target.x
local saveY = event.target.y
local id = event.target.id

if “began” == phase then
display.currentStage:setFocus(objects[event.target])
target.x0 = x
target.y0 = y
target.isFocus = true

event.target:removeEventListener(“touch”, pickMe)
event.target:removeSelf()
objects[id] = nil
print(“objects”… id)
objects[event.target] = display.newImageRect(“picked.png”, 236, 221)
objects[event.target].x = saveX
objects[event.target].y = saveY

elseif “moved” == phase and target.isFocus==true then
target.x = target.x + (x-target.x0)
target.y = target.y + (y-target.y0)
target.x0 = x
target.y0 = y
elseif “ended” == phase then
display.currentStage:setFocus(nil)
objects[event.target].isFocus = false
target.x0 = nil
target.y0 = nil
end

end

for i = 1,5 do
objects[i] = display.newImageRect(“weed.png”, 200, 150 )
objects[i].x = t1[i].x; objects[i].y = t1[i].y
objects[i].id = i
objects[i]:addEventListener(“touch”, pickMe)
end[/lua]

Thanks for any help!! [import]uid: 51459 topic_id: 17168 reply_id: 317168[/import]

attaching listener to a table object is exactly the same as usual

table[1]:addEventListener(“touch”, foo)

or

for i=1, #table do
table[i]:addEventListener(“touch”, foo)
end

something like that [import]uid: 16142 topic_id: 17168 reply_id: 64633[/import]

Thanks darkconsole that makes a lot more sense!

I’ll give that a try… :slight_smile: [import]uid: 51459 topic_id: 17168 reply_id: 64636[/import]

Ok well I see the problem… I am trying to add 2 touch events… How do I combine them so that when I am clicking down on the set of weeds it appears to to be uprooted… By replacing the image… You can see on this new code I posted I commented out the other touch event which replaces the image…

[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR
local loqsprite = require(‘loq_sprite’)
math.randomseed(os.time())
math.random()

local _W = display.contentWidth
local _H = display.contentHeight

local objects = {}
local speed = 5
local i
local x0 , y0, x1, x2

local t1 = {

{ x = 650, y = 365 },
{ x = 350, y = 380 },
{ x = 550, y = 480 },
{ x = 400, y = 550 },
{ x = 700, y = 600 },

}

local background = display.newImageRect(“background.png”, 1024, 768)
background.x = _W/2; background.y = _H/2

function replaceMe(event)
local saveX = event.target.x
local saveY = event.target.y
local id = event.target.id
event.target:removeEventListener(“touch”, replaceMe)
event.target:removeSelf()
objects[id] = nil
print(“objects”… id)
objects[event.target] = display.newImageRect(“picked.png”, 236, 221)
objects[event.target].x = saveX
objects[event.target].y = saveY

end

function pickMe(event)
local phase = event.phase
local x = event.x
local y = event.y
local target = event.target
if “began” == phase then
display.currentStage:setFocus(objects[event.target])
target.x0 = x
target.y0 = y
target.isFocus = true
elseif “moved” == phase and target.isFocus==true then
target.x = target.x + (x-target.x0)
target.y = target.y + (y-target.y0)
target.x0 = x
target.y0 = y
elseif “ended” == phase then
display.currentStage:setFocus(nil)
objects[event.target].isFocus = false
target.x0 = nil
target.y0 = nil
end
end

for i = 1,5 do
objects[i] = display.newImageRect(“weed.png”, 200, 150 )
objects[i].x = t1[i].x; objects[i].y = t1[i].y
objects[i].id = i
objects[i]:addEventListener(“touch”, pickMe)
–objects[i]:addEventListener(“touch”, replaceMe)
end[/lua]
Also I am getting an error on when I drag the weeds around which has to do with this chunk of code
[lua]elseif “ended” == phase then
display.currentStage:setFocus(nil)
objects[event.target].isFocus = false
target.x0 = nil
target.y0 = nil[/lua]
Thanks darkconsoles [import]uid: 51459 topic_id: 17168 reply_id: 64637[/import]

Ok I tried to add a timer… But it just does not seem to like the fact that I have [lua] local x = event.x and local y = event.y[/lua] on one touch event and [lua]local saveX = id.event.target.x, local saveY = id.event.target.y[/lua] on another touch event

Heres my code:
[lua]–[[
PROJECT OBJECTIVES

OBJECTIVE 1:

Pick weeds and make them appear to be choped up.

OBJECTIVE 2:

Spreed good seeds to grow

OBJECTIVE 3:

Water plants and watch them grow

]]
display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR
local loqsprite = require(‘loq_sprite’)
math.randomseed(os.time())
math.random()

local _W = display.contentWidth
local _H = display.contentHeight

local objects = {}
local speed = 5
local i
local x0 , y0, x1, x2

local t1 = {

{ x = 650, y = 365 },
{ x = 350, y = 380 },
{ x = 550, y = 480 },
{ x = 400, y = 550 },
{ x = 700, y = 600 },

}

local background = display.newImageRect(“background.png”, 1024, 768)
background.x = _W/2; background.y = _H/2

function replaceMe(event)

local saveX = event.target.x
local saveY = event.target.y
local id = event.target.id

–if “ended” == phase then
event.target:removeEventListener(“touch”, replaceMe)
event.target:removeSelf()
objects[id] = nil
print(“objects”… id)
objects[event.target] = display.newImageRect(“picked.png”, 236, 221)
objects[event.target].x = t1[i].y
objects[event.target].y = t1[i].x
– end

end

function pickMe(event)
local phase = event.phase
local x = event.x
local y = event.y
local target = event.target
if “began” == phase then
display.currentStage:setFocus(objects[id])
target.x0 = x
target.y0 = y
target.isFocus = true
elseif “moved” == phase and target.isFocus==true then
target.x = target.x + (x-target.x0)
target.y = target.y + (y-target.y0)
target.x0 = x
target.y0 = y
–elseif “ended” == phase then
–display.currentStage:setFocus(nil)
–objects[event.target].isFocus = false
–target.x0 = nil
–target.y0 = nil

end
end
for i = 1,5 do
objects[i] = display.newImageRect(“weed.png”, 200, 150 )
objects[i].x = t1[i].x; objects[i].y = t1[i].y
objects[i].id = i
timer.performWithDelay(10, replaceMe )
objects[i]:addEventListener(“touch”, pickMe)
–objects[i]:addEventListener(“touch”, pickMe)
end[/lua]

Any suggestions and you can be my BEST Friend!!! [import]uid: 51459 topic_id: 17168 reply_id: 64664[/import]

its my example of how things like that can be done, but it need some work
[lua]local weedTable = {}

local unrootWeed = display.newRect(0,0,50,50)
unrootWeed:setFillColor(255,255,0)
unrootWeed.isVisible = false

local function drag_unroot(event)
if event.phase == “moved” then
event.target.x = event.x
event.target.y = event.y
end
end

local function drag_weed(event)
if event.phase == “moved” then
event.target.x = event.x
event.target.y = event.y
event.target:removeSelf()
event.target = nil
unrootWeed.isVisible = true
unrootWeed.x = event.x
unrootWeed.y = event.y
unrootWeed:addEventListener(“touch”, drag_unroot)
end
end

for i=1,5 do
local weed = display.newRect(0,0,50,50)
weedTable[i] = weed
weedTable[i]:setFillColor(0,255,0)
weedTable[i].x = math.random(50,350)
weedTable[i].y = math.random(50,350)
weedTable[i]:addEventListener(“touch”, drag_weed)
end[/lua]

try it and try to figure out how this code can be improved so unrooted weed will not be deleted from screen after event was ended and another weed was picked up [import]uid: 16142 topic_id: 17168 reply_id: 64681[/import]

THANKS darkconsoles!!

Looks good

You are the best!!! [import]uid: 51459 topic_id: 17168 reply_id: 64684[/import]

THATS EXACTLY WHAT I WAS LOOKING FOR!!! YOUR A PRO!!! [import]uid: 51459 topic_id: 17168 reply_id: 64687[/import]

if someone still curious, i post best way(i think) to do those things
[lua]local weedTable = {}
local unrootTable = {}

for i=1, 5 do
local unrootWeed = display.newRect(0,0,50,50)
unrootTable[i] = unrootWeed
unrootTable[i]:setFillColor(255,255,0)
unrootTable[i].isVisible = false
end

local function drag_unroot(event)
if event.phase == “moved” then
event.target.x = event.x
event.target.y = event.y
end
end

local function drag_weed(event)
local id = event.target.id
if event.phase == “moved” then
print(id)
event.target.x = event.x
event.target.y = event.y
event.target:removeSelf()
event.target = nil
unrootTable[id].isVisible = true
unrootTable[id].x = event.x
unrootTable[id].y = event.y
unrootTable[id]:addEventListener(“touch”, drag_unroot)
end
return true
end

for i=1,5 do
local weed = display.newRect(0,0,50,50)
weedTable[i] = weed
weedTable[i].id = i
weedTable[i]:setFillColor(0,255,0)
weedTable[i].x = math.random(50,350)
weedTable[i].y = math.random(50,350)
weedTable[i]:addEventListener(“touch”, drag_weed)
print(weedTable[i].id)
end[/lua]

now every weed is for itself and dont dissapear anymore [import]uid: 16142 topic_id: 17168 reply_id: 64695[/import]

Wow thats even better darkconsoles!!!

How did you get so amazing? [import]uid: 51459 topic_id: 17168 reply_id: 64697[/import]

hahaha, no, i’m seven light years from amazing
i havent even ship one game yet(but i will soon) [import]uid: 16142 topic_id: 17168 reply_id: 64698[/import]

I’m close to completing my first game too!!

The first game is always the hardest to make… :confused:

Whats your game going to be called? [import]uid: 51459 topic_id: 17168 reply_id: 64705[/import]

i’d like not to spoil my game name now, its good name though) [import]uid: 16142 topic_id: 17168 reply_id: 64706[/import]

Oh cool! I guess I’ll find out when its released… :slight_smile:

FYI… I was running some testes and when I move an object over another object… The object then gets removed… Is that suppose to happen? [import]uid: 51459 topic_id: 17168 reply_id: 64709[/import]

wow, thats one funny glitch
i dont know what went wrong and how to deal with it) [import]uid: 16142 topic_id: 17168 reply_id: 64710[/import]

I think it has to do with the fact that the mouse is down. And the program knows that so it removes the images that are suppose to be removed.

I kinda fixed the issue by changing “moved” in the drag_weed function to “began” but what ends up happening is the other images are like blocks and you can no longer move the draggable image anywhere on the screen…

Does that make sense? [import]uid: 51459 topic_id: 17168 reply_id: 64714[/import]

i did some work on it and now it works, i think it not the best way to do this, but still it does work)

[lua]local weedTable = {}

local unroot

local function drag_unroot(event)
if event.phase == “moved” then
unroot.x = event.x
unroot.y = event.y
end
end

local function drag_weed(event)
if event.phase == “began” then
print(event.target.id)
event.target:removeSelf()
event.target = nil
unroot = display.newRect(0,0,50,50)
unroot:setFillColor(255,0,0)
unroot.x = event.x
unroot.y = event.y
unroot:addEventListener(“touch”, drag_unroot)
end
return true
end

for i=1,5 do
local weed = display.newRect(0,0,50,50)
weedTable[i] = weed
weedTable[i].id = "weed #: "…i
weedTable[i]:setFillColor(0,255,0)
weedTable[i].x = math.random(50,350)
weedTable[i].y = math.random(50,350)
weedTable[i]:addEventListener(“touch”, drag_weed)
end
for i,v in pairs(weedTable) do
print(weedTable[i].id)
end[/lua]

if i change unroot.x in drag_unroot to event.target.x then this horrible glitch will occur, i guess its because of propagation of “moved” phase and i dont know how to break it [import]uid: 16142 topic_id: 17168 reply_id: 64731[/import]

Thanks man!! I thought I was asking to much of you when I found that glich… You are amazing!! [import]uid: 51459 topic_id: 17168 reply_id: 64745[/import]

I hope that none of you have been rolling it and then lighting it after picking the stuff that you have been… Hahahaha

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17168 reply_id: 64810[/import]

@ JayantV +1 LOL

@ jakescarano I was going to try land a hand but it seems you been taken care of
best of luck on your first application

@ darkconsoles Keep us posted when your app comes out and if you need beta
testing or feedbacks ill be more then happy help ya out! [import]uid: 30314 topic_id: 17168 reply_id: 64814[/import]