hi
i found the following code in the forum, but i cant figure out how to add more objects. i was able to add 1 object but it wont snap to the slot. what will happen is the 1st object will quickly move and push the 2nd object out of the way. is there a way to add as many objects as i want, and have them snap to the nearest slot? also is it possible that if there is an existing object, it will get pushed to the right and snap to the nearest slot if an object is place over it? it might sound kinda confusing, but the easiest example i can give would be t he ibook book case. you can sort and drag and drop a book and it will snap into the nearest slot while pushing the other books if the slot is take. how can you do that in corona?
any help would be extremely appreciated!!! thank you in advance
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0,0)
–physics.setDrawMode(“hybrid”)
local ROWS=5
local COLS=5
local SLOT_SIZE=96
local board
local player
local collidedWith – slot that we collide with
– A basic function for dragging physics objects
local function startDrag( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
– if we have collided with a slot,
– move the player to the slot
if(collidedWith ~= nil) then
–player.x = collidedWith.x
–player.y = collidedWith.y
– move smoothly to slot
transition.to(player,
{
time=100,
x = collidedWith.x,
y=collidedWith.y
})
end
end
end
– Stop further propagation of touch event!
return true
end
function onPlayerCollision(self,event)
if(event.phase==“began”) then
if(event.other.type==“slot”) then
collidedWith=event.other
end
elseif(event.phase==“ended” or event.phase==“cancelled”) then
if(event.other==collidedWith) then
collidedWith=nil
end
end
end
local function makeSlot()
local slot = display.newCircle(0,0,(SLOT_SIZE)/4)
slot:setFillColor(255,0,0,255)
slot:setReferencePoint(display.CenterReferencePoint)
physics.addBody(slot, “static”, { density=1,
radius=(SLOT_SIZE/4)
})
slot.isSensor=true
slot.type=“slot”
return slot
end
local function makeBoard()
board=display.newGroup()
local counter=0
for y=1, ROWS, 1 do
for x=1, COLS, 1 do
counter = counter + 1
local slot = makeSlot()
slot.x = (x-1) * SLOT_SIZE
slot.y = (y-1) * SLOT_SIZE
slot.id = counter
board:insert(slot)
end
end
board.x = SLOT_SIZE/2
board.y = SLOT_SIZE/2
end
local function makePlayer()
player = display.newRect(0,0,50,50)
player:setFillColor(0,255,255,255)
player.x= 150
player.y = 450
player.type=“player”
physics.addBody(player,“dynamic”, {density=1})
player.isFixedRotation=true
player.collision = onPlayerCollision
player:addEventListener( “collision”, player)
board:insert(player)
end
–
makeBoard()
makePlayer()
player:addEventListener( “touch”, startDrag )[/lua] [import]uid: 82086 topic_id: 14151 reply_id: 314151[/import]