question on how to play sound after collision

Hi all,

I have a question on how to get a sound to play after two objects collide. I have included what I have so far, but it’s not working. Any ideas?

Thanks!

[code]–Main function
function new()
local localGroup = display.newGroup()

local firstSound = audio.loadSound(“people-cheering.mp3”)

–play sound

local Target = function(self, event )
if event.phase == “began” then
if event.other.objectName == “target” then
audio.play(firstSound)
end
end
end

–Background
local background = display.newImage(“sunshine.png”)
localGroup:insert(background)

–first group
function hitTestObjects( obj1, obj2 )
local top = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local bottom = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local top = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local bottom = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return ( left or right ) or ( top or bottom )
end

local target = display.newImage(“first block.png”)
target.width = 140
target.height = 140
target.x = 100
target.y = 800
target.objectName = “target”
localGroup:insert(target)
local image = display.newImage( “clean dog.jpg”)
image.width = 175
image.height = 175
image.x = 100
image.y = 100
localGroup:insert(image)
local function drag(event)
if event.phase == “moved” then
image.x = event.x
image.y = event.y
print(hitTestObjects(target, image))
end
end

image:addEventListener(“touch”, drag) [import]uid: 32253 topic_id: 7374 reply_id: 307374[/import]

How does Target() get called. And you’re using the physics collision handler syntax for event.other but there’s no evidence of physics bodies.

Perhaps you’ve forgotten to include some code in the example. [import]uid: 3953 topic_id: 7374 reply_id: 26073[/import]

ok, so I made some modifications, bu still cant get collision detection to work (I left “print” in so I can see the results in terminal). Do I need to end the drag listeners so the collision listeners will work? Should I use gameUI instead? Sorry, I am new to this and still learning. Thanks again!

[code]
module(…, package.seeall)

local imagelookup = require("_imagelookup")
local animlookup = require("_animlookup")
local physics = require(“physics”)
physics.start()
function newlevel1 ()
local this = display.newGroup()

local background = display.newImageRect(imagelookup.table[“Sunshine”],imagelookup.table[“SunshineWidth”],imagelookup.table[“SunshineHeight”])
background.x, background.y = 384, 512
background.xScale = 0.45714285714285713
background.yScale = 0.9752380952380952
background.isVisible = true

physics.addBody(background, “kinematic”, {density = 1.0, friction = 0.3, bounce = 0.2})

this:insert(background)
this.background = background

local firstblock_1 = display.newImageRect(imagelookup.table[“Firstblock”],imagelookup.table[“FirstblockWidth”],imagelookup.table[“FirstblockHeight”])
firstblock_1.x, firstblock_1.y = 192, 700
firstblock_1.xScale = 1
firstblock_1.yScale = 1
firstblock_1.isVisible = true

physics.addBody(firstblock_1, “kinematic”, {density = 1.0, friction = 0.3, bounce = 0.2})

this:insert(firstblock_1)
this.firstblock_1 = firstblock_1

local nextblock_1 = display.newImage(imagelookup.table[“Nextblock”],imagelookup.table[“NextblockWidth”],imagelookup.table[“NextblockHeight”])
nextblock_1.x, nextblock_1.y = 384, 700
nextblock_1.xScale = 1
nextblock_1.yScale = 1
nextblock_1.isVisible = true
nextblock_1.objectName = “nextblock_1”

physics.addBody(nextblock_1, “kinematic”, {density = 1.0, friction = 0.3, bounce = 0.2})

this:insert(nextblock_1)
this.nextblock_1 = nextblock_1

local lastblock_1 = display.newImage(imagelookup.table[“Lastblock”],imagelookup.table[“LastblockWidth”],imagelookup.table[“LastblockHeight”])
lastblock_1.x, lastblock_1.y = 576, 700
lastblock_1.xScale = 1
lastblock_1.yScale = 1
lastblock_1.isVisible = true

physics.addBody(lastblock_1, “kinematic”, {density = 1.0, friction = 0.3, bounce = 0.2})

this:insert(lastblock_1)
this.lastblock_1 = lastblock_1

local dogbath_1 = display.newImage(imagelookup.table[“Dogbath”],imagelookup.table[“DogbathWidth”],imagelookup.table[“DogbathHeight”])
dogbath_1.x, dogbath_1.y = 192, 100
dogbath_1.xScale = 0.42857142857142855
dogbath_1.yScale = 0.42857142857142855
dogbath_1.isVisible = true
dogbath_1.objectName = “dogbath_1”

physics.addBody(dogbath_1, “kinematic”, {density = 10.0, friction = 1.0, bounce = 0.2})

this:insert(dogbath_1)
this.dogbath_1 = dogbath_1

local function movedogbath_1( event )
dogbath_1.x = event.x
dogbath_1.y = event.y
end
dogbath_1:addEventListener(“touch”, movedogbath_1)

local dirtydog0_1 = display.newImage(imagelookup.table[“Dirtydog0”],imagelookup.table[“Dirtydog0Width”],imagelookup.table[“Dirtydog0Height”])
dirtydog0_1.x, dirtydog0_1.y = 384, 100
dirtydog0_1.xScale = 0.5769230769230769
dirtydog0_1.yScale = 0.5
dirtydog0_1.isVisible = true

physics.addBody(dirtydog0_1, “kinematic”, {density = 1.0, friction = 0.3, bounce = 0.2})

this:insert(dirtydog0_1)
this.dirtydog0_1 = dirtydog0_1

local function movedirtydog0_1( event )
dirtydog0_1.x = event.x
dirtydog0_1.y = event.y
end
dirtydog0_1:addEventListener(“touch”, movedirtydog0_1)

local cleandog_1 = display.newImage(imagelookup.table[“Cleandog”],imagelookup.table[“CleandogWidth”],imagelookup.table[“CleandogHeight”])
cleandog_1.x, cleandog_1.y = 576, 100
cleandog_1.xScale = 0.3
cleandog_1.yScale = 0.45045045045045046
cleandog_1.isVisible = true

physics.addBody(cleandog_1, “kinematic”, {density = 1.0, friction = 0.3, bounce = 0.2})

this:insert(cleandog_1)
this.cleandog_1 = cleandog_1

function movecleandog_1( event )
cleandog_1.x = event.x
cleandog_1.y = event.y
end
cleandog_1:addEventListener(“touch”, movecleandog_1)

–collision detection
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

print( self.objectName … ": collision began with " … event.other.objectName )

elseif( event.phase == “ended” ) then

print( self.objectName … “: collision ended with” … event.other.objectName )

end
end

dogbath_1.collision = onLocalCollision
dogbath_1:addEventListener ( “collision”, dogbath_1 )

nextblock_1.collision = onLocalCollision
nextblock_1:addEventListener ( “collision”, nextblock_1 )

local ui = require(“ui”)
local nextbutton = ui.newButton{
defaultSrc = imagelookup.table[“Go”], defaultX = imagelookup.table[“GoWidth”], defaultY = imagelookup.table[“GoHeight”],

}
nextbutton.x, nextbutton.y = 384, 925
nextbutton.isVisible = true

physics.addBody(nextbutton, “static”, {density = 1.0, friction = 0.3, bounce = 0.2})

this:insert(nextbutton)
this.nextbutton = nextbutton

return this
end
[import]uid: 32253 topic_id: 7374 reply_id: 26283[/import]

ok, I made some changes, but I am wondering how to handle the collision listeners as I cant get the sound to play. I have attached a snippet of the code, but basically it is the same except for the sounds and object names. Should I use a global listener or local? Thanks!

[code]–Main function
function new()
local localGroup = display.newGroup()

–Background
local background = display.newImage( “star splash 2.png”)
localGroup:insert(background)

–load sound
local cheerSound = audio.loadSound( “cheering.mp3” )
local chainsaw = audio.loadSound( “chain saw.mp3” )
local river = audio.loadSound( “river.mp3” )

–first group
local boy = display.newImageRect( “1 copy.png”, 150, 150)
boy.x = 650
boy.y = 100
physics.addBody( boy, “dynamic”, {density=3.0, friction=0.5, bounce=0.2})
boy.isFixedRotation = true
localGroup:insert(boy)
boy.myName = “boy”

–drag
local function moveBoy(event)
boy.x = event.x
boy.y = event.y
end
boy:addEventListener(“touch”, moveBoy)

local target = display.newImageRect( “next block.png”, 140, 140)
target.x = 380
target.y = 800
physics.addBody( target, “dynamic”, {density=3.0, friction=0.5, bounce=0.2})
target.isFixedRotation = true
localGroup:insert(target)

target.myName = “target”

local function ontargetCollision(self, event)
if ( event.phase == “began”) then
if event.other.myName == “target” and event.other.myName == “boy” then

audio.play(cheering)
end
end
end

boy.collision = onboyCollision
boy:addEventListener( “collision”, boy )

target.collision = ontargetCollision
target:addEventListener( “collision”, target )
[import]uid: 32253 topic_id: 7374 reply_id: 29742[/import]

Your play sound in your collisiin is wrong. It should be



audio.play(cheerSound)
[import]uid: 12455 topic_id: 7374 reply_id: 29961[/import]