2
votes

Please forgive me for my english.

Began to explore libGDX and have a problem. When I add actor on stage, method draw() not called. Tried to apply the method to draw a straight line, texture successfully drawn but it is not an actor, and this method is not correct.

Help please.

SpiderHunt.java

package com.spiderhunt;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.spiderhunt.screens.MainMenu;

    public class SpiderHunt extends Game{   
        private SpriteBatch batch; 
        public MainMenu mainMenu;

        private static SpiderHunt instance = new SpiderHunt();

        public SpiderHunt(){

        }

        public static SpiderHunt getInstance() {
            return instance;
        }

        public void create () {     

            //load textures
            Assets.load();

            batch = new SpriteBatch();

            mainMenu = new MainMenu(batch);
            this.setScreen(mainMenu);
        }

        public void showMainMenu(){     
            setScreen(mainMenu);
        }

        public void render (float delta) {

        }

        public void resize(int width, int height) {

        }

        public void pause() {

        }

        public void resume() {

        }

        public void dispose() {

        }   
    }

MainMenu.java

package com.spiderhunt.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.spiderhunt.Assets;
import com.spiderhunt.buttons.btnPlay;

public class MainMenu implements Screen {

    public btnPlay playButton;
    public Stage stage; 
    public SpriteBatch batch;    

    class GoToGameListener extends ClickListener {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            //some code for click or push
        }
    }

    public MainMenu(SpriteBatch batch_1) {
        batch = batch_1;

        stage = new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);  
        Gdx.input.setInputProcessor(stage);

        playButton = new btnPlay(); //make actor  

        stage.addActor(playButton);     
    }       

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

        //make background
        batch.begin();          
        batch.draw(Assets.bgMenuRegion, 0, 0, 540, 960);
        batch.end();

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw(); //this action must do method draw() from actor but it not called !!!!!!!!!!

        //batch.begin();
        //playButton.draw(batch, 0); THIS CODE DRAW BUTTON, BUT IT NOT CORRECTLY ACTOR
        //batch.end();
    }

    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub

    }

     @Override
    public void show() {
        Gdx.input.setInputProcessor(stage);
    }   

    @Override
    public void hide() {
        // TODO Auto-generated method stub

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

}

btnPlay.java

package com.spiderhunt.buttons;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.spiderhunt.Assets;


public class btnPlay extends Actor {

    public btnPlay(){
        setSize(100, 40);
        setPosition(100, 100);
    }

    public void draw(SpriteBatch batch, float parentAlpha) {    
        //!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
        batch.setColor(getColor());        
        batch.draw(Assets.btnPlayRegion, 0, 0);
    }
}

Assets.java

package com.spiderhunt;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Assets {
    public static Texture atlas;    
//backgrounds
    public static TextureRegion bgMenuRegion;
    public static TextureRegion bgSelectLevelRegion;
//buttons
    public static TextureRegion btnPlayRegion;
//objects
    public static TextureRegion objFlyRegion;

    public static void load(){
        atlas = new Texture("atlas.png");

        bgMenuRegion = new TextureRegion(atlas, 0, 0, 540, 960);
        btnPlayRegion = new TextureRegion(atlas, 1111, 1244, 418, 112);
    }
}
2
"not called" as in your code is not executed or there is nothing visible? have you tried to batch.begin/end around your button's draw call?cfrick
and you have overwritten all your methods in your Game class with empty methods. For shorter code? Otherwise nothing at all will render.cfrick

2 Answers

5
votes

Thanks for the help.

And now i find my bug.

I replace SpriteBatch to Batch and it work.

change

public void draw(SpriteBatch batch, float parentAlpha) {    
        //!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
        batch.setColor(getColor());        
        batch.draw(Assets.btnPlayRegion, 0, 0);
}

to

@Override
public void draw(Batch batch, float parentAlpha) {    
        //!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
        batch.setColor(getColor());        
        batch.draw(Assets.btnPlayRegion, 0, 0);
}
0
votes

The problem I believe is at this line:

new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);

Remove batch from the Stage Initialization change it to this:

new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));

You are passing the batch from the Screen to the stage and you are calling batch.end() before stage.draw(). However stage has its own batch so you do not have to pass a batch for the stage to draw. If for your own reasons (e.g. batch projection matrix configuration) you still want your Screen batch passed to the stage do not call batch.end() before stage.draw() since your batch of the stage and your main batch are the same. Call batch.end() after stage.draw()