【问题标题】:Changing height of texture makes texture appear above the ground改变纹理的高度使纹理出现在地面之上
【发布时间】:2019-05-31 02:27:53
【问题描述】:

我想将纹理的高度更改为随机高度(具有指定范围),我几乎想通了,但我遇到的问题是纹理(塔)现在位于地面之上。我想在纹理保持在相同位置时拉伸纹理,但会改变高度长度。

我有一个 Tower 类和一个 Scrollable 类。在Tower 类中,我在reset 方法中生成一个随机高度,但问题是我不知道我必须添加或写入什么才能将纹理放在正确的位置(所以它不是t 高于地面)。

这是Tower 类:

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

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

也许重要的是要知道我有一个 GameRenderer 类,它有一个 drawTowers() 方法,然后在 render() 方法中使用。

这是我的drawTowers() 方法:

private void drawTowers() {

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

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

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

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

}

【问题讨论】:

  • y=0 处的地面吗?

标签: java random libgdx height textures


【解决方案1】:

你把塔画得太高了,你需要增加一半的高度而不是整个高度。

这里是drawTowers()

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

对其他塔做同样的事情。 这可能并不完全正确,但应该不会太远。

【讨论】:

    猜你喜欢
    • 2016-08-28
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多