[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: 6645 topic_id: 7887 reply_id: 28043[/import]