Rendering textures causing memory leak - java

Inside my game I have this code. It renders a texture that serve as a button:
private void drawStart(){
startTexture = new Texture(Gdx.files.internal("start.png"));
startTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
stageStart = new Stage();
stageStart.clear();
buttonStart = new Image(startTexture);
buttonStart.setX(10);
buttonStart.setY(Gdx.graphics.getHeight()/2.75f);
buttonStart.setWidth(Gdx.graphics.getWidth()/4);
buttonStart.setHeight(Gdx.graphics.getHeight()/4);
Gdx.input.setInputProcessor(stageStart);
buttonStart.addListener(new ClickListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
currentState = GameState.RESET;
startTexture.dispose();
stageStart.dispose();
return true;
}
});
stageStart.addActor(buttonStart);
stageStart.draw();
startTexture.dispose();
}
However, whenever I put drawStart(); into my render method, the Java Heap and Native Heap slowly increases by 1 every 10 seconds. So, if the user leaves the game on the menu for about 5 minutes the game will crash on their phone. I've tested it and it only occurs when the texture is rendered.
I would appreciate any help on fixing this. I have tried an if statement that states if rendered = 0, render the texture then set rendered 1 but that didn't work.

This might help you. You only need draw in your render. So now you can put drawStart() in your render method which will only draw the stage, while leaving screen dont forget to call dispose.
private void drawStart(){
stageStart.draw();
}
public void initialize() {
startTexture = new Texture(Gdx.files.internal("start.png"));
startTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
stageStart = new Stage();
stageStart.clear();
buttonStart = new Image(startTexture);
buttonStart.setX(10);
buttonStart.setY(Gdx.graphics.getHeight()/2.75f);
buttonStart.setWidth(Gdx.graphics.getWidth()/4);
buttonStart.setHeight(Gdx.graphics.getHeight()/4);
Gdx.input.setInputProcessor(stageStart);
buttonStart.addListener(new ClickListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
currentState = GameState.RESET;
startTexture.dispose();
stageStart.dispose();
return true;
}
});
stageStart.addActor(buttonStart);
}
public void dispose() {
startTexture.dispose();
}

Your problem is, that in drawStart() you are creating new Textures and a new Stage.
If you call this every render loop, you create new Textures and a new Stage about 60 times/second.
This ofc causes a memory leak.
You should load/create Textures and the Stage only once, in the constructor or in the create() or show() method.
Also think about disposing them when needed. Here is a list of the things you need to dispose manually.
In the render loop you should then only update and draw the things.
But as you only have 3 month of experience i suggest you to learn the basics first. Don't rush into game programming, it will kill your motivation.
First learn the basics, then start with some ASCII-Games (commandline) and then you can start with libgdx.
If you are ready for libgdx, read the Wiki (at least the parts you need) as well as some tutorials (maybe they don't use the latest version of libgdx, but the concept should be more or less the same and it should help you understanding it.)

Related

JPanels not being drawn

I'm working on a vertical scrolling game, and I'm using a thread to generate new enemies every 2 seconds. Each enemy is an image in a JPanel. For some reason, The generated enemies are not showing up in the JFrame, but they are present. When the player collides with one of the enemies, all the enemies show up.
Here's the code:
private void checkCollision() {
for(AlienShip as : enemies) {
if(player.getBounds().intersects(as.getBounds()))
player.setVisible(false);
}
}
private void setAlien() {
alien = new AlienShip();
add(alien);
enemies.add(alien);
System.out.println("Enemies: " + enemies.size());
}
public Thread alienGenerator() {
for(int i = 0; i < 3; i++) { // these are being drawn
setAlien();
}
return new Thread(new Runnable() {
#Override
public void run() {
int sleepTime = 2000;
while(true) {
try {
Thread.sleep(sleepTime);
} catch(InterruptedException e) {
System.out.println(e);
}
setAlien(); //these aren't
}
}
});
}
private void gameLoop() {
alienGenerator().start();
mainTimer = new Timer(50, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
repaint();
checkCollision();
}
});
mainTimer.start();
}
It always seems that you're Darned If You Do And Darned If You Don't. As far as I'm concerned the code you had placed in your earlier post was adequate, as a matter of fact, it was still lacking (no PlayerShip Class). The code example in this post does even less justice. Never the less......
Before I get started I just want you to know that I personally would have tackled this task somewhat differently and the meager assistance provided here will be solely based on the code you have already provided in this and previous posts.
The reason you are not seeing your Alien Ships displaying onto the Game Board upon creation is because you don't revalidate the board panel. As you currently have your code now this can be done from within the Board.setAlien() method where the Alien Ships are added. Directly under the code lines:
alien = new AlienShip();
add(alien);
enemies.add(alien);
add the code line: revalidate();, so the code would look like this:
alien = new AlienShip();
add(alien);
enemies.add(alien);
revalidate();
Your Alien Ships should now display.
On A Side Note:
What is to happen when any Alien Ship actually makes it to the bottom of the Game Board? As a suggestion, have them re-spawn to the top of the game board (serves ya right fer missin em). This can be done from within the AlienShip.scrollShip() method by checking to see if the Alien Ship has reached the bottom of the board, for example:
public void scrollShip() {
if (getY() + 1 > this.getParent().getHeight()) {
setY(0 - PANEL_HEIGHT);
}
else {
setY(getY() + 1);
}
}
In my opinion, PANEL_HEIGHT is the wrong field name to use. I think it would be more appropriate to use something like ALIEN_SHIP_WIDTH and ALIEN_SHIP_HEIGHT. Same for the variables panelX and panelY, could be alienShipX and alienShipY. Food for thought.
As you can see in the code above the current Game Board height is acquired by polling the Game Board's getHeight() method with: this.getParent().getHeight(). This allows you to change the Game Board size at any time and the Alien Ships will know where that current boundary is when scrolling down. All this then means that the setResizable(false); property setting done in the Main Class for the Game's JFrame window can now be resizable: setResizable(true);.
You will also notice that when the Alien Ship is re-spawned at top of the Game Board it is actually out of site and it flows into view as it moves downward. I think this is a much smoother transition into the gaming area rather than just popping into view. This is accomplished with the setY(0 - PANEL_HEIGHT); code line above. As a matter of fact even when the game initially starts, your Alien Ships should flow into the the gaming area this way and that can be done from within the AlienShip.initAlienShip() method by initializing the panelY variable to panelY = -PANEL_HEIGHT;.
This now takes me to the initialization of the PANEL_WIDTH and PANEL_HEIGHT fields. The values seem enormous (224 and 250 respectively). Of course you may have set to these sizes for collision testing purposes, etc but I think an image size of 64 x 35 would most likely suffice:
This image should be a PNG image with a transparent background which then eliminates the need for the setBackground(Color.BLUE); code line located within the AlienShip.initAlienShip() method.
The AlienShip.getX() and AlienShip.getY() methods should be overridden:
#Override
public int getX() { ... }
#Override
public int getY() { ... }
I think extending the AlienShip Class to JLabel would be better than to JPanel. To JPanel seems like overkill:
public class AlienShip extends JLabel { ... }
Adding a background image to the Game Board can add pizazz to the game. This can be achieved by adding the following code to the Board.paintComponent() method:
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon imgIcon = new ImageIcon("images/StarBackground.png");
Image img = imgIcon.getImage();
g.drawImage(img, 0, 0, this.getSize().width, this.getSize().height, this);
}
Images can be acquired here.
This should keep you going for a while. Before to long it'll be Alien mayhem.

JavaFx Stop Moving Image Smudging Canvas

I'm trying to make a simple animated menu with images bouncing off around the screen but the images leave a trail where ever the move.
public void handle(long now) {
// TODO Auto-generated method stub
boolean intersectFlag = false;
for(Letter l : letters){
gameMenuGraphicsContext.drawImage(l.letterImage, l.letterRectangle.getX(), l.letterRectangle.getY());
l.moveSimple();
}
}};
Any idea on how to stop this happening?
Think of the Canvas as a piece of paper onto which you are writing. If you don't erase anything explicitly everything will be visible what you have ever drawn to it. Actually you should reconsider your decision to use a Canvas at all. It is not very well suited for such kind of animations.
As mipa stated, your problem is that the drawn image is never erased. To erase your canvas, use:
graphicsContext.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
To keep yourself from having to call the clearRectmethod each time you want to draw on the screen, one easy write-and-forget way is to combine the clearing and drawing into one method. You can even use a lambda expression to draw on the canvas, as in the following code:
private static void clearAndDraw(GraphicsContext gc, Consumer<GraphicsContext> draw) {
gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight());
draw.accept(gc);
}
public void handle(long now) {
//...
for(Letter l : letters) {
clearAndDraw(graphicsContext, gc -> gc.drawImage(l.letterImage, l.letterRectangle.getX(), l.letterRectangle.getY()));
//...
}
}

Display moving blocks in a JFrame

I have a class that creates a JFrame on which a simple game of Tetris will be played, I also have a class DrawSquare, which does exactly what you think it does, however when I initialise a new instance of the DrawSquare class and then try to draw that one and all the others to my JFrame things start to go wrong, the code is intended for one square to be drawn in the top left hand corner and then drop down a line at a time until it reaches the bottom of the frame (it does this), then a new square should be drawn in the second column at the top of the frame, as well as our first square in the bottom left hand corner, however once it starts dropping down the second column I get a series of squares drawn in a diagonal towards the top right hand corner. At the moment all I plan for the code to do is have a square drop from the top row of each column and stop when it reaches the bottom of the frame, am I storing the instance of the class at the wrong point in the code? Edit: In fact I'm pretty sure it's that, I'd want to store that instance when it reaches the bottom. Does every instance of the class need its own timer?
public class Tetris extends JFrame {
public static final int height = 20; //height of a square
public static final int width = 20; //width of a square
public int xPos = 0; //column number of the square
public int yPos = 0; //row number of the square
public static void main(String[] args){
Tetris tet = new Tetris();
}
public Tetris() {
DrawSquare square = new DrawSquare(xPos, yPos, width, height, false);
add(square);
DrawSquare.squares.add(square);
setSize(220,440);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
public class DrawSquare extends JPanel {
public static List<DrawSquare> squares = new ArrayList<>();
protected int xPos;
protected int yPos;
protected int width;
protected int height;
protected Timer timer = new Timer(200, new TimerListener());
protected boolean endFall = false;
public DrawSquare(int xPos, int yPos, int width, int height, boolean endFall) {
this.xPos = xPos;
this.yPos = yPos;
this.width = width;
this.height = height;
this.endFall = endFall;
this.timer.start();
}
class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
yPos++;
if (yPos > 19) {
yPos = 19;
endFall = true;
}
if (endFall == true) {
timer.stop();
if (xPos > 8) {
xPos = 8;
}
xPos++;
endFall = false;
yPos = 0;
DrawSquare newSqr = new DrawSquare(xPos, yPos, width, height, true);
squares.add(newSqr);
add(newSqr);
}
timer.start();
repaint();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Iterator<DrawSquare> it = squares.iterator();
while (it.hasNext()) {
DrawSquare square = it.next();
g.fillRect(square.xPos * square.width, square.yPos * square.height, square.width, square.height);
}
}
}
You are giving a great example of the fundamental misunderstanding beginners have of how the swing (and many other graphics toolkits) render stuff to the screen. I will give an overview of that, as it pertains to you, then answer your immediate questions and explain how to fix your code.
It took me a (very long) while to figure out how this stuff works my self, so please bear with me. I hope that reading through this answer will help you in a much more general way than answering this one question.
Asynchronous Drawing
Swing draws windows in a totally different sequence (the event dispatching thread) than the ones that modifies the state of your program (the main thread, as well as timer and other threads). You can modify the coordinates of things you want to draw as many times as you like in the main thread, but the changes will not show up until you request them to by calling JComponent.repaint() on one of your components. This will generally trigger a nearly-immediate repaint of the component, displaying your latest state.
If you change the coordinates of a widget like a JPanel in your main thread, it will likely show up immediately. This is because the methods you use to set the position will trigger repaint requests internally.
A repaint request gets queued and eventually processed by the event dispatching thread. This is where the paintComponent method gets called. The paintComponent method should therefore only draw. It should not do any other logic. If it needs to know how to draw some specialized stuff, the information for that should be stashed somewhere accessible by one of the other threads.
In short, you make calculations and update state as you need in the main thread or the timer. Then you access that state in the event dispatching thread via the paintComponent method.
Timers
There are a bunch of ways you can use timers to run your GUI, but you only really need one for the current application. In your case, the timer only needs to do two things:
Check if a block has fallen all the way down and doesn't need to move any more.
Trigger a repaint of your panel.
You do not need to compute the updated position of the blocks in the timer if the block's position is a simple equation with respect to time. If you know the time at which a block appears on the screen and the current time, you know how far the block has moved, so you can paint it in the correct spot based purely on the elapsed time.
If you had a more complicated system with paths that you could not predict purely on the time, I would recommend sticking the movement logic into the timer events as well. In that case, you might consider having multiple timers, or switching to java.util.timer. But again, this does not apply to your current case (even with multiple blocks).
Model and View
The model of your program is the thing that holds the abstract state. In this case, the positions and other meta-data about all your blocks. The view is the part that does the rendering. It is usually a good idea to separate these two things. There is often a third component to GUIs, called the controller, which connects the model and view to the user. We will ignore it here since you are not asking about controlling the blocks yet.
In your current code, you have attempted to represent your blocks with an extension to JPanel and a static list of existing blocks. While a JPanel may be a convenient way to display rectangular blocks with some custom graphics in them (like icons), I would recommend that you start by drawing the blocks directly using the Graphics object passed to paintComponent. At least initially, it will help you to think of the drawing code and the game logic as separate entities.
Final Rant Before Code Dump
I have made rewrites to your code to encapsulate all the ranting I did before into code. Here are some additional minor points about what I did that may help explain my reasoning:
When you call JFrame.add(...) to add a component to a JFrame, you are really calling JFrame.getContentPane().add(...). The content pane is where 90% of normal swing components go in a window. Therefore, we can either set the JPanel that will do the rendering as your content pane or we can add it to the current content pane. I have chosen to do the latter so that you can add other widgets, like a score board, at a later time.
Class names should generally be nouns, while methods are often verbs. This is not an absolute rule (nothing really is), but naming things this way will often help you visualize the interactions between objects in a more meaningful way. I have renamed DrawSquare to GamePiece for this reason.
There is no longer any reason for GamePiece to be a JPanel. It just needs to know its own width, height, and time of appearance.
The other problem with trying to have DrawSquare draw itself is that a component can only really draw within its own bounding box. So you really want to override the paintComponent of whatever holds the rectangles.
The rendering class maintains a reference to two lists of GamePieces. One is for the moving objects and one is for the ones that have fallen. The logic for moving them between the lists is in the timer. This is better than say adding a flag to GamePiece because it facilitates incremental repaint. I will only partially illustrate this here, but there is a version of repaint that only requests a small region to be painted. This would be useful to speed up the movement.
Code
public class Tetris extends JFrame
{
public static final int height = 20; //height of a square
public static final int width = 20; //width of a square
public static final int x = 0;
private GamePanel gamePanel;
public static void main(String[] args)
{
Tetris tet = new Tetris();
// Normally you would tie this to a button or some other user-triggered action.
tet.gamePanel.start();
tet.gamePanel.addPiece(new GamePiece(width, height, x));
}
public Tetris()
{
getContentPane().setLayout(new BorderLayout());
gamePanel = GamePanel();
add(gamePanel, BorderLayout.CENTER);
setSize(220,440);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
}
public class GamePanel extends JPanel
{
private List<GamePiece> moving;
private List<GamePiece> still;
private Timer timer;
public GamePanel()
{
moving = new ArrayList<>();
still = new ArrayList<>();
timer = new Timer(100, new TimerListener());
}
public addPiece(int width, int height, int x)
{
moving.add(new GamePiece(width, height, x));
}
public void start()
{
timer.start();
}
#Override
public void paintComponent(Graphics g)
{
Rectangle clip = g.getClipBounds(null);
Rectangle rectToDraw = new Rectangle();
// I prefer this, but you can make the call every
// time you call `GamePiece.getY()`
long time = System.currentTimeMillis();
for(GamePiece piece : this.moving) {
rectToDraw.setSize(piece.width, piece.height)
rectToDraw.setLocation(piece.x, piece.getY(time))
if(rectangleToDraw.intersects(clip))
((Graphics2D)g).fill(rectToDraw)
}
for(GamePiece piece : this.still) {
rectToDraw.setSize(piece.width, piece.height)
rectToDraw.setLocation(piece.x, piece.getY(time))
if(rectangleToDraw.intersects(clip))
((Graphics2D)g).fill(rectToDraw)
}
}
private class TimerListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e)
{
long time = System.currentTimeMillis();
// Using non-iterator loop to move the pieces that
// stopped safely. Iterator would crash on in-loop move.
for(int i = 0; i < moving.size(); i++) {
piece = moving.get(i);
if(piece.getY(time) > 440 - piece.height) {
moving.remove(i);
still.add(piece);
i--;
}
}
repaint();
}
}
}
public class GamePiece
{
public final int width;
public final int height;
public final long startTime;
public int x;
public GamePiece(int width, int height, int x)
{
this.width = width;
this.height = height;
this.startTime = System.currentTimeMillis();
this.x = x;
}
public int getY(long time)
{
// This hard-codes a velocity of 10px/sec. You could
// implement a more complex relationship with time here.
return (int)((time - this.startTime) / 100.0);
}
}
Your main problem in a nutshell: you need to separate the JPanel component class from the square logical class. Right now, they are one and the same, and every time you create a new DrawSqaure, you're creating a new JPanel, starting a new Swing Timer, and thus calling code that doesn't need to be called. This is also forcing you to make the List static else you'd have a stack overflow error. Solution: separate the two out, make your List non-static, and use only one Swing Timer.

libgdx/Android: graphics disappearing after the app is destroyed/paused

I was doing some tests and I realized that on my Nexus 5, when I hit the back key (or the home) -that is, when there's a change of context- and I go back to my game, the openGL context is lost.
There are no textures anymore (they show as black) or skins for the UI (the buttons are white).
I thought it was automanaged by libgdx automatically, right? So why is this happening?
The way I'm creating the textures is via TextureAtlas, like
TextureAtlas atlas;
TextureRegion bg;
atlas = new TextureAtlas(Gdx.files.internal("mainMenu.atlas"));
bg = atlas.findRegion("bg");
And then it's used with the batch.draw(bg, x, y, w, h);
I also tried creating the TextureRegion loading directly a Texture instead of a TextureAtlas (just in case but it should be the same) and I get the same result...
Anyone?
Edit: more specific code:
Screen class basics:
public class MainMenuScreen extends ScreenManager.Screen {
private Game game;
private InputMultiplexer inputMultiplexer = new InputMultiplexer();
private MainMenuUi screenUi;
private MainMenuView screenView;
private TextureAtlas atlas;
public MainMenuScreen(ConbiniGame game) {
this.game = game;
atlas = new TextureAtlas(Gdx.files.internal("mainMenu.atlas"));
screenUi = new MainMenuUi(game);
screenView = new MainMenuView(atlas);
inputMultiplexer.addProcessor(screenUi.getInputProcessor());
inputMultiplexer.addProcessor(screenView.getInputProcessor());
Gdx.input.setInputProcessor(inputMultiplexer);
}
// ...
}
MainMenuView class where the TextureAtlas is being used...
public class MainMenuView {
private Stage stage;
private OrthographicCamera camera;
private Viewport viewport;
private TextureAtlas atlas;
TextureRegion bg;
public MainMenuView(TextureAtlas atlas) {
atlas = atlas;
bg = atlas.findRegion("bg");
camera = new OrthographicCamera();
camera.setToOrtho(false);
viewport = new FitViewport(1080, 1920, camera);
stage = new Stage(viewport);
}
public void update(float delta) {
stage.act(delta);
}
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.getBatch().begin();
stage.getBatch().draw(bg, 0, 0, stage.getCamera().viewportWidth, stage.getCamera().viewportHeight);
stage.getBatch().end();
stage.draw();
}
public InputProcessor getInputProcessor() {
return stage;
}
}
The code is just to show the use of the texture, with other parts removed
You didn't provide enough information, but your problem is likely caused by using static in your code. Don't do that.
When you press the back button your app is closed. When you press the home button then your app is paused. Note that this are two different things. Therefor you might experience the problem not always when using the home button. This because while your app is paused, that android might decide to close it (to free memory), but it is not guaranteed to do so.
Either way, this is not related to the opengl context being lost. It is just closed. If the context would be really lost then libgdx (and later versions of android) will recover it for you.
When you close your app and then immediately start it again then Android might reuse the same VM for that instance of your app. This also means that any static variables will have the value they had from the previous run of your app. If any of those variables include (may be indirectly) any resources, then those resources won't be valid anymore.
tl;dr never use static in android applications.
The most common mistake (without seeing your code this is just guessing) is to access assets by using a singleton. E.g. MyGame.getInstance().assets.get("atlas",...); don't do that, it will (because of above reason) fail. Instead pass a reference to your MyGame instance to whichever classes needs it (e.g. your Screen: new MenuScreen(this);).
I ran into the same problem when making my first LibGDX game - a Flappy Bird clone - and it turned out the problem was not with the Texture at all, but with a MEMBER Vector2/Vector3 variable that I used for velocity.
To make the bird fall from a given height, I'd declared:
1. a member int variable for gravity (initialised to -15),
2. a Vector2 member variable for bird position, initialised to (50, 300), and
3. another Vector2 member variable for velocity, initialised to 0,0
Now, in the update() method, I passed gravity as a value to velocity's y-axis parameter using velocity.add(0, gravity)
Then I scaled it, using velocity.scl(dt)
Then I added that velocity to position and then unscaled it again, using velocity. scl(1/dt).
This last line was what caused the bug, but since it was essential for the functionality of my game, I couldn't afford to delete.
My solution was to change the velocity variable from a member variable to a local variable, and the textures stopped disappearing when game was paused.
Actually it's OK to use Singleton or other static paterns for texture management. The only thing you should do to escape black-rectangle problem is:
make static content depending of Game entity:
private static TextureStore sStore = null;
private static MyGdxGame sGame;
static public TextureStore getStore()
{
if(sStore==null||sGame!=MyGdxGame.getGame())
{
sStore = new TextureStore();
sGame = MyGdxGame.getGame();
}
return sStore;
}
And in game entity
#Override
public void create()
{
sGame = this;
...

libGDX Multiplexer- removing a processor

I have a platform button the game screen, and I'm trying to make it so the user presses it once, clicks somewhere on the screen to draw one platform, and then if they clicked the screen again, nothing would happen.
Right now, before I click the platform button, nothing happens which is what I want. When I press the platform button, the user can click the screen to draw a platform, but, after pressing the platform button once, every time they click the screen, a platform gets drawn so I'm having trouble making it so they can only draw one. I thought using removeProcessor() would've worked, but it's not.
InputController inputProcessor;
InputMultiplexer multiplexer;
public GameScreen(FallDown game) {
this.game = game;
GAMESCREEN_STATE = WORLD_STATE_READY;
this.cam = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
this.cam.position.set(FRUSTUM_WIDTH / 2, FRUSTUM_HEIGHT / 2, 0);
this.cam.setToOrtho(false, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
batch = new SpriteBatch();
world = new World();
renderer = new WorldRenderer(batch, world);
cam.position.set(FRUSTUM_WIDTH / 2, 105, 0);
inputProcessor = new InputController(game, world, cam);
multiplexer = new InputMultiplexer();
}
Then, at the end of my render method I have
multiplexer.addProcessor(stage);
Gdx.input.setInputProcessor(multiplexer);
These are the listeners for my buttons and I'm just using the reset button as an alternate way to stop the user from drawing platforms.
reset_button.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
multiplexer.removeProcessor(inputProcessor);
return true;
}
});
platform_button.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
if (GAMESCREEN_STATE != WORLD_STATE_RUNNING) {
multiplexer.addProcessor(new InputController(game, world, cam));
}
return true;
}
});
Well, I would advice you not to add and remove processors that many times (especially the one in the render method.. move that to the constructor/create method).
An easy way to achieve what you are looking for is just have a boolean variable:
boolean createplatform = false;
And then set it to true when the button is pressed and to false when the first platform is created.
(So when you touch the screen, that boolean decides if a platform is created).

Categories