【问题标题】:How to show pictures in libGDX?如何在 libGDX 中显示图片?
【发布时间】:2018-04-18 09:44:01
【问题描述】:

我正在开发一个游戏,其中目录中的几张图片显示在屏幕上。 我正在使用scene2d。 但由于某种原因,这些图片没有显示在屏幕上。
任何人都可以解释为什么? 这是我的代码:

public class Picture extends Actor {
    private Image img;
    public Picture(Image img) {
        this.img = img;
    }
}

以及应该在其中绘制图片的游戏类别:

public class GameScreen extends Stage implements Screen {
     private FileHandle dirWithTextures;

public GameScreen() {
     super(new StretchViewport(1260.0f, 836.0f, new OrthographicCamera()));
     dirWithTextures = Gdx.files.internal("textures/");
}
public void buildStage() {
    ArrayList<Picture> pictureList = new ArrayList<Picture>();

    for (int i = 0; i < 4; i++) {
        pictureList.add(new Picture(new Image(newTexture(dirWithTextures.list()[i]))));
    }
    Collections.shuffle(pictureList);

    for (Picture p: pictureList) {
        p.setPosition(getWidth() / 2, getHeight() / 2, Align.center);
        addActor(p);
    }
  }
}

【问题讨论】:

  • 请下次写评论或编辑您的问题,而不是编辑我的答案。你的意思是“我不认为”你先尝试过我的代码吗? Actor 有空的 draw(..) 方法,所以如果你不覆盖它,它就不会被绘制。

标签: java libgdx actor scene2d


【解决方案1】:

你没有覆盖 Actor 的 draw() 方法,所以 Picture 什么也不画。

像这样覆盖:

public class Picture extends Actor {
     private Image img;
     public Picture(Image img) {
         this.img = img;
     }
     @Overrine 
     public void draw(Batch batch, float parentAlpha){
         image.draw(batch, parentAlpha);
     }
}

演员绘制方法:

public void draw (Batch batch, float parentAlpha) {
}

【讨论】:

    【解决方案2】:

    您的 Picture 类是 Actor 具有 Drawable 部分作为数据成员 (Image)。

    为什么你不使用 Image 而不是 (Actor + Drawable)。

    图像是可以绘制的Actor。

    public class Picture extends Image {
    
          // Any additional data members
    }
    

    对于stage来说,这里最好使用关联而不是继承。

    public class GameScreen implements Screen {
        private FileHandle dirWithTextures;
        private Stage stage;
    
      public GameScreen() {
    
          stage= new Stage(new StretchViewport(1260.0f, 836.0f, new OrthographicCamera()));
          dirWithTextures = Gdx.files.internal("textures/");
          buildStage();
      }
    
      public void buildStage() {
          ArrayList<Picture> pictureList = new ArrayList<Picture>();
    
          for (int i = 0; i < 4; i++) {
              pictureList.add(new Picture(new Texture(dirWithTextures.list()[i])));
          }
          Collections.shuffle(pictureList);
    
          for (Picture p: pictureList) {
             p.setPosition(100, 200, Align.center);  // < -- set Position according to your requirement.
             stage.addActor(p);
          }
      }
    
      @Override
      public void render(float delta) {
    
           Gdx.gl.glClearColor(0,0,0,1);
           Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
           stage.draw();
           stage.act();
      }
    
      // implement rest of method of Screen interface
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 2015-10-16
      • 2015-10-08
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多