【问题标题】:Libgdx java sprite size issueLibgdx java精灵大小问题
【发布时间】: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) {
    ..............///

【问题讨论】:

  • PlayerActor 的子类吗?如果是,您可以使用setSize()。我不知道 LibGDX API 中有任何公开的 widthheight 字段,所以我猜你正在修改你定义的一些字段。在这种情况下,您不能指望 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


【解决方案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 类中的函数:

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

你也可以在 Sprite 类中使用 setSize,在渲染之前的某个地方。

spr_player.setSize(width, height);

这是 Class Sprite 中的函数:

public void setSize (float width, float height)

如果在游戏过程中大小不会改变,你可以调用setSize,在方法created或show,或者在构造函数中这样调用,如果你想要的结果:

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

这是 Class Sprite 中的函数:

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

【讨论】:

  • 我试过这个,但现在当我尝试运行程序时,我根本看不到播放器。这只是我的背景,这是代码: Initializing: p = new Player(playerVec, 100, 100, 0, 0);渲染:sb.begin(); sb.draw(spr_player, p.getPosition().x, p.getPosition().y, p.getWidth(), p.getHeight()); sb.end();
  • 没挖过,我修好了! :) 非常感谢兄弟。我只需要使用你的方法来绘制它,我不小心将玩家的宽度/高度设置为 1。
猜你喜欢
  • 2014-04-04
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多