【发布时间】:2015-04-17 13:23:30
【问题描述】:
所以我正在创建一个精灵并使用不同的类将其绘制到屏幕上。
我现在想在每个屏幕上将其缩放为相同的纵横比/尺寸。
我已经尝试使用以下代码来做到这一点:
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 x 100。但这也不起作用。
感谢您的帮助! :)
编辑:添加基类和派生类:
这是 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) {
..............///
【问题讨论】:
-
Player是Actor的子类吗?如果是,您可以使用setSize()。我不知道 LibGDX API 中有任何公开的width或height字段,所以我猜你正在修改你定义的一些字段。在这种情况下,您不能指望 LibGDX 知道您更改了这些字段的值。 -
Player 是我创建的另一个类“MoveEntity”的子类 这是 MoveEntity 类的代码: package com.fam.entities; public MoveEntity(float speed, float rotation, float width, float height, Vector2 position) { super(position, width, height); this.speed = 速度; this.rotation = 旋转; vel = new Vector2(0, 0); } public Vector2 getVel() { return vel; } public void setVel(Vector2 target) { this.vel = target; } 公共浮动 getRotation() { 返回旋转; } 公共无效 setRotation(float r) }
-
而 MoveEntity 是“实体”类的子类,它具有以下代码: public abstract class Entity { public void setPosition(Vector2 position) { this.position = position; } 公共浮动 getWidth() { 返回宽度; } public void setWidth(float width) { this.width = width; } 公共浮动 getHeight() { 返回高度; } public void setHeight(float height) { this.height = height; } 公共矩形 getBounds() { 返回边界; } public void setBounds(Rectangle bounds) { this.bounds = bounds; } }
-
然后你需要告诉 libGDX 来渲染你的对象。请阅读doc 了解如何执行此操作。
-
是的,我看不出与我拥有的渲染代码有任何区别,即:public void render() { p = world.getPlayer(); cam.position.set(p.getPosition().x, p.getPosition().y, 0); sb.begin(); sb.draw(spr_player, p.getPosition().x, p.getPosition().y); sb.end(); cam.update();移动播放器(); }
标签: java libgdx sprite scaling