0 Favourites

# How do I fade music towards the end of a layout?

• 8 posts
• A friend and I are making a side-scrolling/platforming RPG. We've been spending a lot of time writing original music for each unique area of the game, so implementation of the music is pretty important to us.

I think it would be really cool if the music played at a consistent volume throughout a layout, but gradually faded out as the player reached the end of said layout. It would be even better if the player then started on the next layout in silence, and the new music for that layout gradually faded in as they walk forwards.

Sorry if that's a bit confusing or needs some additional clarification.

Has anyone done anything like this? Or have any ideas as to how we could implement this? Thank you so much in advance!!!

• volume = distance from player to end of layout / distance between player and end of layout to start fading.

You can use an invisible object to control this distance and use it's width when calculating

• caiorosisca That's interesting! I've been toying with invisible objects to do this a little bit. I'm pretty new to Construct, so I'm not entirely sure I grasp everything you're saying. How would you suggest using the object's width in the calculation? Thank you!

• volume = abs(Player.X - LayoutWidth) / greenBox.width

Grey is layout size(pretend it on my img).

Blue is at the position where it ends, represented by LayoutWidth.

using this formula when the player is at the left side of the greenbox (this is the region the player needs to be inside for the volume to fade) volume will be 1, and when at the right side volume will be 0.

Note, volume will be 0 exactly at the end of the greenbox, where the blue line is. if the player moves further to the right volume will start going up again. so it's important that it is at the end of layout.

• caiorosisca Ok awesome! That works exactly how you described! The only problem for me now is, I'm using the XAudio2 object, which has max volume set as 0, and the minimum set as -100. So instead of fading from 1 to 0, I need to fade from 0 to -100. I know I'm an idiot for asking, but I'm not very great at math... Haha! How would you adjust the formula to do that?

• ## Buy Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• I'm not good at maths either, but I think that is the formula your are after. Give it a try, if it doesn't work search for the equation of the line

vol = (greenBox.width*100 + -100 * abs(Player.X - LayoutWidth))/-greenBox.width

• caiorosisca No way, it worked!!!! With a couple extra parenthesis. You're far better at math than I am!!!! Thank you so much!

• caiorosisca Sorry to be bothersome, but I have one more question about this. I'm trying to make it so the "green box" doesn't need to be placed exactly at the end of the layout. I've been trying to fix the equation so the distance between the right edge of the "green box" and an object placed at the right edge of the layout is subtracted from the equation. I've been working on this for quite a while, but I'm falling up short somehow. If you could help me with this I would be soooooooooooooooo thankful!

• 8 posts