i’m almost done i juste have a little problem,
i can’t get out my object from the scollview
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
local scrollView = widget.newScrollView
{
width = display.contentWidth,
height =200,
verticalScrollDisabled = true,
hideBackground = true,
}
local function onRectangleTouch(event)
local t = event.target
if event.phase == “began” then
display.getCurrentStage():setFocus( event.target )
t.markX = t.x
t.markY = t.y
screenGroup:insert(event.target)
elseif event.phase == “moved” then
t.x = (event.x - event.xStart) + t.markX
t.y = (event.y - event.yStart) + t.markY
elseif event.phase == “ended” then
display.getCurrentStage():setFocus(nil)
end
return true
end
local rectangle = {}
for i=1,3 do
rectangle[i] = display.newRoundedRect( 50 + (i*120), 100, 100, 100, 3)
rectangle[i]:setFillColor(1,1,1)
rectangle[i].name = “holder”
physics.addBody(rectangle[i], “dynamic”)
rectangle[i].isSensor = true
--rectangle[i]:addEventListener(“touch”,onRectangleTouch)
scrollView:insert( rectangle[i] )
end
screenGroup:insert(scrollView)
–[[local myTarget = display.newRoundedRect(100,0,103,103,3)
myTarget:setFillColor(100,100,100)
myTarget.name = “holder”
physics.addBody(myTarget,“dynamic”)
myTarget.isSensor = true
–myTarget.alpha = 0
local myTarget1 = display.newRoundedRect(210,100,103,103,3)
myTarget1:setFillColor(100,100,100)
myTarget1.name = “holder2”
physics.addBody(myTarget1,“dynamic”)
myTarget1.isSensor = true
local myTarget2 = display.newRoundedRect(320,100,103,103,3)
myTarget2:setFillColor(100,100,100)
myTarget2.name = “holder3”
physics.addBody(myTarget2,“dynamic”)
myTarget2.isSensor = true
local myTarget3 = display.newRoundedRect(430,0,103,103,3)
myTarget3:setFillColor(100,100,100)
myTarget3.name = “holder3”
physics.addBody(myTarget3,“dynamic”)
myTarget3.isSensor = true
local myTarget4 = display.newRoundedRect(540,0,103,103,3)
myTarget4:setFillColor(100,100,100)
myTarget4.name = “holder4”
physics.addBody(myTarget4,“dynamic”)
myTarget4.isSensor = true]]
local myObject = display.newRoundedRect(50,50,100,100,3);
myObject:setFillColor(0,0,1)
myObject.x = 100
myObject.y = 100
myObject.name = “letter”
physics.addBody(myObject,“static”)
function myObject:touch(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
t.y0 = event.y - t.y
– Make myObject temporarily kinematic
event.target.bodyType = “kinematic”
– Stop current motion, if any
event.target:setLinearVelocity(0,0)
event.target.angularVelocity = 0
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,
})
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
– make myObject listen for touch events
myObject:addEventListener(“touch”,myObject)
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(rectangle) do
rectangle[k].collision = onLocalCollision
rectangle[k]:addEventListener( “collision”, rectangle[k] )
end
–[[myTarget.collision = onLocalCollision
myTarget:addEventListener( “collision”, myTarget )
myTarget1.collision = onLocalCollision
myTarget1:addEventListener( “collision”, myTarget1 )
myTarget2.collision = onLocalCollision
myTarget2:addEventListener( “collision”, myTarget2 )
myTarget3.collision = onLocalCollision
myTarget3:addEventListener( “collision”, myTarget3 )
myTarget4.collision = onLocalCollision
myTarget4:addEventListener( “collision”, myTarget4 )]]
–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);