简体   繁体   English

在舞台上添加无限数量的演员(libGDX)

[英]Add unlimited number of actors on stage (libGDX)

H ow can I add unlimited image (Actor) which player add any number of image he want on stage ? ^ h嗷嗷我可以增添无限的图像(演员)的球员加入,他希望在舞台上任意数量的形象

For Example: Suppose the actor is this image 例如:假设演员是这张图片 在此处输入图片说明 , and the player want to add it from any point to any point on the stage coordinates. ,并且玩家希望将其从舞台坐标的任何点添加到任何点。 I as developer I don't know how many images which player want to be added on stage. 作为开发人员,我不知道要在舞台上添加多少个图像。

  • Maybe, 5 images 也许5张图片

在此处输入图片说明

  • Maybe, 4 images 也许是4张图片

在此处输入图片说明

I know the following code not complete : (in render method) 我知道以下代码不完整:(在render方法中)

 Vector2 position = stage.screenToStageCoordinates(new Vector2(Gdx.input.getX(), Gdx.input.getY()));
 image.setPosition(position.x, position.y);
 stage.addActor(image);

Anyone have Idea .. ? 任何人都有主意..?

there is no limit on the Stage about actors count (although you should notice that to many actors can affect the performance) so you just can create them in the render method as you wrote. 关于演员数的舞台没有限制(尽管您应该注意,许多演员会影响表演),因此您可以在编写时在render方法中创建它们。 If you want to create them on user input and on some conditions you should use InputListener 如果要在用户输入和某些条件下创建它们则应使用InputListener

The thing is to create a button like "Buil House" and onClick on it check if player has enough money then create new actor and add it to stage - important thing is that you cannot add the same actor twice 要做的是创建一个类似“ Buil House”的按钮,然后单击它,检查玩家是否有足够的钱, 然后创建新的actor并将其添加到舞台上 -重要的是,您不能两次添加同一actor

     Button button = new Button(skin, "buildButton");

     button.addListener(buildListener);

     ...

     ClickListener buildListener = new ClickListener()
            {
                public void clicked(InputEvent event, float x, float y) 
                {
                    if( cash > BUILDING_COST )
                    {
                        Image building = createBuildig(); //it can be also some class inherits actor if you want it to have some more informations
                        building.setPosition(position.x, position.y); //some position

                        //you can also add actor to some array to process it lateer somehow...

                        stage.addActor( building );
                    }
                }
    };

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

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