libgdx和动作动作

时间:2012-03-08 14:00:38

标签: android action libgdx

我想制作一些游戏,但我遇到了问题。我想画两张图片。我使用Texture和SpriteBatch绘制两个图像。但现在我想实施一些行动。我想为用户创建messege。如果他点击第一张图片,他会得到消息:你的选择是picture1。而对于其他图像算法是相同的。

public void create() {
    background = new Texture(Gdx.files.internal("backg.png"));
    polishFlag = new Texture(Gdx.files.internal("german.png"));
    englishFlag = new Texture(Gdx.files.internal("english.png"));
    batch = new SpriteBatch();
}
public void render() {
    batch.begin();
    batch.draw(background, 0, 0, 480, 320);
    batch.draw(germanFlag, 140,80, 90, 60);
    batch.draw(englishFlag, 260,80, 90, 60);
    batch.end();
}

我如何实现此功能?我希望这个解决方案在android平台上运行。一些想法?

1 个答案:

答案 0 :(得分:1)

看一下InputProcessor接口。 (或者,使用InputMultiplexer类。)

使用InputProcessor,您可以执行以下操作:

public class YourGame implements InputProcessor{
      com.badlogic.gdx.math.Rectangle touchBounds;
      string message ;
      BitmapFont font;

      //lots of input processor methods
      @Override
      public boolean onTouchDown(x, y, int button){
          tocuhBounds.x = 140;
          if (rectangle.contains(x,y))
               message = "your choice is picture1";
          else{
              touchBounds.x = 260;
              if (rectangle.contains(x,y))
                message = "your choice is picture2";
          }
      }

      public void create() {
             background = new Texture(Gdx.files.internal("backg.png"));
             polishFlag = new Texture(Gdx.files.internal("german.png"));
             englishFlag = new Texture(Gdx.files.internal("english.png"));
             batch = new SpriteBatch();
             touchBounds = new Rectangle();
             touchBounds.width = 90;
             touchBounds.height = 60;
             touchBounds.y = 80;
             font = new BitmapFont();
      }

      public void render() {
          batch.begin();
          batch.draw(background, 0, 0, 480, 320);
          batch.draw(germanFlag, 140,80, 90, 60);
          batch.draw(englishFlag, 260,80, 90, 60);
          font.draw(batch, message, 10, 10);
          batch.end();
     }

}