gpt4 book ai didi

java - 改变纹理的高度使纹理出现在地面之上

转载 作者:行者123 更新时间:2023-11-30 05:50:11 27 4
gpt4 key购买 nike

我想将纹理的高度更改为随机高度(具有指定的范围),并且我几乎想出了如何操作,但我遇到的问题是纹理(塔)现在高于地面。我想拉伸(stretch)纹理,同时它保持在相同的位置,但改变高度长度。

enter image description here

我有一个Tower类和a Scrollable类(class)。在 Tower I 类在 reset 中生成随机高度方法,但问题是我不知道到底需要添加或写入什么才能将纹理放在正确的位置(以便它不在地面之上)。

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

}

最佳答案

您将塔画得太高了,您需要添加一半高度而不是整个高度。

这里是drawTowers():

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

对其他塔进行同样的操作。这可能不完全正确,但应该相差不远。

关于java - 改变纹理的高度使纹理出现在地面之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54040248/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com