Hi all,
I am a Corona newbie, with a lot of previous programming experience.
I am trying to implement a grid of objects, which I will drag other objects onto. I want to highlight target objects in the grid as I drag objects over it, and then if I let go of an object, whilst I am hovering over it, I want to transition the dragged object so that it can settle onto the highlighted grid item.
I have prototyped this using sensors for the grid. This all seems well, with the heavy lifting done by the physics engine. However, I am finding that firstly, sometimes when I drag over the target object the highlighting occurs, but the object doesn’t transition to the “grid” when I release the touch, and secondly, sometimes, multiple “grid” objects are highlighted too when I drag over them. I’m not sure of the best way to highlight a single item.
Any suggestions would be very much appreciated.
Some sample code with one draggable object, and two “grid” objects is shown below. This demonstrates the behaviour that I can’t work out, and runs in the simulator.
Thanks.
[lua]
local physics = require(“physics”)
physics.start()
physics.setDrawMode(“normal”)
physics.setGravity(0,0)
display.setStatusBar( display.HiddenStatusBar )
– Variables
local stateMachine
local currentTarget = nil
local isHighlighted = false
local myTarget = display.newRoundedRect(100,200,69,69,3)
myTarget:setFillColor(100,100,100)
myTarget.name = “holder”
physics.addBody(myTarget,“dynamic”)
myTarget.isSensor = true
local myTarget1 = display.newRoundedRect(180,200,69,69,3)
myTarget1:setFillColor(100,100,100)
myTarget1.name = “holder2”
physics.addBody(myTarget1,“dynamic”)
myTarget1.isSensor = true
local myObject = display.newRoundedRect(50,50,67,67,3);
myObject:setFillColor(0,0,255)
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
})
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
myTarget.collision = onLocalCollision
myTarget:addEventListener( “collision”, myTarget )
myTarget1.collision = onLocalCollision
myTarget1:addEventListener( “collision”, myTarget1 )
–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);
[/lua]