简体   繁体   中英

rendering texture from other class in libgdx

i am making a game in libgdx. I have a super class Monster with child classes of that monster (warrior,mage,..). I would like to render this Monster class (actually his child) in playScreen class. Each class has its own animaton and textures, damage/health values. How do i do that? In which class do i define position for rendering, animation of that monster? in child classes, super class or in playScreen? My current code is here:

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());

}

Child class:

public class Mage extends Monster {

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

    super(health, damage, animation);

}

PlayScreen class:

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();

}

You could make a render method in the monster and/or child class. It depends if all monsters are going to be rendered the same way, either way it is useful to make an empty render method in the monster class nonetheless (so we do not have to cast classes in future).

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
}

You then call the render method in your main render in PlayScreen, make sure to put the batch as parameter.

If you have one monster you wish to render differently, you could override the monster's render method like this:

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
}

I hope you know how to use your animation to actually render it now, otherwise here is a useful link . Good luck!

Create base class that will have methods for all entities in your world.

For example let's give in name Entity . It will have only fields and methods that base for all monsters, creatures, player also, etc.

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);
    }
}

Now you can create base entity that will simple draw one texture always.

How to animate it? Create inheritor.

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
    }
}

Now you can extend Monster from AnimatedEntity class. To add attack method for example. Hope you got it. I mean principe.

How to draw all my entities?

Outside constructor :

ArrayList<Entity> entities;

In constructor :

entities = new ArrayList<>();
AnimatedEntity mage = new AnimatedEntity(someAnimation, x, y, width, height);
entities.add(mage);

In render(..) :

for (e in entities) {
    e.draw(batch);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM