Hi there,
I would like to be able to drag icons from a virtual stack. Therefore I created an icon which represents the stack. When the user touches and drags that stack, I want a new Icon to be created which can be dragged elsewhere. Since I need collision detection, sensor-Physics need to be enabled on that new item. I'm storing the newly created icon inside a lua-table since I want the user to be able to drag up to a given number of icons from that stack. Things work so far, but as soon as I drag a second Item from the stack, the first one gets lost.
this is how I tried to implement it:
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 | -- var to prevent creating multiple icons ---------------------------------------------------------------- iconCreated = 0; --List storing dragged Actions ---------------------------------------------------------------- placedActions = {}; -- create stack ---------------------------------------------------------------- action_key = display.newImage("key.png",220, 20); -- function creating new Icon from stack ---------------------------------------------------------------- function createActionIcon(event) if event.phase == "moved" and iconCreated == 0 then if event.target == action_key then -- ActionIcon = display.newImage("key.png", event.x, event.y); -- table.insert(placedActions, display.newImage("key.png", event.x, event.y)); placedActions[#placedActions +1] = display.newImage("key.png", event.x, event.y); physics.addBody(placedActions[#placedActions], "static", {isSensor = true}); end placedActions[#placedActions]:addEventListener("touch", dragAction); -- table.insert(placedActions, ActionIcon); iconCreated = #placedActions; elseif event.phase == "moved" and iconCreated > 0 then placedActions[iconCreated].x = event.x; placedActions[iconCreated].y = event.y; elseif event.phase == "ended" then iconCreated = 0; end end -- function for dragging once icon is created ------------------------------------------------------------------- function dragAction(event) if event.phase == "began" then -- par = event.target; elseif event.phase == "moved" then event.target.x = event.x; event.target.y = event.y; end return true; end --adding Listener to stack-icon ------------------------------------------------------------------- action_key:addEventListener("touch", createActionIcon); |
Posting plug and play code would be a big help to anyone attempting to help you with this - they can't test what you have easily.