在libgdx中从其他类渲染纹理

在libgdx中从其他类渲染纹理,libgdx,Libgdx,我正在用libgdx做一个游戏。我有一个超级类怪物,它的子类是战士、法师等等。我想在playScreen类中呈现这个怪物类(实际上是他的孩子)。每个类都有自己的动画和纹理、伤害/健康值。我该怎么做?我在哪个类中定义渲染的位置,那个怪物的动画?在儿童班、超级班还是在游戏屏幕上?我目前的代码如下: public class Monster { public Animation monster; public TextureAtlas atlas; public int health; public

我正在用libgdx做一个游戏。我有一个超级类怪物,它的子类是战士、法师等等。我想在playScreen类中呈现这个怪物类(实际上是他的孩子)。每个类都有自己的动画和纹理、伤害/健康值。我该怎么做?我在哪个类中定义渲染的位置,那个怪物的动画?在儿童班、超级班还是在游戏屏幕上?我目前的代码如下:

public class Monster {
public Animation monster;
public TextureAtlas atlas;
public int health;
public int damage;

public Monster(){

    atlas = new TextureAtlas(Gdx.files.internal("mons1.txt"));
    monster = new Animation(1/15f, atlas.getRegions());

}
儿童班:

public class Mage extends Monster {

public Mage(int health,int damage, Animation animation){

    super(health, damage, animation);

}
播放屏幕类:

public class PlayScreen  implements Screen, InputProcessor {
private SpriteBatch batch;
public TextureAtlas atlas;
TextureRegion region;
private int height;
private Viewport viewport;
private Camera camera;
private int width;
private float elapsedTime = 0;
private Handler h;
private Stage stage;
private InputProcessor processor;

public PlayScreen(Handler h){
    this.h = h;
    batch = h.batch;
    camera = h.camera;
    viewport = h.viewport;
    height = h.height;
    width = h.width;
    region = new TextureRegion();
    stage = new Stage(viewport,batch);
    stateTime = 0f;

}
@Override
public void render(float delta) {

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();

    batch.end();

}

您可以在monster和/或child类中创建一个render方法。这取决于是否所有的怪物都将以相同的方式渲染,无论哪种方式,在怪物类中创建一个空的渲染方法都是有用的(因此,我们以后不必强制转换类)

然后在PlayScreen的主渲染中调用render方法,确保将批处理作为参数

如果您有一个怪物希望以不同方式渲染,可以使用如下方式渲染该怪物的渲染方法:

public class Mage extends Monster {

public Mage(int health,int damage, Animation animation){
    super(health, damage, animation);
}

@Override
public void render(SpriteBatch batch) {
    // your mage specific render
    // calling super.render(batch) will call its superclass' render
}

我希望您现在知道如何使用动画实际渲染它,否则这里有一个有用的方法。祝你好运

创建基类,该基类将为世界中的所有实体提供方法。

例如,让我们输入名称
实体
。它将只有所有怪物、生物、玩家等的场和方法

class Entity {

    protected int x;   // use getters/setters to get/change these fields
    protected int y;
    protected int width;
    protected int height;
    protected Texture texture;

    public Entity(Texture texture, int x, int y, int width, int height) {
        this.texture = texture;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    void draw(SpriteBatch batch) {
        batch.draw(texture, x, y, width, height);
    }
}
现在您可以创建基本实体,该实体将始终简单地绘制一个纹理

如何设置动画?创建继承者。

class AnimatedEntity extends Entity{

    protected float stateTimer = 0f;   // use getters/setters to get/change these fields
    protected Animation animation;

    public AnimatedEntity(Animation animation, int x, int y, int width, int height) {
        super(animation.getKeyFrames(0), x, y, width, height);  // calls parent constructor
        this.animation = animation;

    }


    @Override
    void draw(SpriteBatch batch) {
        texture = animation.getKeyFrame(stateTimer);  // texture from parent visible here 
        super(batch); // calls draw method from Entity
    }
}
现在您可以从AnimatedEntity类扩展怪物。例如,添加
攻击
方法。希望你收到了。我是说普林西比

如何绘制所有我的实体?

外部
构造函数

ArrayList<Entity> entities;
entities = new ArrayList<>();
AnimatedEntity mage = new AnimatedEntity(someAnimation, x, y, width, height);
entities.add(mage);
渲染(…)
中:

entities = new ArrayList<>();
AnimatedEntity mage = new AnimatedEntity(someAnimation, x, y, width, height);
entities.add(mage);
for (e in entities) {
    e.draw(batch);
}