相关疑难解决方法(0)

在我的Java Game项目中过度使用static?

我目前正在开发一个Java平台,我为它编写了自己的游戏引擎Bonsai.现在我问自己一个问题"我是否过度使用静力学?".

一方面它非常方便,因为我不必像地图或播放器那样在每个类中保持对游戏实例的引用.另一方面......我已经不得不剥离applet支持,因为那里的所有静态内容非常错误.

所以我的问题是,既然你可能比我更有经验的Java程序员,我应该摆脱所有静态吗?如果是的话,什么才能成为这样的有效方法:

public void draw(Graphics2D) {
  if (this.game.time() > this.timer) {
    this.game.image.draw(this.tiles[this.game.animation.get("tileAnim")], x, y, null)
  }
}
Run Code Online (Sandbox Code Playgroud)

代替:

public void draw(Graphics2D) {
  if (Game.time() > this.timer) {
    Image.draw(this.tiles[Animation.get("tileAnim")], x, y, null)
  }
}
Run Code Online (Sandbox Code Playgroud)

甚至在地图编辑器中更糟糕:

   public void control() {
     if(this.map.game.input.keyPressed(...)) {
       this.map.game.sound.play(...);
     }
   }
Run Code Online (Sandbox Code Playgroud)

编辑
根据答案我决定有一个GameObject类,它为每个组件提供包装器方法.地图,播放器等然后从它的子类,这样我所有this.game调用都隐藏在场景后面,它仍然在前端看起来很好:

public class GameObject {
    private Game game;

    public GameObject(Game g) {
        game = g;
    }

    public Game Game() {
        return game;
    }

    public GameAnimation Animation() {
        return game.animation;
    }

    public GameInput Font() …
Run Code Online (Sandbox Code Playgroud)

java static

4
推荐指数
1
解决办法
2763
查看次数

标签 统计

java ×1

static ×1