【问题标题】:rendering texture from other class in libgdx从 libgdx 中的其他类渲染纹理
【发布时间】:2017-11-14 13:09:36
【问题描述】:

我正在 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();

}

【问题讨论】:

    标签: libgdx


    【解决方案1】:

    创建具有适用于您世界中所有实体的方法的基类。

    例如,让我们输入名称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);
    }
    

    【讨论】:

      【解决方案2】:

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

      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 的主渲染中调用渲染方法,确保将批处理作为参数。

      如果你有一个怪物你想用不同的方式渲染,你可以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。祝你好运!

      【讨论】:

        猜你喜欢
        • 2014-10-28
        • 2011-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-22
        • 2014-04-03
        相关资源
        最近更新 更多