简体   繁体   中英

How to add child image to sprite in Libgdx as we can do in Cocos2d?

While i was learning about Libgdx i came across this problem of adding a child sprite to parent sprite in Libgdx as i did the same in Cocos2d it had no problem of doing it. How can the same be achieved in Libgdx, so that i can perform operation on parent sprite and it will be followed by the child added to it.

EDIT : I have seen at https://github.com/libgdx/libgdx/wiki/Scene2d of scene2d, but they havn't mentioned how to make use of Group.

This is what i tried earlier but its coming white screen

public class TestScreen implements Screen{
private MainGame game;
private Sprite sprite;
public Group group;
private Stage stage;
private SpriteBatch batch;
public TextureAtlas atlas1;
public Skin skin;
public Image bg;
public Sprite sprite1;

public TestScreen(MainGame game)
{   
    this.game=game;

    group = new Group();
    batch = new SpriteBatch();

    circle=AssestLoader.circle1;
}


@Override
public void render(float delta) 
{
    // TODO Auto-generated method stub

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    stage.act(delta);

    batch.begin();
    stage.addActor(group);
    batch.end();

}

@Override
public void show() {

    // TODO Auto-generated method stub

    atlas1 = new TextureAtlas("test/gamescreentest.pack");
    skin = new Skin();
    skin.addRegions(atlas1);

    bg = new Image();
    bg.setDrawable(skin, "playbg_hdpi");
    bg.setHeight(460f);
    bg.setWidth(460f);

    group.addActor(bg);

}

@Override
public void hide() {}

@Override
public void resize(int width, int height) {}

@Override
public void pause() {}

@Override
public void resume() {}

@Override
public void dispose() {}}

Its coming white screen without image

You could use scene2d and actors ( https://github.com/libgdx/libgdx/wiki/Scene2d ). But this would require you to (maybe) rewrite parts of your application.

If your images are static and don't change I'd recommend you to draw the child ontop of the parent using a framebuffer. That'd be best in case of performance.

EDIT: The blog post at http://www.gamefromscratch.com/post/2013/12/11/LibGDX-Tutorial-3C-Scene-management.aspx has a very good example of groups.

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