Here’s two solutions. The first is manually calculating whether the boxes are overlapping and the second requires adding the physics engine to detect collisions. You’ll notice that in this example only the start and end of the physics collisions are displayed, but you can check the API documentation to see that there are other types.
main.lua:
[lua]–
– Abstract: Follow Me (touch event) sample app
– Version: 1.1
– Copyright © 2010 ANSCA Inc. All Rights Reserved.
– Permission is hereby granted, free of charge, to any person obtaining a copy of
– this software and associated documentation files (the “Software”), to deal in the
– Software without restriction, including without limitation the rights to use, copy,
– modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
– and to permit persons to whom the Software is furnished to do so, subject to the
– following conditions:
– The above copyright notice and this permission notice shall be included in all copies
– or substantial portions of the Software.
– THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
– INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
– PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
– FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
– OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
– DEALINGS IN THE SOFTWARE.
– Demonstrates how to use create draggable objects. Also shows how to move
– an object to the top.
local physics = require(“physics”)
physics.start()
physics.setGravity(0,0)
physics.setDrawMode(“hybrid”)
local arguments =
{
{ name=“one”, x=50, y=10, w=100, h=100, r=10, red=255, green=0, blue=128 },
{ name=“two”, x=10, y=200, w=100, h=100, r=10, red=0, green=128, blue=255 },
{ name=“three”, x=90, y=350, w=100, h=100, r=10, red=255, green=255, blue=0 }
}
– manual checking of collisions
local stage = display.getCurrentStage()
local function checkCollisions()
for i=1, stage.numChildren do
for c=1, stage.numChildren do
if (i ~= c) then
local a, b = stage[i], stage[c]
local hw = b.width
if (a.x>b.x-hw and a.x<b.x and a.y>b.y-hw and a.y<b.y then> print('MANUAL: ‘…a.name…’ collides with '…b.name)
end
end
end
end
end
local function printTouch( event )
if event.target then
local bounds = event.target.stageBounds
print( “event(” … event.phase … “) (”…event.x…","…event.y…") bounds: “…bounds.xMin…”,"…bounds.yMin…","…bounds.xMax…","…bounds.yMax )
checkCollisions()
end
end
local function onTouch( event )
local t = event.target
– Print info about the event. For actual production code, you should
– not call this function because it wastes CPU resources.
printTouch(event)
local phase = event.phase
if “began” == phase then
– Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
– Spurious events can be sent to the target, e.g. the user presses
– elsewhere on the screen and then moves the finger over the target.
– To prevent this, we add this flag. Only when it’s true will “move”
– events be sent to the target.
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
– Make object move (we subtract t.x0,t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
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
end
end
– Important to return true. This tells the system that the event
– should not be propagated to listeners of any objects underneath.
return true
end
local function physicsCollision( event )
print('PHYSICS: ‘…event.target.name…’ collided with '…event.other.name)
end
– Iterate through arguments array and create rounded rects (vector objects) for each item
for _,item in ipairs( arguments ) do
local button = display.newRoundedRect( item.x, item.y, item.w, item.h, item.r )
button:setFillColor( item.red, item.green, item.blue )
button.strokeWidth = 6
button:setStrokeColor( 200,200,200,255 )
– physics collision detection
button.name = item.name
physics.addBody( button, “dynamic” )
button.isSensor=true
button:addEventListener( “collision”, physicsCollision )
– Make the button instance respond to touch events
button:addEventListener( “touch”, onTouch )
end
– listener used by Runtime object. This gets called if no other display object
– intercepts the event.
local function printTouch2( event )
print( “event(” … event.phase … “) (”…event.x…","…event.y…")" )
return true
end
Runtime:addEventListener( “touch”, printTouch2 )[/lua]
[import]uid: 8271 topic_id: 27327 reply_id: 111042[/import] </b.y></b.x>