2017-11-14 18 views
0

私はlibgdxでゲームを作っています。私はその怪物(戦士、魔道師、..)の子クラスを持つスーパークラスのモンスターを持っています。私はこのMonsterクラス(実際には彼の子供)をplayScreenクラスにレンダリングしたいと思います。各クラスには独自のアニマトンとテクスチャ、ダメージ/ヘルス値があります。それ、どうやったら出来るの?どのクラスでレンダリングのための位置を定義するのですか、そのモンスターのアニメーションですか?子クラス、スーパークラス、またはplayScreenで私の現在のコードはここにある:libgdxの他のクラスのテクスチャをレンダリング

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

あなたはモンスターでrenderメソッドおよび/または子クラスを作ることができます。それでも、すべてのモンスターが同じ方法でレンダリングされるかどうかによって、モンスタークラスの空のレンダリングメソッドを作るのに便利です(将来はクラスをキャストする必要はありません)。

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のメインレンダーでレンダリングメソッドを呼び出します。そのバッチをパラメータとして配置してください。

あなたは違ったレンダリングしたいモンスター1体を持っている場合は、あなたがこのような怪物の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です。がんばろう!

0

世界中のすべてのエンティティのメソッドを持つ基本クラスを作成します。

たとえば、名前をEntityとしましょう。それは今、あなたは、単純な、常に1つのテクスチャを描画しますベースのエンティティを作成することができる唯一のフィールドやメソッドも全てのモンスター、生き物、プレーヤーのためのベースなど

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; 

render(..)

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

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

関連する問題