2015-04-17 88 views
-1

所以我創建一個精靈並使用不同的類將其繪製到屏幕上。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) { 
    ............../// 
+1

是''Player'的Actor'一個子類?如果是,你可以使用'setSize()'。我不知道LibGDX API中的任何公共'width'或'height'字段,所以我的猜測是你正在修改你定義的一些字段。在這種情況下,你不能指望LibGDX知道你改變了這些字段的值。 –

+0

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

+0

而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

回答

1

也許它不會說英語,但你談論一個精靈,反正我也沒有看到他有任何網站。

假設這是你提到的精靈 - >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的funtion:

public void draw (Texture texture, float x, float y, 
        float width, float height) 

也可以使用,的setSize在Sprite類中,在渲染之前的某個地方。

spr_player.setSize(width, height); 

這是在類雪碧的funtion:

public void setSize (float width, float height) 

如果大小將不會在遊戲過程中可能發生變化,你可以調用的setSize,在創建方法或節目,或在構造函數中調用這個方法,如果你想要的結果:

Sprite spr_player = new Sprite (your_texture, width, height); 

這是在類雪碧funtion:

public Sprite (Texture texture, int srcWidth, int srcHeight) 
+0

我試過了,但現在當我嘗試運行程序時,我根本看不到球員。這只是我的背景,這是代碼:初始化:p = new Player(playerVec,100,100,0,0);渲染:sb.begin(); \t \t \t \t sb.draw(spr_player,p.getPosition()。x,p.getPosition()。y,p.getWidth(),p。的getHeight()); \t \t \t \t sb.end(); – Dumbostyle

+0

從未開採,我修好了! :)非常感謝兄弟。我只是不得不使用你的方法來繪製它,我意外地把玩家的寬度/高度設置爲1。 – Dumbostyle