所以我創建一個精靈並使用不同的類將其繪製到屏幕上。Libgdx java精靈大小問題
我現在想要在每個屏幕上將其縮放爲相同的長寬比/大小。
我試圖這樣做,通過使用此代碼:
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的靜態值100。但這也不起作用。
謝謝你的幫助! :)
編輯:加入基地和派生類:
下面是實體類的代碼:
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) \t { \t \t super(position,width,height); \t \t \t \t this.speed = speed; \t \t \t \t this.rotation = rotation; \t \t \t \t vel = new Vector2(0,0); \t} \t \t 公共Vector2 getVel() \t { \t \t返回VEL; \t} \t \t 公共無效setVel(Vector2目標) \t { \t \t this.vel =目標; \t} \t \t 公共浮動getRotation() \t { \t \t返回轉動; \t} \t \t公共無效setRotation(浮動R) \t } – Dumbostyle
而MoveEntity是子類的類 「實體」 具有這種代碼: 公共抽象類實體 { \t公共無效setPosition兩種(Vector2位置) \t { \t \t this。position = position; \t} \t public float getWidth(){ \t \t return width; \t} \t公共無效setWidth(浮動寬度) \t { \t \t this.width =寬度; \t} \t public float getHeight(){ \t \t return height; \t} \t公共無效自動調用setHeight(浮動高度) \t { \t \t this.height =高度; \t} \t公共矩形的getBounds(){ \t \t返回界限; \t} \t公共無效的setBounds(矩形範圍) \t { \t \t this.bounds =界限; \t} } – Dumbostyle