According the the documentation at http://docs.coronalabs.com/api/event/collision/x.html, event.x (and event.y) should mark where the collision occurs in global coordinates. I’m having trouble getting this to happen. How can I create a circle where two objects collide?
Here’s a sample modified from the basic Physics Based Game for a new project:
–
– level1.lua
local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
– include Corona’s “physics” library
local physics = require “physics”
physics.start(); physics.pause()
– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
– BEGINNING OF YOUR IMPLEMENTATION
– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
– create a grey rectangle as the backdrop
local background = display.newRect( 0, 0, screenW, screenH )
background:setFillColor( 128 )
– make a crate (off-screen), position it, and rotate slightly
local crate = display.newImageRect( “crate.png”, 90, 90 )
crate.x, crate.y = 160, -100
crate.rotation = 15
crate.myName = “crate”;
– add physics to the crate
physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } )
– create a grass object and add physics (with custom shape)
local grass = display.newImageRect( “grass.png”, screenW, 82 )
grass:setReferencePoint( display.BottomLeftReferencePoint )
grass.x, grass.y = 0, display.contentHeight
grass.myName = “grass”;
– define a shape that’s slightly shorter than image bounds (set draw mode to “hybrid” or “debug” to see)
local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }
physics.addBody( grass, “static”, { friction=0.3, shape=grassShape } )
– all display objects must be inserted into group
group:insert( background )
group:insert( grass)
group:insert( crate )
end
– Called immediately after scene has moved onscreen:
function scene:enterScene( event )
local group = self.view
physics.start()
end
– Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
physics.stop()
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
package.loaded[physics] = nil
physics = nil
end
local function onCollision( event )
if ( event.phase == “began” ) then
print( "began: " … event.object1.myName … " & " … event.object2.myName … " at " … event.x … “,” … event.y )
local myCircle = display.newCircle( 0, 0, 10 )
myCircle.x = event.x;
myCircle.y = event.y;
myCircle:setFillColor(0,0, 255)
elseif ( event.phase == “ended” ) then
print( "ended: " … event.object1.myName … " & " … event.object2.myName … " at " … event.x … “,” … event.y )
end
end
– END OF YOUR IMPLEMENTATION
Runtime:addEventListener( “collision”, onCollision )
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene [import]uid: 177725 topic_id: 30886 reply_id: 330886[/import]