简体   繁体   中英

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 ?

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

在此处输入图片说明

  • Maybe, 4 images

在此处输入图片说明

I know the following code not complete : (in render method)

 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. If you want to create them on user input and on some conditions you should use 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

     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 );
                    }
                }
    };

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