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