Background music

  • #10, by afrlmeTuesday, 23. April 2013, 18:13 11 years ago
    it is possible, the way Esmeralda mentioned - a bit more effort involved & somewhat less accurate but very do-able smile

    Imperator

    7278 Posts


  • #11, by SimonSThursday, 25. April 2013, 15:40 11 years ago
    Let me help you out with the math (put this into a mainLoop function) :

      local point=getObject("Game.GameCurrentCharacter"):getPoint(VCharacterPosition)
      local point2={x=500,y=500} // point where is maximum volume
      local distance=math.sqrt(math.pow(math.abs(point.x-point2.x),2)+math.pow(math.abs(point.x-point2.x),2))
      local perc = 100*(1-distance/1000) //1000 is the pixels distance where the volume should be 0
      setVolume(eMusicVolume,math.max(math.min(perc,100),0))
    


    You could also do it only on the horizontal axis, what in my opinion would be more likely to be used.

      local point=getObject("Game.GameCurrentCharacter"):getPoint(VCharacterPosition)
      local point2={x=500,y=500}
      local distance=math.abs(point.x-point2.x)
      local perc = 100*(1-distance/1000) //1000 is the pixels distance where the volume should be 0
      setVolume(eMusicVolume,math.max(math.min(perc,100),0))
    


    Keep in mind to reset the music volume once your player leaves the scene. Also if you have a volume setting in your options this would be overwritten. In that case you should copy your global volume to a value and multiply with that in setVolume wink

    Thread Captain

    1580 Posts

  • #12, by afrlmeThursday, 25. April 2013, 16:04 11 years ago
    bloody nora mate, that math isn't half confusing!

    * edit: when using functions & especially mainLoop handler, you should add all variables into a temporary table as it creates a new table entry each time you call the function which gets saved (the mainLoop handler gets called 100's of times or so in a second & hence you would be creating a load of unnecessary table entries - something like that)

    Balance should be added to the equation too - for x axis only mind. (you could do that in 2 ways: the whole scene width or the current viewable part of the scene which is whatever the games default resolution is)

    Imperator

    7278 Posts