我正在用libgdx制作游戏。我有一个超级怪物,有那个怪物的孩子类(战士,法师,......)。我想在playScreen类中渲染这个Monster类(实际上是他的孩子)。每个类都有自己的动画和纹理,伤害/健康值。我怎么做?我在哪个类中定义渲染位置,该怪物的动画?在儿童班,超级班或在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);
}
PlayScreen类:
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();
}
答案 0 :(得分:0)
您可以在怪物和/或子类中创建渲染方法。这取决于所有怪物是否会以相同的方式呈现,无论哪种方式,在怪物类中制作一个空的渲染方法都是有用的(所以我们将来不必再投射类)。
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 void render(SpriteBatch batch) {
// here you will use your animation and textureAtlas to render
}
然后在PlayScreen中调用主渲染中的render方法,确保将批处理作为参数。
如果你想要以不同的方式呈现一个怪物,你可以override这样的怪物的渲染方法:
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
}
我希望您知道如何使用动画实际渲染它,否则这是一个有用的link。祝你好运!
答案 1 :(得分:0)
创建将为您的世界中的所有实体提供方法的基类。
例如,让我们给出名称Entity
。它将只有基于所有怪物,生物,玩家等的场和方法。
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类扩展Monster。例如,添加attack
方法。希望你明白了。我的意思是普林西比。
如何绘制我的所有实体?
外constructor
:
ArrayList<Entity> entities;
在constructor
:
entities = new ArrayList<>();
AnimatedEntity mage = new AnimatedEntity(someAnimation, x, y, width, height);
entities.add(mage);
在render(..)
:
for (e in entities) {
e.draw(batch);
}