can someone test the code and help me please i don’t know why the square are offset from their position
local widget = require( “widget” )
local screenGroup = display.newGroup()
local physics = require(“physics”)
physics.start()
physics.setDrawMode(“normal”)
physics.setGravity(0,0)
local stateMachine
local currentTarget = nil
local isHighlighted = false
– Create a ScrollView
scrollView = widget.newScrollView
{
width = 1200,
height = 200,
scrollWidth = 1200,
scrollHeight = 100,
verticalScrollDisabled = true,
--hideBackground = true,
backgroundColor = {0,0,0}
}
local rect = {}
local pos = 0
for i=1, 60 do
rect[i] = display.newRoundedRect( 125 + (pos*120), 100, 100, 100, 3)
rect[i]:setFillColor(1,0,1)
rect[i].name = “holder”
rect[i].alpha = 0
physics.addBody(rect[i], “dynamic”)
rect[i].isSensor = true
pos = pos + 1
scrollView:insert( rect[i] )
end
screenGroup:insert(scrollView)
function onRectangleTouch(event)
local t = event.target
– printTouch(event)
local phase = event.phase
if phase == “began” then
– Make target the top-most object
local parent = t.parent
parent:insert(t)
display.getCurrentStage():setFocus(t)
– This flag is to prevent spurious events being sent to the target
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x --event.xStart
t.y0 = event.y - t.y --event.yStart
print("t.x0 = "…t.x0)
– Make myObject temporarily kinematic
event.target.bodyType = “kinematic”
– Stop current motion, if any
event.target:setLinearVelocity(0,0)
event.target.angularVelocity = 0
screenGroup:insert(t)
elseif t.isFocus then
if phase == “moved” then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif phase == “ended” or phase == “cancelled” then
if currentTarget ~= nil and isHighlighted then
– Move piece to target
transition.to(t,{
time = 150,
x = currentTarget.x,
y = currentTarget.y,
})
print("currentTarget.x = "…currentTarget.x)
scrollView:insert(t)
currentTarget = nil
isHighlighted = false
end
display.getCurrentStage():setFocus(nil)
t.isFocus = false
– Switch body type back to “static”
event.target.bodyType = “static”
end
end
return true
end
local rectangle = {}
pos = 0
for i = 1, 60 do
rectangle[i] = display.newRoundedRect( 125 + (pos*120), 100, 100, 100, 3)
pos = pos + 1
rect[i]:setFillColor(0,1,0)
physics.addBody(rectangle[i], “static”)
rectangle[i].name = “animals”
rectangle[i]:addEventListener( “touch” , onRectangleTouch)
scrollView:insert(rectangle[i])
end
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
--print( self.name … ": collision began with " … event.other.name )
stateMachine:dispatchEvent({name=“highlight”, state=“on”, customData = self});
elseif ( event.phase == “ended” ) then
--print( self.name … ": collision ended with " … event.other.name )
stateMachine:dispatchEvent({name=“highlight”, state=“off”, customData = self});
end
end
for k,v in pairs(rect) do
rect[k].collision = onLocalCollision
rect[k]:addEventListener( “collision”, rect[k] )
end
–Create an empty group to use as a listener for custom events
stateMachine = display.newGroup();
–Centralized logic control
function stateMachine:highlight(e)
if(e.state == “on”) then
--print("Highlight on: " … e.customData.object1.name)
currentTarget = e.customData
isHighlighted = true
e.customData:setFillColor(255,255,0)
elseif(e.state == “off”) then
currentTarget = nil
isHighlighted = false
--print("Highlight off: " … e.customData.other.name)
e.customData:setFillColor(100,100,100)
end
end
stateMachine:addEventListener(“highlight”, stateMachine);