Glitch in the matrix!! Fixed :]

Hello fellow game makers…

So far I have code that when you click and drag an image it replaces it with another image. Everything seems to work great until I move the object over another object… That object gets removed. :frowning:

Here is the code:

[lua]–[[
PROJECT OBJECTIVES

OBJECTIVE 1:

Pick weeds and make them appear to be uprooted.

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 weedTable = {}
local unrootTable = {}

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

for i=1, 5 do
local unrootWeed = display.newImageRect(“picked.png”, 236, 221)
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
weedTable[id].isVisible = false
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.newImageRect(“weed.png”, 200, 150 )
weedTable[i] = weed
weedTable[i].id = i
weedTable[i].x = t1[i].x;
weedTable[i].y = t1[i].y;
weedTable[i]:addEventListener(“touch”, drag_weed)
print(weedTable[i].id)
end[/lua]

Thanks for any help finding this nasty little glitch!! [import]uid: 51459 topic_id: 17183 reply_id: 317183[/import]

+1, its all because of the event.target thing in drag_unroot function
it may be cured by disallowing event to propagate except object in question

anyone know how to do this? any help is appreciated [import]uid: 16142 topic_id: 17183 reply_id: 64732[/import]

try setting focus [import]uid: 7911 topic_id: 17183 reply_id: 64738[/import]

I am new to setting the focus… do you do something like this?

event.target.isFocus = true [import]uid: 51459 topic_id: 17183 reply_id: 64746[/import]

heres a snippet from one of my files
[blockcode]
if event.phase == “began” then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
elseif event.phase == “moved” then
event.target.y = math.min( 380, math.max( 140, event.y) )
elseif event.phase == “ended” or event.phase == “cancelled” then
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
ctrl[1] = math.floor( ( ( 380 - control[1].y ) / 24 ) + 0.5 )
ctrl[2] = math.floor( ( ( 380 - control[2].y ) / 24 ) + 0.5 )
print( ctrl[1]…","…ctrl[2] )
fnSetControl() [import]uid: 7911 topic_id: 17183 reply_id: 64748[/import]

NIIIICE!! [import]uid: 51459 topic_id: 17183 reply_id: 64755[/import]

if it helps someone, heres the finally working code:
[lua]local oneTable = {}
local function foo(event)
if event.phase == “began” then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
event.target.text = “unroot”
print(event.target.id)
elseif event.phase == “moved” then
event.target.x = event.x
event.target.y = event.y
elseif event.phase == “ended” then
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end

for i=1, 5 do

local root = display.newText(“root”,0,0,nil,30)
root.id = "#: "…i
oneTable[root.id] = root
root.x = math.random(50,500)
root.y = math.random(50,500)
oneTable[root.id]:addEventListener(“touch”, foo)
end

for i,v in pairs(oneTable) do
print(i,v)
end[/lua]

there’s a glitch however, if you touch screen, but not button, and drag it, the drag will pick up any object that crossed it and drag it too

i just dont know what all this means, listener is attached to an object, not to runtime and this is really weird [import]uid: 16142 topic_id: 17183 reply_id: 64753[/import]

how do I get that other picture back though? [import]uid: 51459 topic_id: 17183 reply_id: 64757[/import]

I tried this but the other word keeps on following the mouse…

[lua]local weedTable = {}

local function drag_weed(event)
if event.phase == “began” then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
unroot = display.newImageRect(“picked.png”, 236, 221)
unroot.x = event.x
unroot.y = event.y
unroot = event.target
print(event.target.id)
elseif event.phase == “moved” then
event.target.x = event.x
event.target.y = event.y
elseif event.phase == “ended” then
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end

for i=1, 5 do

local root = display.newText(“root”,0,0,nil,30)
root.id = "#: "…i
weedTable[root.id] = root
root.x = math.random(50,500)
root.y = math.random(50,500)
weedTable[root.id]:addEventListener(“touch”, drag_weed)
end

for i,v in pairs(weedTable) do
print(i,v)
end[/lua] [import]uid: 51459 topic_id: 17183 reply_id: 64759[/import]

This code seems to server my purpose in this app so it is now 100% complete

Here if anybody wants to take a look…

[lua]local _W = display.contentWidth
local _H = display.contentHeight

local weedTable = {}

local unroot

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 function drag_unroot(event)
if event.phase == “moved” then
display.getCurrentStage():setFocus( event.target )
unroot.x = event.x
unroot.y = event.y
unroot.isFocus = true

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 FOR ALLL THE HELP!!! [import]uid: 51459 topic_id: 17183 reply_id: 64765[/import]