If a variable decreases how to increase another problem

HI,

I havnt posted anything for a while as things have been going good. However I had this problem way back and just skirted around it, now is the time I need it to work, I will do my best to explain.

I have a scrolling background.

I have a player.

As the player moves left to right the background scrollSpeed is increased according to where the player.x postion is.

So as the player.x increases so does the scrollSpeed of the background, as the player.x decreases the scrollSpeed slows down.

All working nicely.

Now I have a button that reverses the player and also reverses the background, working fine.

The background now scrolls right to left, BUT as the player moves further left the scrollSpeed decreases because the player.x is decreasing. The scrollSpeed needs to increase now as the players.x decreases.

SO how do I do this ?

I could write a load of if thens and say if the playerSpeed decreases increase the scrollspeed, but this would have jerky results; I have tried this it does work but there has to be a better way.

Here is the barebones of the code(logic), I have tried to solve for hours :frowning: but not getting to far so any help would be most welcome.

[lua]

 function updateTerrain()

        if switchTerrain == 0 then

            if player.x <= 100 then scrollSpeedX = 0

            else
            scrollSpeedX = -player.x /70  – dividing by 70 is used to slow the terrain down to an acceptable level –
            end

— code here for movement / removed for clarity —

elseif switchTerrain == 1 then

            –
              if player.x <= 380 then scrollSpeedX = 0

             else
            scrollSpeedX = player.x / 70   – player is now moving opposite direction but scrollspeed is wrong
             end                                        – needs to increase as player.x is decreased

[/lua]

now I could it llike this  but this seems very wrong, there has to be a more elegant solution so the increments can be smooth.

[lua]

elseif switchTerrain == 1 then
          
            if player.x > 320 then terrain.x = terrain.x +1  end
            if player.x > 270 and  player.x < 320 then terrain.x = terrain.x +1.5  end
            if player.x > 220 and  player.x < 270 then  terrain.x = terrain.x +2  end
            if player.x > 170 and  player.x < 220 then  terrain.x = terrain.x +2.5  end

[/lua]

[lua]

local updateTerrain()

      local playerTravel

      local multiplier

      if switchTerrain == 0 then

            playerTravel = player.x

            multiplier = - 1

      else

            playerTravel = 480 - player.x

            multiplier = 1

      end

      if playerTravel <= 100 then

            scrollSpeedX = 0

      else

            scrollSpeedX = (playerTravel / 70 ) * multiplier

      end

           

end

[/lua]

NIck,

thank you so much for that, it works a treat, I had been bashing my head on this for an age.

Now I see it , its self explanatary, great stuff, good lesson :slight_smile:

No problem :slight_smile:

[lua]

local updateTerrain()

      local playerTravel

      local multiplier

      if switchTerrain == 0 then

            playerTravel = player.x

            multiplier = - 1

      else

            playerTravel = 480 - player.x

            multiplier = 1

      end

      if playerTravel <= 100 then

            scrollSpeedX = 0

      else

            scrollSpeedX = (playerTravel / 70 ) * multiplier

      end

           

end

[/lua]

NIck,

thank you so much for that, it works a treat, I had been bashing my head on this for an age.

Now I see it , its self explanatary, great stuff, good lesson :slight_smile:

No problem :slight_smile: