如何用不同的方法libgdx绘制Sprite

时间:2015-08-13 22:03:44

标签: java android mobile libgdx 2d

您好我在我的create方法中创建了一个按钮。我在按钮和inputevent中添加了一个addlistener。但是如何在create方法中渲染精灵,如果按钮是触摸的话。绘制精灵我该怎么做?/

 public void create () {
    buttonStyle = new TextButtonStyle();
    buttonStyle.up = skin.getDrawable("button");
    buttonStyle.over = skin.getDrawable("buttonpressed");
    buttonStyle.down = skin.getDrawable("buttonpressed");
    buttonStyle.font = font;
    button = new TextButton("START", buttonStyle);

    stage.addActor(button);
    Gdx.input.setInputProcessor(stage);
     button.addListener(new InputListener(){    
         @Override
    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button ){
            drawTile(200,50);
                return true;
            }
        });

    // method used to draw a sprite when passing certain coordinates 
    public void drawTile(int x , int y){

      spriteBatch.draw(sprite, x  , y   );
      }

public void render () {

    Gdx.gl.glClearColor(1f, 0f, 0f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    spriteBatch.begin();
    spriteBatch.draw(background, 0, 0);
    drawGrid();
    spriteBatch.draw(startButton, 0, 0);
   stage.draw();

   spriteBatch.end()



}    

1 个答案:

答案 0 :(得分:1)

通常我的实体经理会控制这样的情况(不确定是否采用最佳方法),您必须调整代码。

首先,我有一个抽象类Entity,它将成为我游戏中所有实体的父级,所以如果我有敌人播放器,例如,我将有两个课程EnemyPlayer,它们将从Entity延伸。这个抽象类有(和其他东西)两个主要方法,update()render()

public abstract class Entity {
    ...
    public abstract void update(float delta); // <- to move, collisions, blablabla
    public abstract void render(SpriteBatch spritebatch); // <- to draw
    ...
}

通过这样做,你只需要一些管理员来正确更新你的实体,非常简单的是:

public class EntityManager{
    // A list of ALL your entities in your game...
    private static Array<Entity> myEntityList = new Array<Entity>();

    /**
     * A very simple add method... This is what you will call when you touch
     * your button.
     */
    public static void add(Entity newEntity){
        myEntityList.add(newEntity);
    }

    public static void update(float delta) {
        // Take care of those entities that are being updated or expired!
        ...
        // Update each of my entities
        for(Entity e : myEntityList){
            e.update(delta); // <- I'll update all my entities
        }
        ...
    }

    public static void render(SpriteBatch spritebatch){
        // draw each of my entities
        for(Entity e : myEntityList){
            e.render(spritebatch); // <- I'll draw all my entities
        }
    }

    // dispose and other things are here too (omitted for simplicity)
}

因为这个类是一堆静态方法,所以你只需要在主类中调用update()render()方法而不需要实例化它......

@Override
public void render(float delta){
    // code here
    ...
    EntityManager.update(delta); // <- Update all your entities
    spritebatch.begin();
    EntityManager.render(spritebatch); // <- Draw all your entities
    spritebatch.end();
    ...
    // more code here
}

最后,无论你有按钮监听器......

myButton.addListener(new InputListener() {
    public boolean touchDown (...) {
            Gdx.app.log("I'll add an entity");
            EntityManager.add(new Enemy()); // <--- here, new entity added!
            // EntityManager.add(new OtherEnemy()); <-- other example
            return false;
    }

    public void touchUp (...) {
            Gdx.app.log("blah");
    }
});

我必须省略很多代码,但我希望你能得到这个想法。通过这样做,您可以更好地控制游戏中的所有实体。我再说一遍,不确定最佳方法是否对我有效。