因此,我正在创建一个精灵,并使用其他类将其绘制到屏幕上。
我现在想将其缩放为每个屏幕上相同的宽高比/尺寸。
我尝试通过使用以下代码来做到这一点:
public Player(Vector2 position, float width, float height, float rotation, float speed)
{
super(speed, rotation, width, height, position);
}
public void update()
{
width = Gdx.graphics.getWidth() / 10;
height = Gdx.graphics.getHeight() / 10;
}
但这是行不通的,它什么也没做。
我也尝试将宽度和高度设置为静态值,例如100 x100。但这也不起作用。
感谢您的任何帮助!:)
编辑:添加基类和派生类:
这是Entity类的代码:
public abstract class Entity {
public void setPosition(Vector2 position) {
this.position = position;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public Rectangle getBounds() {
return bounds;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
}
这是MoveEntity类的代码:
public MoveEntity(float speed, float rotation,
float width, float height,
Vector2 position) {
super(position, width, height);
this.speed = speed;
this.rotation = rotation;
vel = new Vector2(0, 0);
}
public Vector2 getVel() {
return vel;
}
public void setVel(Vector2 target) {
this.vel = target;
}
public float getRotation() {
return rotation;
}
public void setRotation(float r) {
..............///
也许它不会说英语,但是您谈论的是一个精灵,无论如何我都看不到他。
假设这是您要引用的精灵-> spr_player
您可以通过几种方式来执行此操作,但是要基于发布的这一行代码
变量类。(用于测试):
float width = Gdx.graphics.getWidth() / 10;
float height = Gdx.graphics.getHeight() / 10;
尝试使用此:
sb.draw(spr_player, p.getPosition().x, p.getPosition().y
width, height);
这是SpriteBatch类中的功能:
public void draw (Texture texture, float x, float y, float width, float height)
您也可以在渲染之前的某个位置使用Sprite类中的setSize。
spr_player.setSize(width, height);
这是Class Sprite中的功能:
public void setSize (float width, float height)
如果大小可能不会在游戏中改变,则可以在创建或显示的方法中调用setSize,或者在构造函数中调用setSize,如果需要的话:
Sprite spr_player = new Sprite (your_texture, width, height);
这是Class Sprite中的功能:
public Sprite (Texture texture, int srcWidth, int srcHeight)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句