vendredi 4 janvier 2019

Changing height of texture makes texture appear above the ground

I want to change the height of my texture to a random height (with a specified range), and I almost figured out how, but the problem I have is that the texture (tower) is now above the ground. I want to stretch the texture while it remains on the same position, but changes height length.

enter image description here

I have a Tower class and a Scrollable class. In the Tower class I generate a random height in the reset method, but the problem is that I don't know what exactly I have to add or write to put the texture on the correct position (so that it isn't above the ground).

Here is the Tower class:

public class Tower extends Scrollable {

    private Random r;

    // When Tower's constructor is invoked, invoke the super (Scrollable)
    // constructor
    public Tower(float x, float y, int width, int height, float scrollSpeed) {
        super(x, y, width, height, scrollSpeed);
        // Initialize a Random object for Random number generation
        r = new Random();
    }

    @Override
    public void reset(float newX) {
        // Call the reset method in the superclass (Scrollable)

        super.reset(newX); // newX
        // Change the height to a random number

        Random r = new Random();
        int low = 0;
        int high = 15;
        int result = r.nextInt(high-low) + low;
        height = result;    
    }    
}

And here's the 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;
    }    
}




Aucun commentaire:

Enregistrer un commentaire