I am following a video tutorial on a java game ( so I can go off from there ) but one of my buttons will not work (exit button). Please Help. I'm using lwjgl and slick.
main class:
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame {
public static final String gamename = "Java Game Alpha 1.0";
public static final int menu = 0;
public static final int play = 1;
public Game(String gamename) {
super(gamename);
this.addState(new Menu(menu));
this.addState(new Play(play));
}
public void initStatesList(GameContainer gc) throws SlickException{
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);
this.enterState(menu);
}
public static void main(String[] args) {
AppGameContainer appgc;
try{
appgc = new AppGameContainer(new Game(gamename));
appgc.setDisplayMode(1600, 800, false);
appgc.start();
}catch(SlickException e) {
e.printStackTrace();
}
}
}
menu class:
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.state.StateBasedGame;
public class Menu implements GameState {
Image PlayNow;
Image exitGame;
public Menu(int state){
}
#Override
public void mouseClicked(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void mouseDragged(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void mouseMoved(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void mouseWheelMoved(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void inputEnded() {
// TODO Auto-generated method stub
}
#Override
public void inputStarted() {
// TODO Auto-generated method stub
}
#Override
public boolean isAcceptingInput() {
// TODO Auto-generated method stub
return false;
}
#Override
public void setInput(Input arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(int arg0, char arg1) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(int arg0, char arg1) {
// TODO Auto-generated method stub
}
#Override
public void controllerButtonPressed(int arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void controllerButtonReleased(int arg0, int arg1) {
// TODO Auto-generated method stub
}
#Override
public void controllerDownPressed(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerDownReleased(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerLeftPressed(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerLeftReleased(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerRightPressed(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerRightReleased(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerUpPressed(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void controllerUpReleased(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void enter(GameContainer arg0, StateBasedGame arg1)
throws SlickException {
// TODO Auto-generated method stub
}
#Override
public int getID() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void init(GameContainer gc, StateBasedGame sbg)
throws SlickException {
// TODO Auto-generated method stub
PlayNow = new Image ("res/PlayNow.png");
exitGame = new Image ("res/exitGame.png");
}
#Override
public void leave(GameContainer arg0, StateBasedGame arg1)
throws SlickException {
// TODO Auto-generated method stub
}
#Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException {
// TODO Auto-generated method stub
g.drawString("It's time for an adventure!", 50, 50);
//g.drawRect(730, 320, 70, 80); //x, y, width, height
PlayNow.draw(680, 320, 250, 50);
exitGame.draw(680, 380, 250, 50);
int xpos = Mouse.getX();
int ypos = Mouse.getY();
g.drawString("Mouse Position: " + xpos + " " + ypos, 10, 22);
}
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException {
// TODO Auto-generated method stub
int xpos = Mouse.getX();
int ypos = Mouse.getY();
Input input = gc.getInput();
//play now
if( (xpos > 683) && (xpos < 920) && (ypos > 440) && (ypos < 478) ) {
if(Mouse.isButtonDown(0)) {
sbg.enterState(1);
}
//exit Game
if ( (xpos > 683) && (xpos < 920) && (ypos > 379) && (ypos < 417) ) {
if(Mouse.isButtonDown(0)) {
gc.exit();
}
}
}
}
}
and there is one more class, but it doesn't matter. Please help!
The problem is your second if statement is nested within the first. So, the first statement must be true for the second to exit. Unfortunately, for the first statement to be true, the second can never be true. So the second never executes.
The first if requires ypos>440 && ypos<478. The second if requires ypos>379 && <417. There is no overlap between those 2 ranges, and so the second if can never execute. Here's your code with cleaner formatting.
if( (xpos > 683) && (xpos < 920) && (ypos > 440) && (ypos < 478) ) {
if(Mouse.isButtonDown(0)) {
sbg.enterState(1);
}
//ypos will never be less than 417 because to reach this point it MUST
//be greater than 440 due to first if statement.
if ( (xpos > 683) && (xpos < 920) && (ypos > 379) && (ypos < 417) ) {
if(Mouse.isButtonDown(0)) {
gc.exit();
}
}
}
And here's the solution:
if( (xpos > 683) && (xpos < 920) && (ypos > 440) && (ypos < 478) ) {
if(Mouse.isButtonDown(0)) {
sbg.enterState(1);
}
}
//exit Game
if ( (xpos > 683) && (xpos < 920) && (ypos > 379) && (ypos < 417) ) {
if(Mouse.isButtonDown(0)) {
gc.exit();
}
}
Now the second if is independent of the first.
Related
This is my code, I want to add functionality to the Rect that I made, like when you click on it, a window pops up
public class codetwo extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.LIGHT_GRAY);
g.setColor(Color.RED);
g.fillRect(110,110, 120, 120);
}
public static void main(String args[]) {
codetwo cd = new codetwo();
JFrame frame = new JFrame("Title");
frame.add(cd);
frame.setSize(440,350);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You need to use Mouselistener for this.
public class codetwo extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.LIGHT_GRAY);
g.setColor(Color.RED);
g.fillRect(110,110, 120, 120);
}
public static void main(String args[]) {
codetwo cd = new codetwo();
cd.addMouseListener(new MouseListener(){
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (e.getX() >= 110 && e.getX() <= 230 && e.getY() >= 110 && e.getY() <= 230) {
JOptionPane.showMessageDialog(cd, "Some one clicked here ?", "Listening", JOptionPane.INFORMATION_MESSAGE);
}
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
JFrame frame = new JFrame("Title");
frame.add(cd);
frame.setSize(440,350);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I've a Screen class that implements GestureListener, but it doesn't provide the PanStop method (which is mentioned in the LibGdx JavaDocs and Wiki)
Has this method been removed (and the docs out of date), or am I missing something?
If the former, then how does one detect and handle a touch-up or pan-stop?
Thanks.
EDIT: Implementation added....
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import java.util.Random;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.utils.Array;
import com.me.mygdxgame.actors.ActorJ;
public class JGameScreen implements Screen, GestureListener {
final MyGdxGame game;
private Stage stage;
private GameGrid gameGrid;
// Constructor & Init Screen
//
public JGameScreen(final MyGdxGame gam) {
game = gam;
stage = new Stage();
stage.setViewport(600, 600, true);
gameGrid = new GameGrid(stage, 10, 10);
}
// GestureListener Events
//
#Override
public void show() {
// TODO Auto-generated method stub
Gdx.input.setInputProcessor(new GestureDetector(this));
}
#Override
public boolean touchDown(float x, float y, int pointer, int button) {
// TODO Auto-generated method stub
//chain = gameGrid.getChain();
return false;
}
#Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
// TODO Auto-generated method stub
vector2 = stage.screenToStageCoordinates(vector2.set(x,y));
gameGrid.get(25).setPosition(vector2.x, vector2.y);
return true;
}
#Override
public boolean fling(float velocityX, float velocityY, int button) {
// TODO Auto-generated method stub
return false;
}
#Override
public void hide() {
// TODO Auto-generated method stub
Gdx.input.setInputProcessor(null);
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor( 0.9f, .04f, 0.2f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
stage.setViewport(game.WIDTH, game.HEIGHT, true);
stage.getCamera().translate(-50, -50, 0);
}
#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
}
#Override
public boolean tap(float x, float y, int count, int button) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean longPress(float x, float y) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean zoom(float initialDistance, float distance) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
Vector2 pointer1, Vector2 pointer2) {
// TODO Auto-generated method stub
return false;
}
}
I got the nightly build last week and I can see panStop in GestureListener interface. Odd issue.
I'm at a loss as to how to implement the LIBGDX controller support. So here are the details.
Let's say I'm following the tutorial here: http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-create-a-working-prototype-in-a-day-tutorial-part-1/
The Git for the tutorial at Part 4 (where i'm trying to implement a gamepad) is here: https://github.com/obviam/star-assault/tree/part4
I've made it to the final step and now it's running and I can interact with the character using the keyboard. I want to now make the character controllable via a controller either with the OUYA or through USB.
I have read the following: http://www.badlogicgames.com/wordpress/?p=2724
I have added the jar files to the main project and have Ordered and Exported.
I have now modified my GameScreen class to look as follows:
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.PovDirection;
import com.badlogic.gdx.controllers.mappings.Ouya;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.math.Vector3;
public class GameScreen implements Screen, InputProcessor, ControllerListener {
private World world;
private WorldRenderer renderer;
private CharacterController charactercontroller;
private int width, height;
#Override
public void show() {
world = new World();
renderer = new WorldRenderer(world, false);
charactercontroller = new CharacterController(world);
Gdx.input.setInputProcessor(this);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
charactercontroller.update(delta);
renderer.render();
}
#Override
public void resize(int width, int height) {
renderer.setSize(width, height);
this.width = width;
this.height = height;
}
#Override
public void hide() {
Gdx.input.setInputProcessor(null);
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
Gdx.input.setInputProcessor(null);
}
// * InputProcessor methods ***************************//
#Override
public boolean keyDown(int keycode) {
if (keycode == Keys.LEFT)
charactercontroller.leftPressed();
if (keycode == Keys.RIGHT)
charactercntroller.rightPressed();
if (keycode == Keys.UP)
charactercontroller.jumpPressed();
return true;
}
#Override
public boolean keyUp(int keycode) {
if (keycode == Keys.LEFT)
charactercontroller.leftReleased();
if (keycode == Keys.RIGHT)
charactercontroller.rightReleased();
if (keycode == Keys.UP)
charactercontroller.jumpReleased();
return true;
}
#Override
public boolean keyTyped(char character) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDown(int x, int y, int pointer, int button) {
if (!Gdx.app.getType().equals(ApplicationType.Android))
return false;
if (x < width / 2 && y > height / 2) {
charactercontroller.leftPressed();
}
if (x > width / 2 && y > height / 2) {
charactercontroller.rightPressed();
}
return true;
}
#Override
public boolean touchUp(int x, int y, int pointer, int button) {
if (!Gdx.app.getType().equals(ApplicationType.Android))
return false;
if (x < width / 2 && y > height / 2) {
charactercontroller.leftReleased();
}
if (x > width / 2 && y > height / 2) {
charactercontroller.rightReleased();
}
return true;
}
#Override
public boolean touchDragged(int x, int y, int pointer) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
#Override
public void connected(Controller controller) {
// TODO Auto-generated method stub
}
#Override
public void disconnected(Controller controller) {
// TODO Auto-generated method stub
}
#Override
public boolean buttonDown(Controller controller, int buttonCode) {
if (buttonCode == Ouya.BUTTON_O) {
charactercontroller.jumpPressed();
}
return false;
}
#Override
public boolean buttonUp(Controller controller, int buttonCode) {
if (buttonCode == Ouya.BUTTON_O) {
charactercontroller.jumpReleased();
}
return false;
}
#Override
public boolean axisMoved(Controller controller, int axisCode, float value) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean povMoved(Controller controller, int povCode,
PovDirection value) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean xSliderMoved(Controller controller, int sliderCode,
boolean value) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean ySliderMoved(Controller controller, int sliderCode,
boolean value) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean accelerometerMoved(Controller controller,
int accelerometerCode, Vector3 value) {
// TODO Auto-generated method stub
return false;
}
}
So based on my understand of how this works, right now when I load this on the OUYA the character should Jump when the "O" button is pressed. But nothing happens. I think I'm missing something. Any glaring mistakes in implementation?
Where I'm at with Java is I've been web developing for a number of years and have just recently returned to Java again to play around with games and interactive stuff on micro consoles. I figure starting off with a simple tutorial and experimenting with building on it would be a good idea, right?
Look forward to getting some awesome help!
Thanks,
Phil
Try adding:
Controllers.addListener(this);
right before:
Gdx.input.setInputProcessor(this);
setInputProcessor only registers listeners for the built-in input events (keys, touch events, etc). The controllers are an external library, and it needs to be initialized and loaded separately.
You will need to import com.badlogic.gdx.controllers.Controllers, too.
I'm currently developing a simple multiplayer game with Slick2D in Java. It's going to be a little more complex with the menu structure because I need several textfields to get the information which server address to connect, how many players are allowed to play, nickname and so on.
I reproduced my problem in 3 classes: The main class (ExampleGame) extends from StateBasedGame, Play and Menu from BasicGameState.
The classes Play and Menu have each one textfield and one button. My problem is I can only type in one of these classes into a textfield. The other textfield doesn't show any cursor responses when you click into the textfield.
This is the main class ExampleGame:
public class ExampleGame extends StateBasedGame {
public static final int MAIN_MENU_STATE = 0;
public static final int GAME_PLAY_STATE = 1;
public ExampleGame(String name) {
super(name);
this.addState(new Menu(MAIN_MENU_STATE));
this.addState(new Play(GAME_PLAY_STATE));
this.enterState(MAIN_MENU_STATE);
}
/**
* #param args
*/
public static void main(String[] args) {
AppGameContainer appgc;
try{
appgc = new AppGameContainer(new ExampleGame("ExampleGame"));
appgc.setDisplayMode(640, 360, false);
appgc.setTargetFrameRate(50);
appgc.start();
}catch(SlickException e){
e.printStackTrace();
}
}
#Override
public void initStatesList(GameContainer arg0) throws SlickException {
// TODO Auto-generated method stub
this.getState(GAME_PLAY_STATE);
this.getState(MAIN_MENU_STATE);
System.out.println(this.getStateCount() + ", " + this.getCurrentStateID());
this.enterState(MAIN_MENU_STATE);
}
}
ExampleGame automatically calls the MainMenu (Menu.java):
public class Menu extends BasicGameState {
private int stateID;
private Image background;
private UnicodeFont font;
private TextField textfield;
private Image createGameOption;
private int menuX = 50;
private int menuY = 250;
public Menu(int stateID) {
this.stateID = stateID;
}
#Override
public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
System.out.println("StateID: " + arg1.getState(this.getID()) + " , State: " + arg1.getCurrentState());
background = new Image("images/background_1024x640_v1.jpg");
createGameOption = new Image("images/btn_CreateGame_for_1024x640_v1.png");
textfield = new TextField(arg0, arg0.getDefaultFont(), 100, 100, 200, 30);
}
#Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2) throws SlickException {
// TODO Auto-generated method stub
background.draw(0, 0);
createGameOption.draw(menuX, menuY);
textfield.render(arg0, arg2);
}
#Override
public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
Input input = arg0.getInput();
int mouseX = input.getMouseX();
int mouseY = input.getMouseY();
// if mouse is over a button
if ((mouseX >= menuX && mouseX <= menuX + createGameOption.getWidth()) && (mouseY >= menuY && mouseY <= menuY + createGameOption.getHeight())) {
if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
// fx.play();
arg1.enterState(ExampleGame.GAME_PLAY_STATE);
}
}
}
#Override
public int getID() {
// TODO Auto-generated method stub
return stateID;
}
}
Here I can write in the textfield. So far so good. By clicking on the button I call the next page (Play.java):
public class Play extends BasicGameState {
private int stateID;
private Image background;
private UnicodeFont font;
private TextField textfield;
private Image createGameOption;
private int menuX = 350;
private int menuY = 250;
public Play (int stateID) {
this.stateID = stateID;
}
#Override
public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
System.out.println("StateID: " + arg1.getCurrentStateID() + " , State: " + arg1.getCurrentState());
arg1.inputStarted();
background = new Image("images/background_1024x640_v1.jpg");
createGameOption = new Image("images/btn_CreateGame_for_1024x640_v1.png");
textfield = new TextField(arg0, arg0.getDefaultFont(), 150, 100, 200, 30);
}
#Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2) throws SlickException {
// TODO Auto-generated method stub
background.draw(0,0);
createGameOption.draw(menuX, menuY);
textfield.render(arg0, arg2);
}
#Override
public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
Input input = arg0.getInput();
int mouseX = input.getMouseX();
int mouseY = input.getMouseY();
if ((mouseX >= menuX && mouseX <= menuX + createGameOption.getWidth())
&& (mouseY >= menuY && mouseY <= menuY + createGameOption.getHeight())) {
if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
arg1.enterState(ExampleGame.MAIN_MENU_STATE);
}
}
}
#Override
public int getID() {
// TODO Auto-generated method stub
return stateID;
}
}
The class Play has almost the same code as Menu but it seems that the textfield in Play doesn't response to the mouse and key. It seems to be inactive because you can't see any changes. But that's actually not true. If I click in the textfield, type something and go back to the Menu by clicking the button, you can see the text you typed in. It seems that both textfields (the one from Menu and from Play) are connected to somehow. But I have no idea where the problem is from.
Has anyone any suggestions how I can fix that?
Thanks in advance!
Slick TextField not working
This is the exact answer you're looking for that I answered not too long ago. This user had the same problem as you, and resolved it by following my wiki link by example of how to use StateBasedGame's
I'm writing an libgdx game, but it seems that i cannot show my splashscreen, it only shows a black screen. does anyone know how this comes?
I don't get any runtime error, it only shows a blackscreen, even if i set the glClearColor to an other color then black
package mygame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class SplashScreen implements Screen {
Texture splashTexture;
Sprite splashSprite;
SpriteBatch batch;
MyGame game;
public SplashScreen(MyGame game) {
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
splashTexture = new Texture("mygame/splash.png");
splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splashSprite = new Sprite(splashTexture);
//splashSprite.setColor(1, 1, 1, 0);
splashSprite.setX(Gdx.graphics.getWidth() / 2 - (splashSprite.getWidth() / 2));
splashSprite.setY(Gdx.graphics.getHeight() / 2 - (splashSprite.getHeight() / 2));
batch = new SpriteBatch();
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
I tried your code (see below) and it works well enough, so perhaps the problem lies in the MyGame class. Are you, for example, overriding Game.render() then not calling super.render()?
package hacks;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyGame extends com.badlogic.gdx.Game {
#Override
public void create() {
setScreen(new SplashScreen(this));
}
public static void main(String[] args) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = MyGame.class.getName();
config.width = 800;
config.height = 480;
config.fullscreen = false;
config.useGL20 = true;
config.useCPUSynch = true;
config.forceExit = true;
config.vSyncEnabled = true;
new LwjglApplication(new MyGame(), config);
}
}
class SplashScreen implements Screen {
Texture splashTexture;
Sprite splashSprite;
SpriteBatch batch;
MyGame game;
public SplashScreen(MyGame game) {
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
splashTexture = new Texture("assets/data/libgdx.png");
splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splashSprite = new Sprite(splashTexture);
// splashSprite.setColor(1, 1, 1, 0);
splashSprite.setX(Gdx.graphics.getWidth() / 2 - (splashSprite.getWidth() / 2));
splashSprite.setY(Gdx.graphics.getHeight() / 2 - (splashSprite.getHeight() / 2));
batch = new SpriteBatch();
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
this is how my game class looks like:
package mygame;
import com.badlogic.gdx.Game;
public class MyGame extends Game {
#Override
public void create() {
setScreen(new SplashScreen(this));
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void render() {
// 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
}
}
This is how your game class should look like...you need to call the super functions .
package com.sample;
import com.badlogic.gdx.Game;
public class Sample extends Game{
#Override
public void dispose() {
// TODO Auto-generated method stub
super.dispose();
}
#Override
public void pause() {
// TODO Auto-generated method stub
super.pause();
}
#Override
public void resume() {
// TODO Auto-generated method stub
super.resume();
}
#Override
public void render() {
// TODO Auto-generated method stub
super.render();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
super.resize(width, height);
}
#Override
public void create() {
// TODO Auto-generated method stub
setScreen(new AnotherScreen(this));
}
}