javajavafx-2java-canvas

Can you make a Canvas scroll to the left


So I'm making a basic 2D game, and I'm using paths to move the player to a certain point. I am restricted to a window size of 800 x 600, and I want the path to finish at the X coordinate of 1400. How would I make the canvas scroll along to the left as the path goes continues passed the 800 X point.

Another solution I've thought of would be to make the Path take longer and just have the background scroll to give the illusion of what I am trying to achieve. However I would still prefer the first option.


Solution

  • There are two solutions:

    1. You can wrap your canvas in a ScrollPane. Note that you can hide the scrollbars and scroll the viewport programatically (for example to keep the player character centered at all times).

    2. You can refresh the canvas every time the player moves and render everything relative to a "world origin". Use g.translate(-world.x,world.y) before any other rendering command to do this. Note the negative X coordinate.