Java libGDX-运行时随机更改纹理大小的问题

时间:2019-01-03 18:55:44

标签: java random scroll libgdx textures

我正在编写自己的“ FlappyBird”版本,但偶然发现了一些问题。

我有四个不同的纹理(塔),应该具有随机的高度(具有指定范围),但是我面临的问题是:

  • 如果我在drawTowers()方法之外生成随机数,则所有塔的高度都不同,但高度不会改变
  • 如果我在drawTowers()方法中生成随机数,则每毫秒更改时会显示许多不同高度的塔

这是我的drawTowers()方法:

Random r = new Random();
int low = 0;
int high = 25;
int result1 = r.nextInt(high-low) + low;
int result2 = r.nextInt(high-low) + low;
int result3 = r.nextInt(high-low) + low;
int result4 = r.nextInt(high-low) + low;

private void drawTowers() {

    batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight() - result1, 
           tower1.getWidth(), midPointY - (tower1.getHeight())); 


    batcher.draw(AssetLoader.texture2, tower2.getX(), tower2.getY() + tower2.getHeight() - result2,
           tower2.getWidth(), midPointY - (tower2.getHeight()));

    batcher.draw(AssetLoader.texture3, tower3.getX(), tower3.getY() + tower3.getHeight() - result3,
         tower3.getWidth(), midPointY - (tower3.getHeight()));

    batcher.draw(AssetLoader.texture4, tower4.getX(), tower4.getY() + tower4.getHeight() - result4,
            tower4.getWidth(), midPointY - (tower4.getHeight()));

}

两个塔之间的间距也有相同的问题。我希望间隙具有随机范围(具有指定的最小值和最大值)。因此,我使用了两个类:ScrollHandlerScrollable

这是我的ScrollHandler类的样子:

public class ScrollHandler {

private Tower tower1, tower2, tower3, tower4;
public static final int SCROLL_SPEED = -59;

Random r = new Random();
int low = 90;
int high = 110;
int result = r.nextInt(high-low) + low;

public final int TOWER_GAP = result; 

public ScrollHandler(float yPos) {

    tower1 = new Tower(210, 0, 25, 0, SCROLL_SPEED);
    tower2 = new Tower(tower1.getTailX() + TOWER_GAP, 0, 25, 0, SCROLL_SPEED);
    tower3 = new Tower(tower2.getTailX() + TOWER_GAP, 0, 25, 0, SCROLL_SPEED);
    tower4 = new Tower(tower3.getTailX() + TOWER_GAP, 0, 25, 0, SCROLL_SPEED);
}

public void update(float delta) {
    // Update our objects

    tower1.update(delta);
    tower2.update(delta);
    tower3.update(delta);
    tower4.update(delta);

    // Check if any of the pipes are scrolled left,
    // and reset accordingly
    if (tower1.isScrolledLeft()) {
        tower1.reset(tower4.getTailX() + TOWER_GAP);
    } else if (tower2.isScrolledLeft()) {
        tower2.reset(tower1.getTailX() + TOWER_GAP);
    } else if (tower3.isScrolledLeft()) {
        tower3.reset(tower2.getTailX() + TOWER_GAP);
    } else if (tower4.isScrolledLeft()) {
        tower4.reset(tower3.getTailX() + TOWER_GAP);
    }
}
}

这是我的Scrollable课:

public class Scrollable {

protected Vector2 position;
protected Vector2 velocity;
protected int width;
protected int height;
protected boolean isScrolledLeft;

public Scrollable(float x, float y, int width, int height, float scrollSpeed) {
    position = new Vector2(x, y);
    velocity = new Vector2(scrollSpeed, 0);
    this.width = width;
    this.height = height;
    isScrolledLeft = false;
}

public void update(float delta) {
    position.add(velocity.cpy().scl(delta));

    // If the Scrollable object is no longer visible:
    if (position.x + width < 0) {
        isScrolledLeft = true;
    }
}

// Reset: Should Override in subclass for more specific behavior.
public void reset(float newX) {
    position.x = newX;
    isScrolledLeft = false;
}

public boolean isScrolledLeft() {
    return isScrolledLeft;
}

public float getTailX() {
    return position.x + width;
}

public float getX() {
    return position.x;
}

public float getY() {
    return position.y;
}

public int getWidth() {
    return width;
}

public int getHeight() {
    return height;
}

我不知道为再次出现随机的高度和间隙,我到底需要改变什么。也许我必须处理纹理本身?预先感谢!

texture1 = new Texture("png/turm_gryffindor.png");
    gryffindor = new TextureRegion(texture1, 136, 16, 15, 1);

1 个答案:

答案 0 :(得分:0)

在塔重置方法中和在开始时(初始化游戏时)重新创建随机数。这样,当它们移回起点时,它们将具有新的随机高度。