picking weeds is FUN! [Solved]

@Jayant hahaha. Very funny!
@leivagames do you offer beta testing for free? [import]uid: 51459 topic_id: 17168 reply_id: 64821[/import]

still, why screen reacts to touch and drag outside the objects? i dont get it, its must be a bug [import]uid: 16142 topic_id: 17168 reply_id: 64823[/import]

if you still have problems, then try the way I suggested earlier which includes each weed having it’s own function embedded.

using the weed Factory, that will create a weed at the randomX and randomY that we pass to it.

[code]
local function makeNewWeed(x,y)
local weed = display.newImage(“weed.png”,x,y)

local function onTouch(event)
local phase = event.phase
local target = event.target

print("Touched weed ", target.weedID ,"Phase = " … phase )
end

weed:addEventListener(“touch”, onTouch)

return weed
end
local weeds = {}
local i=0
local rX, rY

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

weeds[i] = makeNewWeed(rX,rY)
weeds[i].weedID = i
end

[/code] [import]uid: 3826 topic_id: 17168 reply_id: 64826[/import]

it still reacts to outside drag, when i touch screen and drag with mouse its grab whatever comes on its way

its definitely a bug, maybe its because of the “stable 591” build? [import]uid: 16142 topic_id: 17168 reply_id: 64828[/import]

It’s soooo easy to be *fixated* that it’s a bug just because it is a stable/daily build…

did you consider the fact that, when your code looks like this

 local function onTouch(event)  
 if event.phase == "moved" then  
 print("moooooed")  
 end  
 end  

Now, touch anywhere and then just pass your mouse/finger over the object, it will trigger the print.

This *is* definitely not a bug, but the weed!!

that is exactly how the touch is supposed to work and that is what I had mentioned in another post when someone was talking about shortening the lines of code, these are the kind of things that happen.

so the fix…

 local function onTouch(event)  
 if "began" == event.phase then  
 --First let us start with the began  
 event.target.isFocus = true  
 event.target.touchID = event.id  
 elseif "moved" == event.phase and   
 event.target.isFocus and   
 event.target.touchID == event.ID then  
 --We check for the event being moved and the fact  
 -- that it had the touch and needs to be moved  
 -- and in a multi-touch we also ensure that the ID  
 -- of the touch is the same as the one that started it  
 print("Moooooed")  
 elseif "ended" == event.phase or "cancelled" == event.phase then  
 event.target.isFocus = nil  
 event.target.touchID = nil  
 end  
 end  

We have suddenly made the unstable build that you had with a bug… non buggy… TADA!!

cheers,

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

guess there’s so much more for me to learn) i just did not stumble upon something like that earlier… theres first time for everything)

thanks a lot, JayantV [import]uid: 16142 topic_id: 17168 reply_id: 64834[/import]

there always is… believe me even I learn something everyday, not necessarily about lua or Corona or xcode or programming, just about anything.

I am sure that many here might have played with Lego at some stage or the other. I have this habit, whenever I am trying to make a LEGO or a furniture that comes in a flat pack, one of the most frequent sentence is “They have a piece short” you cannot imagine the amusement that it causes to everyone around me, it is like clockwork, I have to say that when I open and start… and generally as it turns out… the end product is always amazing.

As a developer, I would *dislike* my user blaming my software and calling it a bug, because that sets a precedence for other users to easily discard using their minds and when the do not want to do something, they just start, “Oh, you know that’s a bug”

15+ odd years dealing with users that find a way to mask their ignorance by blaming something else I can only say “Do not fall into that trap or encourage others to do so…”

cheers,

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

Hey thanks JayantV!

Just a quick question… How come I can’t drop the weed like you did with the plastic army man…

When I do something like this

[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.newRect(0,0,50,50)
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( event.target )
unroot.x = event.x
unroot.y = event.y
if unroot.isFocus==true then return end
if unroot.y < (_H *.95) then
unroot.y = unroot.y+speed
end
elseif “ended” == event.phase or “cancelled” == event.phase then
print(hitTestObjects(unroot, hitSpot))
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)
–unroot:setFillColor(255,0,0)
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… [import]uid: 51459 topic_id: 17168 reply_id: 64862[/import]

see I don’t do weed, so dropping it is easy for me… do not get too attached to it :wink:

seriously,

For the armyman, I have a enterFrame loop running that moves the armyMan down *without* physics since your code lacks that, it does not drop the weed.

cheers,

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

I actually figured out something better… :slight_smile:

[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) then
transition.to( unroot, { time= 500, alpha=0, y= 350 } )
end
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)
–unroot:setFillColor(255,0,0)
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

[/lua] [import]uid: 51459 topic_id: 17168 reply_id: 64866[/import]

if transitions work for you, go for it…

cheers,

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

Funny… So I am beginning to understand now… JayantV… When you set up the enterFrame it checks if you are below a certain point and if you are it will drop it…

I have another question regarding the dragging, can you tell me why when I let go of and pick up another weed it looses focus and you can’t pick it up again? I am lost… I guess I smoked to much… hehe

[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) then
transition.to( unroot, { time= 500, alpha=0, y= 350 } )
end
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)
–unroot:setFillColor(255,0,0)
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
[/lua]
thanks JayantV [import]uid: 51459 topic_id: 17168 reply_id: 64869[/import]

simple…

…because when you touch another weed, the unroot variable is not pointing to another weed.

Secondly, your magic number y=350, i am not sure where you got that from.

The iOS screen is generally 480x320(landscape) or 320x480 (portrait) or 960x640 (retina landscape) or 640x960 (retina portrait)

I hope the code above is for learning *only* it needs optimisation and *lots* of it as there are plenty of issues that will pop-up as you go…
cheers,

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

I am learning every day. Remember I have only been programing since april.

I put y=350 because the hitSpot.y =300 And so It appears to be fading into a bucket when I place the weed on top of it…

This is a new concept that you have brought to my attention… Unfortunately I am working with a programmer who doesn’t really know what she is doing (my mom) and doesn’t tell me things like optimisation because she is to busy doing *other* things… I am willing to learn the right way, so if you can PLEASE show me an example or something that would really be helpful!!..
Thanks JayantV

JAKE
[import]uid: 51459 topic_id: 17168 reply_id: 64873[/import]

Heres what I wanted!! All 100% complete and working with no glitches!!

[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

local gameState = 0

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

local rectTable = {}

local function onTouch(event)

local t = event.target
local phase = event.phase
local id = event.target.id

if “began” == event.phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
print(id)
rectTable[id]:setFillColor(255,0,0)

– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y

elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false

print(“YES!! Touching the gBox”)
end
end
– Stop further propagation of touch event!
return true
end

for i=1,5 do

local imgRect = display.newRect(0,0,50,50)
imgRect.x = math.random(150,200) ; imgRect.y = math.random(150,350)
rectTable[i] = imgRect
rectTable[i].id = i
rectTable[i]:setFillColor(0,255,0)
rectTable[i].x = math.random(50,350)
rectTable[i].y = math.random(50,350)
rectTable[i]:addEventListener(“touch”, onTouch)
print(rectTable[i].id)
end[/lua] [import]uid: 51459 topic_id: 17168 reply_id: 65085[/import]