0

Sorry if the title is a little... vague. Anyway, I'm making a platformer using java and I've been trying to create a pressure plate triggered dispenser that shoots poisoned darts. The game is pretty much split up into a grid system, with each block being a spot on the grid. so far I've made it so that when the pressure plate is activated, it looks as if it's pressed down. I've also been able to draw it to the screen but it does not move or anything. This is the code I use to draw it to the screen:

public void drawDart(Graphics g) {
    if(Component.isPressed) {
        for(int x = 0; x < block[0].length; x++) {
            for (int y=0; y < block[0].length; y++) {
                if(x == 91 && y == 35) {
                    block[x][y] = new Block(new Rectangle(x * Tile.tileSize, y * Tile.tileSize, Tile.tileSize, Tile.tileSize), Tile.poisonDart); 
                }
            }
        }
    }
}

Unfortunately, with this code when I step on the pressure plate, the dart is drawn, but will not move. I've tried using x++ before but that doesn't move the dart at all.

Another problem is that I would like the dart to look like it's affected by gravity, which is hard to do when the game is split into a large grid system. Either way, what do you guys think?

Moderator edit (moved from OP comment):

block[x][y] = new Block(new Rectangle(x * Tile.tileSize, y * Tile.tileSize,
    Tile.tileSize, Tile.tileSize), Tile.poisonDart); 
4

1 回答 1

1

你是否为你的游戏实现了“游戏循环”?它看起来像这样(伪代码):

while (gameRunning) {
  for (GameEntity e in gameWorldEntities) {
    e.update(timePassed);  
  }
  for (GameEntity e in gameWorldEntities) {
    e.draw();
  }
}

每个 dart 对象都应该知道它在游戏世界中的位置,并且 Dart 类的 update() 方法应该更新它的位置,包括在它被发射时水平移动它并使其由于重力而下落。除非您使用单独的物理引擎,否则跟踪其水平和垂直速度是一个好主意,听起来您不是。然后 draw() 方法只需要将它绘制在屏幕上正确的位置。

于 2012-10-10T22:06:38.327 回答