Reputation: 1134
How 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.
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 .. ?
Upvotes: 1
Views: 629
Reputation: 13581
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 );
}
}
};
Upvotes: 1