Ok, so I have two questions really. One is pretty simple. Im trying to figure out how to detect collisions. I know its easy I think the only reason I’m having problems is because my collision detection occurs inside a function. The code is posted below and the lines to look at are 35-39 (the function) and 113-120. Its supposed to print “Its Working!” to terminal and its not showing up. Everything else is working.
The other question I have is how to make a sprite play on collision. Im guessing you would just move the — into the collision function. Would you put the code to do so after line 36? And can you move and make sprites behave like physical bodies (i.e. add bounciness, friction, and that good stuff to them?) Ive commented out the lines that make the sprite play for now.
Thanks in advance
[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
local bird
local block1
– BEGINNING OF YOUR IMPLEMENTATION
– NOTE: Code outside of listener functions (below) will only be executed once,
– unless storyboard.removeScene() is called.
– move Bird function
local function moveBird(event)
if event.phase == “began” then
local target = event.target – this is the bird that was touched
target:applyLinearImpulse( .3 , -1.3, bird.x, bird.y)
end
return true
end
local function myCollision(event)
if event.other.myName == “block1” then
print “Its Working!”
end
end
– Called when the scene’s view does not exist:
function scene:createScene( event )
local group = self.view
– create the level background
local background = display.newImageRect( “Stage1BG.jpg”, display.contentWidth, display.contentHeight )
background:setReferencePoint( display.TopLeftReferencePoint )
background.x, background.y = 0, 0
– make a bird (off-screen), position it, and rotate slightly
bird = display.newImage( “Bird2Side.png” )
bird.x = display.contentWidth/65 + 30
bird.y = display.contentHeight/2
– add physics to the bird
physics.addBody( bird, “dynamic”, { density=0.2, friction=0.3, bounce=0.4, radius=22 } )
–add an event listener to the bird
bird:addEventListener( “touch”, moveBird )
–local sprite = require “sprite”
–local NewBlock = sprite.newSpriteSheetFromData( “NewBlock.png”, require(“NewBlock”).getSpriteSheetData() )
–local spriteSet = sprite.newSpriteSet(NewBlock,1,11)
–sprite.add(spriteSet,“NewBlock”,1,11,200,0)
–local spriteInstance = sprite.newSprite(spriteSet)
–spriteInstance.x = display.contentWidth/2
–spriteInstance.y = display.contentHeight/2
–spriteInstance:prepare(“NewBlock”)
–spriteInstance:play()
local startingBranch = display.newImage( “StartingBranch.png” )
startingBranch.x = display.contentWidth/65
startingBranch.y = display.contentHeight - 40
–add starting Branch as a physics body
physics.addBody(startingBranch, “static”, { friction=0.5 } )
local endingBranch = display.newImage(“EndingBranch.png”)
endingBranch.x = display.contentWidth
endingBranch.y = display.contentHeight - 40
–add ending Branch as a physics body
physics.addBody(endingBranch, “static”, { friction=0.5 })
local block1 = display.newImage(“WoodBlock1.png”)
block1.x = display.contentWidth/4
block1.y = display.contentHeight - 40
–add block 1 as physics body
physics.addBody( block1, “static”, { density=2.0, friction=0.3, bounce = .7 } )
local block2 = display.newImage(“WoodBlock2.png”)
block2.x = display.contentWidth/2
block2.y = display.contentHeight - 40
–add block 2 as physics body
physics.addBody( block2, “static”, { density = 1, friction = 2, bounce = .7, })
local block3 = display.newImage(“WoodBlock3.png”)
block3.x = display.contentWidth/1.33
block3.y = display.contentHeight - 40
–add block 3 as physics body
physics.addBody( block3, “static”, { density=2.0, friction=0.3, bounce = .7 } )
– Adds “myName” to both the ojbects colliding
bird.myName = “bird”
block1.myName = “block1”
block2.myName = “block2”
block3.myName = “block3”
– Collision detection
block1:addEventListener(“collision”, myCollision)
– all display objects must be inserted into group
group:insert( background )
group:insert( bird )
group:insert( block1 )
group:insert( block2 )
group:insert( block3 )
group:insert( startingBranch )
group:insert( endingBranch )
end[/lua] [import]uid: 162953 topic_id: 29921 reply_id: 329921[/import]