简体   繁体   English

将儿童演员相对于舞台而不是父母定位

[英]Position Child Actor relative to Stage and NOT Parent

I have HorizontalGroup with actors added to it. 我有添加演员的Horizo​​ntalGroup。 When I tap an actor, I want it to pop up in the center of the screen with (pseudocode) 当我点击一个演员时,我希望它以(伪代码)出现在屏幕中央。

Gdx.graphics.getWidth()/2 - myActor.getWidth()/2;
Gdx.graphics.getHeight()/2 - myActor.getHeight()/2;

The problem is that the actor is getting position relative to the parent and not the stage/screen. 问题在于演员正在相对于父对象而不是舞台/屏幕获得位置。

How could I go about solving this? 我该如何解决呢?

There are quite a few possibilities to solve your question. 有很多可能性可以解决您的问题。

  1. Transform the screen coordinates to stage coordinates (skip if they are the same) then transform the stage coordinates to the group coordinates, then set the actor to the transformed position 将屏幕坐标转换为舞台坐标(如果相同则跳过),然后将舞台坐标转换为组坐标,然后将actor设置为转换后的位置

     vector = stage.screenToStageCoordinates(vector); vector = group.stageToLocalCoordinates(vector); actor.setPosition(vector.x, vector.y); 
  2. Add a Stack to your stage, add already existing group to it, then if you tap on the actor add it to the stack (this automatically removes it from its old parent) Stack添加到舞台,向其中添加已经存在的组,然后如果您点击actor,则将其添加到堆栈(这会自动将其从其旧父级中删除)

     Stack stack = new Stack(); stage.addActor(stack); stack.add(yourGroup); // then on tap stack.add(actor); 
  3. Disable the group layout, so if you manipulate the actor, the group layout is not calculated again, plus disable child transformation to parent coordinates: 禁用组布局,因此,如果您操纵角色,则不会再次计算组布局,并禁用对父坐标的子转换:

     group.setLayoutEnabled(false); group.setTransform(false); 

    You might enable the layout/transformation again, later. 您稍后可以再次启用布局/转换。

  4. Similar to 2., but add the actor the stage and set the position to the center 与2.类似,但将actor添加到舞台并将位置设置到中心

     // then on tap stage.addActor(actor); actor.setPosition( (stage.getWidth() - actor.getWidth()) / 2f, (stage.getHeight() - actor.getHeight()) / 2f); 

Depending on your setup some solutions might not work as expected. 根据您的设置,某些解决方案可能无法按预期工作。


Here an example of 4.: 这里以4为例:

public class TestGame extends ApplicationAdapter
{
    private Skin    skin;
    private Stage   stage;
    private Texture t;

    @Override
    public void create()
    {
        skin = new Skin(Gdx.files.internal("uiskin.json"));

        t = new Texture(Gdx.files.internal("badlogic.jpg"));

        // So we see everything
        stage = new Stage(new ExtendViewport(4000, 400));

        HorizontalGroup hGroup = new HorizontalGroup();
        hGroup.setFillParent(true);

        // So that the hGroup is on top
        Table filler = new Table(skin);
        filler.setFillParent(true);
        filler.add(hGroup).fill().center();

        stage.addActor(filler);

        for (int i = 0; i < 10; i++)
        {
            final ImageButton b = new ImageButton(
                    new TextureRegionDrawable(new TextureRegion(t)));
            b.addListener(new ClickListener()
            {

                @Override
                public void clicked(InputEvent event, float x, float y)
                {
                    if (b.getParent().equals(hGroup))
                    {
                        stage.addActor(b);
                        b.setPosition(
                                (stage.getWidth() - b.getWidth()) / 2f,
                                (stage.getHeight() - b.getHeight()) / 2f);
                    } else
                    {
                        hGroup.addActor(b);
                    }
                }
            });
            hGroup.addActor(b);
        }

        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void resize(int width, int height)
    {
        stage.getViewport().update(width, height);
    }

    @Override
    public void render()
    {
        Gdx.gl.glClearColor(0f, 0f, 0f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act();
        stage.draw();
    }

    @Override
    public void dispose()
    {
        stage.dispose();
        skin.dispose();
        t.dispose();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM