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. :(
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | --[[ 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 |
+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
try setting focus
I am new to setting the focus.. do you do something like this?
event.target.isFocus = true
heres a snippet from one of my files
1 2 3 4 5 6 7 8 9 10 11 12 | 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() |
if it helps someone, heres the finally working code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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 |
NIIIICE!!
how do I get that other picture back though?
I tried this but the other word keeps on following the mouse..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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 |
This code seems to server my purpose in this app so it is now 100% complete
Here if anybody wants to take a look..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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 |
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
How to Make something flash?
Weird error with the enterFrame event
Cross App Promotion
Having an issue with dragging
First Code Exchange submission -- RSS parser
SQLite - single record selection?
Need help calling a function inside an object from another function
Make spawning objects not spawn at the same time
How do you make a list of display objects?