dragging from a virtual stack

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:

-- 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);  

As you can see from the lines commented out in the code, I first tried to store the newly created Icon in a temp var and then add that to the table. As that did not work I tried to store the icon inside the table directly, but that didin’t change anything. As a third try I thought it might be a somehow not properly-working table-library and tried storing it directly via indices, but same result…

Any suggestions?

Thanks in advance,
Dschonny

[import]uid: 98556 topic_id: 16700 reply_id: 316700[/import]

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. [import]uid: 52491 topic_id: 16700 reply_id: 62526[/import]

Well, not sure if I understood what you mean by plug and play code, if it means the code should be executable as is, I tried to do so, but pittily forgot to add the lines for starting the physics engine.
So here’s the plug and play code, I also replaced the image which would need a .png-file to be present by a simple rectangle…

-- start physics  
-----------------------------------------------------------------  
local physics = require "physics";  
physics.start();  
physics.setGravity(0, 10);  
physics.setDrawMode("hybrid");  
-- var to prevent creating multiple icons  
----------------------------------------------------------------  
iconCreated = 0;  
   
--List storing dragged Actions  
----------------------------------------------------------------  
placedActions = {};  
   
-- create stack  
----------------------------------------------------------------  
action\_key = display.newRect(50, 50, 50, 50);  
   
-- 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.newRect(event.x, event.y, 50, 50);  
 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);  
  

[import]uid: 98556 topic_id: 16700 reply_id: 62633[/import]

**Solved**

Just misinterpreted how event-handling of touch events works, I thought dragging initially would be handled by the createActionIcon-function as long as the dragging didn’t stop, instead it immediatly switched to the registered function for the new icon, so the last parts of the creation-function never were reached…

Thanx anyways, sometimes just posting about a problem brings new views…

[import]uid: 98556 topic_id: 16700 reply_id: 62635[/import]

Well done, glad to hear you were able to solve it :slight_smile: [import]uid: 52491 topic_id: 16700 reply_id: 62736[/import]