LibGDX - 如何减慢update()方法?

时间:2015-05-25 20:19:02

标签: java libgdx 2d-games

我正在尝试在LibGDX中重新创建太空入侵者来控制游戏开发但是当我尝试设置每个敌人(现在他们只是正方形)按顺序移动时,update()方法会让他们改变他们的帖子方式太快了。有没有办法减慢整个项目的渲染速度或任何其他正确的方法来解决这个问题?我还尝试在Timer类中处理移动并对其进行策划,但它导致内存过载,并且不会在每个对象的同时触发线程。

  package regularmikey.objects;

  import java.util.Timer;
  import java.util.TimerTask;

  import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;

  public class Aliens {

public float x;
public float y;
public float fx;
public float fy;
public int step_count = 0;

public Aliens(float x, float y) {
    this.fx = this.x = x;
    this.fy = this.y = y;


};


public void update(float dt){

    if(step_count == 10) { 
        step_count = 0;
        y = y - 1;
        x = fx; 

    }
    x = x + 3;
    step_count++;
 };

PlayState类

package regularmikey.gamestates;

import java.util.ArrayList;

import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import regularmikey.managers.GameStateManager;
import regularmikey.objects.Aliens;
import regularmikey.objects.Bullet;
import regularmikey.objects.Player;

public class PlayState extends GameState {

private Player player;
private ArrayList<Bullet> bullets;
private ArrayList<Aliens> aliens;
private ShapeRenderer sr;

public PlayState(GameStateManager gsm) {

    super(gsm);

}

@Override
public void init() {
    sr = new ShapeRenderer();
    bullets = new ArrayList<Bullet>();
    aliens =  new ArrayList<Aliens>();
    player = new Player(bullets);
    spawnAliens();




}

@Override
public void update(float dt) {
    player.update(dt);

    for(int i = 0; i < aliens.size(); i++) {
        aliens.get(i).update(dt);
    }



}

public void spawnAliens() {
    aliens.clear();



    float i, j;



    for(j = 100; j <= 510; j = j + 45) {
        for(i = 250; i <= 445; i =  i + 45) {

            aliens.add(new Aliens(j, i));
        }

    }




}

@Override
public void draw() {
    player.draw(sr);



    for(int i = 0; i < aliens.size(); i++) {
        aliens.get(i).draw(sr);

    }



}

@Override
public void handleinput() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}
}


public void draw(ShapeRenderer sr) {
    sr.setColor(1, 1, 1, 1);
    sr.begin(ShapeType.Line);
    sr.rect(x, y, 30, 30);
    sr.end();


};


}

1 个答案:

答案 0 :(得分:3)

此处的关键是与dt方法一起传递的update()变量,该方法是Delta Time的缩写。

Delta时间是游戏开发中常用的因素,表示自上一帧开始以来经过的时间。

我无法确切地说明你让外星人移动的方式,但通常的方法是让游戏中的实体顺利移动(无论帧速率如何):

void update(float deltaTime){
    this.position = currentPosition + (this.movementSpeed × deltaTime);
}

这并未考虑实体移动的方向等。但这就是这个例子的重点。

所以,在你的情况下,你可以这样:

package regularmikey.objects;

  import java.util.Timer;
  import java.util.TimerTask;

  import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;

  public class Aliens {

public float x;
public float y;
public float fx;
public float fy;
public int step_count = 0;

private float moveTimer;
private float moveTreshold;

public Aliens(float x, float y) {
    this.fx = this.x = x;
    this.fy = this.y = y;

    moveTimer = 0; // This will act as a stopwatch
    moveTreshold = 3000; // Alien will move once it passes this treshold
};


public void update(float dt){
    // check whether we passed the treshold for moving or not
    if(moveTimer += dt; > moveTreshold){
      if(step_count == 10) { 
          step_count = 0;
          y = y - 1;
          x = fx; 

      }
      x = x + 3;
      step_count++;

      moveTimer = 0; // reset the timer
    }
 };