Page 1 of 1

Objects touching

Posted: Thu Apr 17, 2014 2:05 pm
by GloriousDerp
I need help trying to play a sound when objects hit something.

Code: Select all

	
	if objects.box == objects.box2 then
		love.audio.play(sound)
	end

	objects.box = {}
	objects.box.body = love.physics.newBody(world, 160, 300, "dynamic")
	objects.box.shape = love.physics.newRectangleShape(50, 100)
	objects.box.fixture = love.physics.newFixture(objects.box.body, objects.box.shape)
	objects.box.fixture:setRestitution(1)
	objects.box.body:setFixedRotation(true)
	
	objects.box2 = {}
	objects.box2.body = love.physics.newBody(world, 880, 300, "dynamic")
	objects.box2.shape = love.physics.newRectangleShape(50, 100)
	objects.box2.fixture = love.physics.newFixture(objects.box2.body, objects.box2.shape)
	objects.box2.fixture:setRestitution(1)
	objects.box2.body:setFixedRotation(true)
	

Re: Objects touching

Posted: Thu Apr 17, 2014 7:21 pm
by Autophoenix
Create a variable in love.load(). Call it 'playsound', for example.

Code: Select all

function love.load()
        playsound = false
end

And then, in update, remove love.audio.play(sound). Replace it with this:

Code: Select all

if objects.box == objects.box2 then
        playsound = true
end
To play the sound:

Code: Select all

function love.draw()
        if playsound == true then
                love.audio.play(sound)
                playsound = false
        end
end

Re: Objects touching

Posted: Thu Apr 17, 2014 7:26 pm
by Autophoenix
PS: To play the sound multiple times, read this:

viewtopic.php?f=4&t=7706

Re: Objects touching

Posted: Thu Apr 17, 2014 9:58 pm
by LuaWeaver
There's an edit button for a reason. :)