Libgdx sliding pause menu - java

So in my Gameplay screen user has posibillity to click on menu button and then that menu button will pause the game and show the menu.
I tried to impelement menu as a new stage, but that just opens a new screen, sets the graphics of the menu like I want to and leaves the rest of the screen black.
Here's the image so you can see what I'm talking about:
I want this green part to smoothly slide into Gameplay scene and I wan't to get rid of this black part of the screen and instead leave that part transparent (I want it to show only the green part of a Image).
Tried to find some good tutorials about pause menus, but no luck.
Here's the code of my pause screen:
public class Menu implements Screen{
Stage menuStage = null;
private Image menu_bg = null;
private Main game = null;
public Menu(Main gm){
game = gm;
}
#Override
public void show() {
menuStage = new Stage(new StretchViewport(1920, 1080));
Gdx.input.setInputProcessor(menuStage);
menu_bg = new Image(new Texture(Gdx.files.internal("menuProba.png")));
menuStage.addActor(menu_bg);
menuButtons();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
menuStage.draw();
menuStage.act();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
private void menuButtons(){
Table menuButtons = new Table();
menuButtons.setFillParent(true);
final Image resumeGame = new Image(new Texture(Gdx.files.internal("playbutton.png")));
final Image retryGame = new Image(new Texture(Gdx.files.internal("retrybutton.png")));
final Image homeButton = new Image(new Texture(Gdx.files.internal("homebutton.png")));
final Image exitButton = new Image(new Texture(Gdx.files.internal("exitbutton.png")));
resumeGame.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
resumeGame.addAction(Actions.scaleTo(1, 1.1f,.1f));
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button){
resumeGame.addAction(Actions.scaleTo(1, 1,.1f));
}
});
retryGame.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
retryGame.addAction(Actions.scaleTo(1, 1.1f,.1f));
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button){
retryGame.addAction(Actions.scaleTo(1, 1,.1f));
}
});
homeButton.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
homeButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button){
homeButton.addAction(Actions.scaleTo(1, 1,.1f));
game.setScreen(new MainMenu(game));
}
});
exitButton.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
exitButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button){
exitButton.addAction(Actions.scaleTo(1, 1,.1f));
}
});
/*HorizontalGroup horizontalGroup = new HorizontalGroup();
horizontalGroup.addActor(resumeGame);
horizontalGroup.addActor(retryGame);
horizontalGroup.addActor(homeButton);
horizontalGroup.addActor(exitButton);
menuButtons.add(horizontalGroup);*/
menuButtons.add(resumeGame).expand().left().padLeft(40);
menuButtons.row();
menuButtons.add(retryGame).expand().left().padLeft(40);
menuButtons.row();
menuButtons.add(homeButton).expand().left().padLeft(40);
menuButtons.row();
menuButtons.add(exitButton).expand().left().padLeft(40);
menuStage.addActor(menuButtons);
}
}
And the Gameplay screen:
public class GameScreen implements Screen, InputProcessor {
boolean menuScreen;
private Texture menuImage = null;
public boolean buttonMenuTouched = false;
public InputMultiplexer multiplexer = null;
BitmapFont font;
//SCORE-------------------------------------
private SpriteBatch batch = null;
private OrthographicCamera mCamera = null;
private BitmapFont scoreFont = null;
private int score = 0;
//--------------------------------------------
Main game = null;
public Texture font_texture;
public GameScreen(Main gm){
game = gm;
}
Stage gameStage = null;
private Image game_bg = null, menu_bg = null;
private GameManager manager = null;
#Override
public void show() {
mCamera = new OrthographicCamera(1920, 1080);
font_texture = new Texture(Gdx.files.internal("font.png"));
font_texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
scoreFont = new BitmapFont(Gdx.files.internal("font.fnt"), new TextureRegion(font_texture), false);
batch = new SpriteBatch();
multiplexer = new InputMultiplexer();
gameStage = new Stage(new StretchViewport(1920, 1080));
multiplexer.addProcessor(this);
multiplexer.addProcessor(gameStage);
Gdx.input.setInputProcessor(multiplexer);
game_bg = new Image(new Texture(Gdx.files.internal("pozadinaGameScreen.png")));
gameStage.addActor(game_bg);
menuImage = new Texture("menuProba.png");
manager = new GameManager(game, this, gameStage);
manager.createPlayer();
manager.createBlueMonster();
manager.createHUDButtons();
manager.createGameOverStage();
gameScreenButtoni();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameStage.draw();
gameStage.act();
manager.checkButtons();
manager.checkCollisions();
batch.setProjectionMatrix(mCamera.combined);
batch.begin();
this.scoreFont.draw(batch, ""+manager.score, Gdx.graphics.getWidth() - 1070, Gdx.graphics.getHeight() - 580);
batch.end();
if (manager.gameOver == true){
manager.gameOverStage.draw();
manager.gameOverStage.act();
Gdx.input.setInputProcessor(manager.gameOverStage);
}
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
manager.gameOverStage.dispose();
gameStage.dispose();
manager.dispose();
game.setScreen(game.mainMenu);
}
// METODE INPUT PROCESORA
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
manager.shootBullet();
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
public void gameScreenButtoni(){
//menuImage = new Image(new Texture(Gdx.files.internal("menuProba.png")));
Table buttoni = new Table();
buttoni.setFillParent(true);
final Image menuButton = new Image(new Texture(Gdx.files.internal("menubutton.png")));
menuButton.addListener(new InputListener(){
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
menuButton.addAction(Actions.scaleTo(1, 1.1f,.1f));
buttonMenuTouched = true;
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button){
menuButton.addAction(Actions.scaleTo(1, 1,.1f));
buttonMenuTouched = false;
game.setScreen(new Menu(game));
}
});
final Image scoreText = new Image(new Texture(Gdx.files.internal("score.png")));
buttoni.add(menuButton).expand().top().left().padLeft(20).padTop(20);
buttoni.add(scoreText).expand().top().right().padTop(30).padRight(140);
gameStage.addActor(buttoni);
}
public void menuScreen(){
Stage menu = new Stage(new StretchViewport(400,400));
menu_bg = new Image(new Texture(Gdx.files.internal("logoMali.png")));
menu.addActor(menu_bg);
}
}
I know that I'm doing this wrong, but how should I do it? Draw a rectangle when button is pressed or what?

In my game I make NavigationDrawer in libgdx as you can see:
You can make this with sample code, just follow a few steps:
1- Copy NavigationDrawer class into your project:
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
/**
* Created by Crowni on 10/6/2015.
*/
public class NavigationDrawer {
// this value is suitable
private static final float INTERVAL_SEC = 0.001f;
// Some attributes for {#link NavigationDrawer}
private static float CAM_MAIN_POSITION;
public static float WIDTH;
public static float SPEED;
private static OrthographicCamera camera;
/**
* To ensure the {#link NavigationDrawerListener#onFinish()} called one time
**/
private static boolean isFinish = true;
private static Task task;
private static Timer timer = new Timer();
private static NavigationDrawerListener listener;
public interface NavigationDrawerListener {
public void onStart();
// May be you want to make some Actions here during sliding
public void onRun();
public void onFinish(float camX);
}
public static void initialize(Stage stage, NavigationDrawerListener listener) {
NavigationDrawer.listener = listener;
camera = ((OrthographicCamera) stage.getCamera());
setNavigationDrawerDefault(stage);
initializeTimerTask(false);
}
private static void setNavigationDrawerDefault(Stage stage) {
WIDTH = stage.getWidth() - stage.getWidth() / 3;
CAM_MAIN_POSITION = stage.getWidth() / 2;
SPEED = 2f;
}
private static Task initializeTimerTask(boolean show) {
task = new Task() {
public void run() {
if (!timer.isEmpty()) {
if (show)
camera.position.x -= SPEED;
else
camera.position.x += SPEED;
listener.onRun();
} else if (isFinish) {
listener.onFinish(setDefaultCameraEndPostion(show));
}
}
};
return task;
}
/**
* #param show
* : I passed it here because I know it is correct choice
**/
private static float setDefaultCameraEndPostion(boolean show) {
isFinish = false;
if (show)
return camera.position.x = CAM_MAIN_POSITION - WIDTH;
else
return camera.position.x = CAM_MAIN_POSITION;
}
private static boolean isOpened() {
return camera.position.x != CAM_MAIN_POSITION;
}
public static void show(boolean show) {
if ((isOpened() && !show) || (!isOpened() && show))
startTask(show);
}
private static void startTask(boolean show) {
if (timer.isEmpty()) {
isFinish = true;
listener.onStart();
timer.scheduleTask(initializeTimerTask(show), 0f, INTERVAL_SEC,
((int) (WIDTH / SPEED)));
}
}
}
2- In your Screen write the following:
#Override
public void show() {
stage = new Stage(new StretchViewport(1080, 1920));
// May be you want to make some Actions with NavigationDrawer state
NavigationDrawerListener listener = new NavigationDrawerListener() {
#Override
public void onStart() {
System.out.println("onStart");
}
#Override
public void onRun() {
System.out.println("onRun");
}
#Override
public void onFinish(float camX) {
System.out.println("onFinish: " + camX);
}
};
// You must be initialize NavigationDrawer Firstly
NavigationDrawer.initialize(stage, listener);
// This image is sample to show how navigationDrawer look like on the screen
Image background= new Image(new Texture(Gdx.files.internal("background.jpg")));
background.addListener(new ClickListener() {
private int clicked = 0;
public void clicked(InputEvent event, float x, float y) {
if (clicked % 2 == 0) {
clicked++;
NavigationDrawer.show(true);
} else {
clicked++;
NavigationDrawer.show(false);
}
}
});
background.setFillParent(true);
stage.addActor(background);
Gdx.input.setInputProcessor(stage);
}
3- The result of sample code look like that:
4- Finally you can create images, labels, ... actors in navigation width which their positions into off-screen i.e. with negative positions. Enjoy!
UPDATE
This Navigation Drawer More performance and beauty sliding.

Related

Libgdx Input don't working on Android

I'm new in Libgdx and I'm trying to make a map that can be explored using Camera. Fo that I implements GestureListener in my own Map class.
public class Map extends Stage implements GestureListener {
public String mapName;
private Sprite background;
public LocationPoint points[];
private OrthographicCamera camera;
private Batch batch;
public Music anbientSound;
public int numOfPoints;
public int locationsX[];
public int locationsY[];
public Map(Sprite background) {
this.background = background;
}
public Sprite getBackground() {
return background;
}
public void activate() {
InputMultiplexer inputChain = new InputMultiplexer();
if(points==null) {
points = new LocationPoint[numOfPoints];
for(int i = 0; i < numOfPoints; i++) {
points[i] = new LocationPoint(locationsX[i], locationsY[i]);
addActor(points[i]);
}
}
batch = GameUtils.batch;
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0);
camera.update();
Music music = GameUtils.addMusic(anbientSound);
music.setLooping(true);
music.play();
inputChain.addProcessor(this);
inputChain.addProcessor(new GestureDetector(this));
Gdx.input.setInputProcessor(inputChain);
}
public void draw() {
Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(Gdx.gl20.GL_COLOR_BUFFER_BIT);
Batch batch = this.batch;
batch.setProjectionMatrix(camera.combined);
batch.begin();
background.draw(batch);
batch.end();
batch.begin();
for(int i = 0; i < numOfPoints; i++) {
points[i].draw(batch, 1);
addActor(points[i]);
}
batch.end();
}
public void dispose() {
GameUtils.stopMusic();
background.getTexture().dispose();
anbientSound.dispose();
}
#Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
camera.translate(-deltaX, deltaY);
float initialX = camera.viewportWidth / 2;
float initialY = camera.viewportHeight / 2;
GameUtils.limitBound(camera, initialX, initialY, background.getWidth(), background.getHeight());
camera.update();
return true;
}
#Override
public boolean touchDown(float x, float y, int pointer, int button) {
return false;
}
#Override
public boolean tap(float x, float y, int count, int button) {
return false;
}
#Override
public boolean longPress(float x, float y) {
return false;
}
#Override
public boolean fling(float velocityX, float velocityY, int button) {
return false;
}
#Override
public boolean panStop(float x, float y, int pointer, int button) {
return false;
}
#Override
public boolean zoom(float initialDistance, float distance) {
return false;
}
#Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
Vector2 pointer1, Vector2 pointer2) {
return false;
}
#Override
public void pinchStop() {}
}
The method activate() is used to activate the resources of the Map class. The class Map also have a ImageButtons called LocationPoints.
public class LocationPoint extends ImageButton {
private Monster monster;
private Trap trap;
public boolean occuped;
public boolean isTrap;
public int f = 20;
public int k = 20;
public LocationPoint(float x, float y) {
super(GameUtils.getLocationDrawable());
this.setSize(46, 46);
setPosition(x, y);
addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
setPosition(f, k);
f += 10;
k += 10;
return super.touchDown(event, x, y, pointer, button);
}
});
}
public void occup(Monster monster) {
this.monster = monster;
occuped = true;
if(isTrap)
captured();
}
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
}
public void empty() {
monster = null;
occuped = false;
}
public void captured() {
monster.capture();
}
public Monster getMonster() {
return monster;
}
}
In LocationPoint class I add a InputListener to make a thing when that LocationPoint is touched.
When a play the game on android both LocationPoints event and the Map pan event. But after I move the camera, when I touch on LocationPoint it don't fires that point event.
But when I return the camera to initial position the LocationPoints events works fine! Can you help me? (And sorry for my broken english...)
Actor is already a child so remove redundant call from draw() method.
for(int i = 0; i < numOfPoints; i++) {
points[i].draw(batch, 1);
addActor(points[i]); // <-- Not Should be in draw() call
}
Stage having own SpriteBatch that created by default constructor, use that one or pass own batch in Stage constructor.
Use getBatch() method of Stage if you want to draw something by yourself.
Creating camera in Map class also redundant, use getViewport().getCamera() that return camera of stage, you can typecast to OrthographicCamera
call super.dispose(); inside your dispose() method
Drawing all your Actor by yourself ? If you're not doing something beyond the scope, no need to override draw() method of Stage.

libgdx input method not called

I have a screen class which sets the input processor to the stage class Gdx.input.setInputProcessor(gameStage);, and my stage class implements inputProcessor public class GameStage extends Stage implements InputProcessor. The input methods in my stage class are not called and I can't figure out why. I tested it with this method
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
batch.begin();
font.draw(batch, "text", 200, 50);
batch.end();
return super.touchDown(screenX, screenY, pointer, button);
}
But the text is not drawn, when I try it with Gdx.input.isTouched() in the act method, it does work.
Thanks
EDIT: All the code
public class GameScreen implements Screen {
private Game game;
private GameStage gameStage;
private OrthographicCamera camera;
private Viewport viewport;
private Box2DDebugRenderer renderer;
public GameScreen(Game game) {
this.game = game;
this.camera = new OrthographicCamera();
this.viewport = new ScreenViewport(camera);
this.gameStage = new GameStage(viewport);
this.renderer = new Box2DDebugRenderer();
Gdx.input.setInputProcessor(gameStage);
}
public OrthographicCamera getCamera() {
return this.camera;
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(247/255.0f, 247/255.0f, 247/255.0f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gameStage.act();
renderer.render(gameStage.getWorld(), camera.combined);
gameStage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
Stage class:
public class GameStage extends Stage {
private World world;
private Player player;
private BitmapFont font;
private Box2DDebugRenderer renderer;
public GameStage(Viewport viewport) {
super(viewport);
font = new BitmapFont();
setUpWorld();
}
public void setUpWorld() {
world = WorldUtils.createWorld();
player = new Player(WorldUtils.createPlayer(world));
}
#Override
public void draw() {
super.draw();
getBatch().begin();
font.draw(getBatch(), "Position: X=" + player.getX() + " Y=" + player.getY(), 200, 50);
getBatch().end();
}
#Override
public void act(float delta) {
super.act(delta);
world.step(1 / 300f, 6, 2);
}
public World getWorld() {
return this.world;
}
public Player getPlayer() {
return this.player;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
player.move();
super.touchDown(screenX, screenY, pointer, button);
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return super.touchDragged(screenX, screenY, pointer);
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return super.touchUp(screenX, screenY, pointer, button);
}
}
Player class:
public class Player extends Actor{
private Body body;
public Player(Body body) {
this.body = body;
}
public void move() {
body.applyForceToCenter(50000, 200, true);
}
}
Don't draw inside your input handlers. Just store the coordinates. It's not working because you're trying to draw outside of the game loop. Also return false not the parent method, when you return true the input processor calls all the other matched input handlers. Something like this should do it.
boolean touched;
void draw(Batch batch)
if(touched){
batch.begin();
font.draw(batch, "text", 200, 50);
batch.end();
}
}
#Override
public boolean touchDown(int screenX, int screenY, int
pointer , int button) {
super.touchDown(screenX, screenY, pointer, button);
touched = true;
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int
pointer , int button) {
super.touchUp(screenX, screenY, pointer, button);
touched = false;
return false;
}
Edit: Also remove implements InputProcessor The stage class already implements that class so you don't have to. This is probably what causing you handlers to be ignored. You're set input processor of the parent class as main handler but you're override the input handlers of interface not the parent so they aren't being seen.
Edit 2: Try implementing InputProcessor and not extending Stage.
public class GameStage InputProcessor {
private Stage stage;
private InputMultiplexer input;
private World world;
private Player player;
private BitmapFont font;
private Box2DDebugRenderer renderer;
public GameStage(Viewport viewport) {
stage = new Stage(viewport);
font = new BitmapFont();
setUpWorld();
input = new InputMultiplexer();
input.addProcessor(this);
input.addProcessor(stage);
}
public void setUpWorld() {
world = WorldUtils.createWorld();
player = new Player(WorldUtils.createPlayer(world));
stage.addActor(player)
}
#Override
public void draw() {
stage.draw();
getBatch().begin();
font.draw(getBatch(), "Position: X=" + player.getX() + " Y=" + player.getY(), 200, 50);
getBatch().end();
}
#Override
public void act(float delta) {
stage.act(delta);
world.step(1 / 300f, 6, 2);
}
public World getWorld() {
return this.world;
}
public Player getPlayer() {
return this.player;
}
public InputAdapter getInput(){
return this.input;
}
public Stage getStage(){
return this stage;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
player.move();
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return true;
}
}
If you want to allow the stage to respond to user input as well as respond to user input yourself you need to use an input multiplexer.
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(myStage); // allows stage actors to respond to user input
multiplexer.addProcessor(new MyGameInputProcessor()); // allows you to repond to user actions
Gdx.input.setInputProcessor(multiplexer);
Hopefully you're doing in this way :
public class GdxTest extends Game {
MyStage myStage;
#Override
public void create() {
myStage =new MyStage();
Gdx.input.setInputProcessor(myStage);
}
#Override
public void render() {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
myStage.draw();
myStage.act();
}
}
And MyStage class
public class MyStage extends Stage {
BitmapFont font;
public MyStage(){
font=new BitmapFont();
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
Gdx.app.log("MyStage","Inside touchDown method");
drawText();
return super.touchDown(screenX, screenY, pointer, button);
}
#Override
public void act() {
super.act();
//if(Gdx.input.isTouched()) {
// drawText();
//}
}
public void drawText(){
getBatch().begin();
font.draw(getBatch(), "text", 200, 50);
getBatch().end();
}
}
According to you : text is not drawing when you're calling drawText() from touchDown(..) method but if you're calling it from act() method, you're getting Text on Screen.
Reason why text not rendering :
Whenever you touch screen drawText() method called from touchDown(..) method but that will be cleared by next render() call so you got noting on screen but on the other hand If you call drawText() from act() method that actually called by render() method of ApplicationListener so text draw after clearing the frame buffers and setting the color buffer.
Solution :
You need to call drawText() on each render call so override draw() method of MyStage and call drawText() using flag.
boolean drawText;
#Override
public void draw() {
super.draw();
if(drawText) {
drawText();
}
}
and change flag value in touchDown(..) method
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
drawText=true;
return super.touchDown(screenX, screenY, pointer, button);
}
Two recommendation :
Stage class already implement InputProcessor by his adapter class so need to implement again.
Stage class having own private batch reference so no need to create your own, you can use getBatch() method.

Libgdx sceene2d and Input Processor not updating screen

I am rather new to the libgdx Framework so I hope I am not asking anything stupid, but I have a problem with updating my screen on the toucheEvent. It seems that the touch event fires, but the stage is not updated so the screen is all the time the same. Here is the code
MainClass
public class MainGame implements Screen {
public LabirintGame game;
public Stage stage;
public OrthographicCamera camera;
public ActorM rigth;
public ActorM wrong;
public MainGame(LabirintGame game) {
this.game = game;
this.camera = new OrthographicCamera();
}
#Override
public void show() {
this.camera.setToOrtho(false, 800, 480);
stage = new Stage(new ScreenViewport());
stage.clear();
Words group = new Words(stage);
InputMultiplexer inputMultiplexer = new InputMultiplexer();
inputMultiplexer.addProcessor(stage);
inputMultiplexer.addProcessor(new MyInputProcessor(stage, camera));
Gdx.input.setInputProcessor(inputMultiplexer);
//Add wrong and rigth boxes
rigth = new ActorM("box", 0, 0, 200,200);
wrong = new ActorM("box",(game.width - 230), 0, 200, 200);
wrong.moveBy(200,200);
Button createButtons = new Button();
createButtons.setStyle("atlas-besede/besede.atlas", "buttonOff", "buttonOn");
TextButton ValidationButton = createButtons.createButton("Validate", (game.width/2), 0, 150, 150);
ValidationButton.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
game.setScreen(new Labirint(game));
return true;
}
});
stage.addActor(ValidationButton);
stage.addActor(rigth);
stage.addActor(wrong);
List<String> backgrounds = Arrays.asList("s", "z");
for (int i = 0; i < backgrounds.size(); i++) {
Word actor = new Word(backgrounds.get(i),(i + 1) * 300, 300, 100, 100);
actor.setPosition((i + 1) * 300, 300);
actor.setName(backgrounds.get(i));
group.addActor(actor);
}
stage.addActor(group);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
game.batch.begin();
game.batch.setProjectionMatrix(camera.combined);
game.batch.end();
}
ActorM
package com.mygdx.game;
public class ActorM extends Actor {
public SpriteBatch batch;
public TextureAtlas atlas;
public TextureAtlas.AtlasRegion region;
Sprite sprite;
public int x;
public int y;
public int width;
public int height;
public ActorM(String actorName, int x, int y, int width, int height) {
//this.region = region;
super();
batch = new SpriteBatch();
atlas = new TextureAtlas(Gdx.files.internal("atlas-start/atlas-start.atlas"));
sprite = atlas.createSprite(actorName);
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.setBounds(0, 0, sprite.getWidth(), sprite.getHeight());
setTouchable(Touchable.disabled);
setName(actorName);
setPosition(x,y);
}
#Override
public void draw (Batch batch, float parentAlpha) {
batch.draw(sprite, x,y, width, height);
}
public void move(int posX){
this.x = this.x + posX;
}
}
MyInputProcessor
public class MyInputProcessor implements InputProcessor {
private OrthographicCamera camera;
private Stage stage;
private Vector2 coordinates;
private Music sound;
public MyInputProcessor( Stage stage, OrthographicCamera camera) {
this.stage = stage;
this.camera = camera;
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)
{
//Gdx.app.log("", "x " + screenX + " y " + screen`enter code here`Y + " pointer " + pointer);
Vector2 coordinates = stage.screenToStageCoordinates(new Vector2((float)screenX,(float)screenY));
Actor hitactor = stage.hit(coordinates.x, coordinates.y, true);
Gdx.app.log("", coordinates.toString());
if (hitactor != null){
//Gdx.app.log("", "HIT" + hitactor.getName());
Gdx.app.log("", "HIT" + hitactor.getRotation());
hitactor.setRotation(hitactor.getRotation() + 1f);
hitactor.setPosition(5,5);
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
coordinates = stage.screenToStageCoordinates(new Vector2((float)screenX,(float)screenY));
Actor hitactor = stage.hit(coordinates.x, coordinates.y, true);
if (hitactor != null){
Gdx.app.log("", "Drag");
hitactor.setRotation(hitactor.getRotation() + 1f);
}
camera.update();
return true;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}
Your "ValidationButton" uses an InputProcessor that always returns true and it's the first actor in the stage, so nothing else in the stage will ever get an opportunity to respond to touch down events. Furthermore, since your stage is the first input processor in your InputMultiplexer, your other input processor never gets an opportunity to respond to touch down events either.
You should use an EventListener on your button instead of an InputListener, so much of the logic will be taken care of for you.
By the way, your ActorM class is spawning a SpriteBatch that it never uses. SpriteBatch takes up significant memory, and there's no need for there to be more than one of them in your game. The Stage already has a reference to a SpriteBatch that it passes into your Actor's draw method, so the Actor does not need to create or even reference a SpriteBatch.
Also, your ActorM class is loading a complete copy of a TextureAtlas for itself so there will be duplicate Textures loaded for each instance of ActorM, and you lose all the benefits of a TextureAtlas, since you won't be using it for sprite batching. You need to load the TextureAtlas only one time, and pass a reference of it into the constructor of your ActorM class, so they can all share the same Texture.

LibGDX, flappy bird clone, creating BUTTON

I am developing a flappy bird clone, and I really got stuck with this button that I want to create. I am sorry, I know this is a basic question, but I am beginner and I really need your help. So, the game is pretty much finished, but instead of touching all over the screen for a bird to jump, I want to make a button that will do the same thing. Here is my InputHandler class:
public class InputHandler implements InputProcessor {
private Bird myBird;
private GameWorld myWorld;
// Ask for a reference to the Bird when InputHandler is created.
public InputHandler(GameWorld myWorld) {
// myBird now represents the gameWorld's bird.
this.myWorld = myWorld;
myBird = myWorld.getBird();
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if (myWorld.isReady()) {
myWorld.start();
}
myBird.onClick();
if (myWorld.isGameOver() || myWorld.isHighScore()) {
// Reset all variables, go to GameState.READ
myWorld.restart();
}
return true;
}
#Override
public boolean keyDown(int keycode) {
return false;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
#Override
public boolean scrolled(int amount) {
return false;
}
}
I created a stage and an actor (button) in GameScreen class (it is not working), and it shows on screen, but I don't know what to do next. Again, I am sorry for such a basic question, but I really need your help to finish this game.
public class GameScreen implements Screen {
private OrthographicCamera cam;
private int gameHeight;
Stage stage;
TextureAtlas buttonAtlas;
TextButtonStyle buttonStyle;
TextButton button;
Skin skin;
BitmapFont font;
SpriteBatch batch;
private GameWorld world;
private GameRenderer renderer;
private float runTime;
// This is the constructor, not the class declaration
public GameScreen() {
float screenWidth = Gdx.graphics.getWidth();
float screenHeight = Gdx.graphics.getHeight();
float gameWidth = 544;
float gameHeight = screenHeight / (screenWidth / gameWidth);
world = new GameWorld();
renderer = new GameRenderer(world, (int) gameHeight, (int) gameWidth);
Gdx.input.setInputProcessor(new InputHandler(world));
}
#Override
public void render(float delta) {
runTime += delta;
world.update(delta);
renderer.render(runTime);
cam = new OrthographicCamera();
cam.setToOrtho(false, 544, gameHeight);
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
stage.act();
batch.begin();
stage.draw();
batch.end();
}
#Override
public void resize(int width, int height) {
System.out.println("GameScreen - resizing");
}
#Override
public void show() {
System.out.println("GameScreen - show called");
stage = new Stage (Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
font = new BitmapFont(Gdx.files.internal("data/font.fnt"), false);
skin = new Skin();
buttonAtlas = new TextureAtlas ("data/button.pack");
skin.addRegions(buttonAtlas);
buttonStyle = new TextButtonStyle();
buttonStyle.up = skin.getDrawable("button");
buttonStyle.over = skin.getDrawable("button - Copy");
buttonStyle.down = skin.getDrawable("button - Copy");
buttonStyle.font = font;
button = new TextButton("" ,buttonStyle);
stage.addActor(button);
}
#Override
public void hide() {
System.out.println("GameScreen - hide called");
}
#Override
public void pause() {
System.out.println("GameScreen - pause called");
}
#Override
public void resume() {
System.out.println("GameScreen - resume called");
}
#Override
public void dispose() {
// Leave blank
}
}
You can pretty much get rid of anything that references general touch input. Once you've done that, you need to make your button's OnClick() method call the method that "bounces" the birdie. If you've given this function a method of it's own, it should be relatively easy.
To pull away from "doing it for you", I'll show you an example in C#:
boingButton_Click(Object sender, EventArgs e)
{
bird.Bounce();
}
And the bounce method as such:
Bounce()
{
bird.Height += 4;
bird.Descend();
}
This would be assuming you have a method for the bird's descent. The advantage with the button is that ideally you shouldn't need any overloads for it's method, although I may be wrong with Java.
Wolfish's idea is right. Translated into Libgdx framework would look as follows:
btnBounce.addListener( new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
bird.bounce();
};
});

LIBGDX having more than 1 button on the main menu

i have 5 buttons i want to put on the main menu but when i have all 5 actors it doesn't work and when i take them all out and leave one in it works.
How do get around having multiple buttons?
Here is my code now
public class MainMenu implements Screen {
CrazyZombies game;
Stage stage;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
Button play, option, quit, custom, store, menu;
public MainMenu(CrazyZombies game) {
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
batch.begin();
batch.end();
}
#Override
public void resize(int width, int height) {
if (stage == null)
stage = new Stage(width, height, true);
stage.clear();
Gdx.input.setInputProcessor(stage);
/**
* quit Button
*/
TextButtonStyle styleQuit = new TextButtonStyle();
styleQuit.up = skin.getDrawable("8layer");
styleQuit.down = skin.getDrawable("8layer");
quit = new Button(styleQuit);
quit.setWidth(854);
quit.setHeight(480);
quit.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
}
});
/**
* End quit Button
*/
/**
* store Button
*/
TextButtonStyle styleStore = new TextButtonStyle();
styleStore.up = skin.getDrawable("9layer");
styleStore.down = skin.getDrawable("9layer");
store = new Button(styleStore);
store.setWidth(854);
store.setHeight(480);
store.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(new StoreScreen(game));
}
});
/**
* End store Button
*/
/**
* customs Button
*/
TextButtonStyle styleCustom = new TextButtonStyle();
styleCustom.up = skin.getDrawable("10layer");
styleCustom.down = skin.getDrawable("10layer");
custom = new Button(styleCustom);
custom.setWidth(854);
custom.setHeight(480);
custom.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(new CustomScreen(game));
}
});
/**
* End customs Button
*/
/**
* Options Button
*/
TextButtonStyle styleOptions = new TextButtonStyle();
styleOptions.up = skin.getDrawable("11layer");
styleOptions.down = skin.getDrawable("11layer");
option = new Button(styleOptions);
option.setWidth(854);
option.setHeight(480);
option.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
game.setScreen(new OptionScreen(game));
}
});
/**
* End Options Button
*/
/**
* Play Button
*/
TextButtonStyle stylePlay = new TextButtonStyle();
stylePlay.up = skin.getDrawable("7layer");
stylePlay.down = skin.getDrawable("7layer");
play = new Button(stylePlay);
play.setWidth(854);
play.setHeight(480);
play.addListener(new InputListener(){
});
play.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
return true;
}
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
Gdx.app.log(CrazyZombies.LOG, "un-touched");
game.setScreen(new GameScreen(game));
}
});
/**
* End Play Button
*/
/**
* start Background
*/
TextButtonStyle styleMenu = new TextButtonStyle();
styleMenu.up = skin.getDrawable("background");
menu = new Button(styleMenu);
menu.setWidth(854);
menu.setHeight(480);
/**
* End Background
*/
//stage.addActor(menu);
stage.addActor(play);
//stage.addActor(option);
//stage.addActor(store);
//stage.addActor(custom);
//stage.addActor(quit);
}
#Override
public void show() {
Audio.playMusic(true);
batch = new SpriteBatch();
atlas = new TextureAtlas("data/mainmenu/mainmenu.pack");
skin = new Skin();
skin.addRegions(atlas);
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
skin.dispose();
atlas.dispose();
stage.dispose();
Audio.dispose();
}
}
Create a Table object.
Add all buttons into it.
Add that Table object to the stage.
Hope it helps. Good luck.

Categories