Log In  


At the moment I am trying to make a sound play once during a collision.

The collision doesn't stop player speed, but rather the player can "run" through the collision box.
This means that the collision function returns true for several frames in the game.

Whilst can get a collision to play a sound, because the function is returning true for several frames the sound bite keeps restarting every frame.

Could someone please explain how I could make it so that

Whilst collision = True
Sfx plays all the way through(only once though)

I am currently trial and erroring my way through this, but I wondered if I could cut that a little shorter by asking a question on here. I did try searching online for tutorials regarding this but they either make a sound effect play when a button is pressed, or something that doesn't necessarily apply to a situation like this.

Thanks.

1


flip a variable ‘colliding’ when entering collision state, flip it back to false when exiting.
fire the sound only in case of collision and ‘colliding’ is false (eg a new collision).


@freds72 thanks alot for your quick response, it works!.

Sorry for the late reply by the way.


Can someone elaborate on @freds72 's suggestion? I'm struggling to implement sfx that play a single time upon ground collision in my platformer.

Thank you and sorry for the bump.


So I got it to work but I don't understand the logic behind it.

local v=mget((p1.x+4)/8,(p1.y+8)/8)

	if p1.dy>=0 then
		if fget(v,0) then
			p1.y = flr((p1.y)/8)*8
			p1.dy = 0
			if not p1.isgrounded then 
				sfx(2)
				p1.isgrounded=true
			end
		else p1.isgrounded=false
		end
	end

Why does telling the game to play the sound effect while checking that the player character isn't grounded play the sound effect upon impact?


1

@8bit_gnosis you can think of it as "the player is not grounded yet" so in the instance that it has become grounded, we play the sound at the same time we flag the player as grounded. So, the next frame we don't enter this block as they are now flagged as grounded. This continues until the player leaves the ground.



[Please log in to post a comment]