Objects touching

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
GloriousDerp
Prole
Posts: 6
Joined: Tue Feb 25, 2014 10:52 pm

Objects touching

Post 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)
	
User avatar
Autophoenix
Prole
Posts: 40
Joined: Thu Mar 06, 2014 4:18 pm
Location: Rio de Janeiro, Brazil

Re: Objects touching

Post 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
a² = b² + c²
User avatar
Autophoenix
Prole
Posts: 40
Joined: Thu Mar 06, 2014 4:18 pm
Location: Rio de Janeiro, Brazil

Re: Objects touching

Post by Autophoenix »

PS: To play the sound multiple times, read this:

viewtopic.php?f=4&t=7706
a² = b² + c²
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

Re: Objects touching

Post by LuaWeaver »

There's an edit button for a reason. :)
"your actions cause me to infer your ego is the size of three houses" -finley
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 32 guests