jeudi 3 janvier 2019

Java libGDX - Problems with changing texture size randomly at runtime

I'm programming my own version of "FlappyBird", but I stumbled upon some problems.

I have four different textures (towers), which should have a random height (with a specified range), but the problems I'm facing are:

  • If I generate the random numbers outside my drawTowers() method, then then all towers have different heights, but the height does not change
  • If I generate the random numbers inside my drawTowers() method, then a lot of towers with different height are shown while changing every millisecond

Here is my drawTowers() method:

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()));

}

I also have the same problem for the gap between two towers. I want the gap to have a random range (with specified minimum and maximum). So I used two classes: A ScrollHandler and a Scrollable.

This is what my ScrollHandler class looks like:

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);
    }
}
}

And here is my Scrollable class:

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;
}

I don't know what exactly I have to change to get a random height and random gap every time it appears again. Maybe I have to work with the texture itself? Thanks in advance!

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




Aucun commentaire:

Enregistrer un commentaire