2015-04-17 86 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