开发者

Java 2D Game Question

开发者 https://www.devze.com 2023-04-04 02:30 出处:网络
I\'m creating my first game in Java. It is a simple 2D top-down shooter game. I want to have the camera follow my character around, so, us开发者_运维知识库ing Canvas, how do I make it so the camera fo

I'm creating my first game in Java. It is a simple 2D top-down shooter game. I want to have the camera follow my character around, so, us开发者_运维知识库ing Canvas, how do I make it so the camera follows the character exactly in the center. While the map is moving when I hit the arrow keys.


One possibility for the logic is such:

  • Create a listener and attach it to your character (either directly or indirectly depending on your design). Listen for character movement, or position changes.
    • Directly meaning that your character class has an addListener method.
    • Indirectly meaning that your character class posts events to a global event bus, with which the listeners register to receive events of particular types.
  • Calculate the camera position based on the character position & center it accordingly. This will require a little math!

By implementing this using an event system you can easily separate the logic that centers the camera from the logic that moves the character. This way you can have the arrow keys move or you can use click to move and the camera logic will be unaffected.

Sorry I cannot provide general help regarding the actual Java 2D and Canvas calls that you might need to make, but I hope that a broad example of how one might go about doing this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号