Libgdx java精灵大小问题
因此,我创建了一个精灵,并使用另一个类将其绘制到屏幕上 现在,我想将其缩放为每个屏幕上相同的纵横比/大小 我已尝试使用以下代码来实现这一点:Libgdx java精灵大小问题,java,libgdx,sprite,scaling,Java,Libgdx,Sprite,Scaling,因此,我创建了一个精灵,并使用另一个类将其绘制到屏幕上 现在,我想将其缩放为每个屏幕上相同的纵横比/大小 我已尝试使用以下代码来实现这一点: public Player(Vector2 position, float width, float height, float rotation, float speed) { super(speed, rotation, width, height, position); } public void update() {
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。但这也不起作用
谢谢你的帮助!:)
编辑:添加基类和派生类:
以下是实体类的代码:
以下是MoveEntity类的代码:
也许它不会说英语,但你说的是一个精灵,我没有看到他的任何网站,无论如何 假设这是您所指的精灵->
spr\u player
您可以通过多种方式来实现这一点,但要基于您发布的这一行代码
变量类。(用于测试):
尝试使用以下方法:
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);
这是Sprite类中的功能:
public void setSize (float width, float height)
public Sprite (Texture texture, int srcWidth, int srcHeight)
如果大小在游戏期间不会改变,您可以在创建或显示的方法中调用setSize,或者在构造函数中以这种方式调用setSize,如果您希望得到以下结果:
Sprite spr_player = new Sprite (your_texture, width, height);
这是Sprite类中的功能:
public void setSize (float width, float height)
public Sprite (Texture texture, int srcWidth, int srcHeight)
Player
是Actor
的子类吗?如果是,您可以使用setSize()
。我不知道libgdxapi中有任何公共的width
或height
字段,所以我猜您正在修改您定义的一些字段。在这种情况下,您不能期望LibGDX知道您更改了这些字段的值;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)}和MoveEntity是类“Entity”的一个子类,该类具有以下代码:public抽象类Entity{public void setPosition(Vector2 position){this.position position=position;}public float getWidth(){return width;}public void setWidth(浮动宽度){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;}然后你需要告诉libGDX渲染你的对象。请阅读关于如何做的说明。是的,我看不出与我的渲染代码有什么区别,那就是: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();movePlayer();}我尝试过这个,但是现在当我尝试运行这个程序时,我根本看不到播放器。这只是我的背景,这是代码:初始化:p=newplayer(playerVec,100100,0,0);渲染:sb.begin();sb.draw(spr_player,p.getPosition().x,p.getPosition()).y,p.getWidth(),p.getHeight();sb.end();从未开采过,我修好了!:)非常感谢兄弟。我只是用你的方法画出来,我不小心把球员的宽度/高度设置为1。
public Sprite (Texture texture, int srcWidth, int srcHeight)