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));
}
}
Related
So I'm trying to draw a circle where a user clicks, which can then be re-sized by the bar down below. Everything works except for that the circle won't draw where I want it to. Any suggestions?
Here is my Panel
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.event.*;
public class TestClass extends JFrame {
private JSlider slide;
private MainClass myPanel;
public int x1=0;
public int y1=0;
public TestClass(){
super("The Title");
myPanel = new MainClass();
myPanel.setBackground(Color.YELLOW);
slide = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
slide.setMajorTickSpacing(10);
slide.setPaintTicks(true);
slide.addChangeListener(
new ChangeListener(){
public void stateChanged(ChangeEvent e){
myPanel.checkDiameter(slide.getValue());
}
}
);
HandlerClass handler = new HandlerClass();
slide.addMouseListener(handler);
add(slide, BorderLayout.SOUTH);
add(myPanel, BorderLayout.CENTER);
}
public int setX1(){
return x1;
}
public int setY1(){
return y1;
}
private class HandlerClass implements MouseListener{
#Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
x1=event.getX();
y1=event.getY();
repaint();
}
#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
}
}
}
Here is the other important class which creates a window and call TestClass;
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MainClass extends JPanel{
private int d = 10;
public void paintComponent(Graphics g){
super.paintComponent(g);
TestClass values = new TestClass();
g.setColor(Color.CYAN);
g.fillOval(values.setX1()+50, values.setY1(), d, d);
}
public void checkDiameter(int newD)
{
//New format for if statements
d = (newD >= 0 ? newD //if
: 10//else
);
repaint();
}
public Dimension getPreferredSize(){
return new Dimension(200,200);
}
public Dimension getMinimumSize(){
return getPreferredSize();
}
}
Hey Guys I found a way to get it working.
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.event.*;
public class TestClass extends JFrame {
private JSlider slide;
private MainClass myPanel;
public static int x1=0,y1=0;
public TestClass(){
super("The Title");
myPanel = new Panel();
myPanel.setBackground(Color.YELLOW);
//Allows you to re-size the drawn circle
slide = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
slide.setMajorTickSpacing(10);
slide.setPaintTicks(true);
slide.addChangeListener(
new ChangeListener(){
public void stateChanged(ChangeEvent e){
myPanel.checkDiameter(slide.getValue());
}
}
);
//Create a way to handle user mouse events
HandlerClass handler = new HandlerClass();
myPanel.addMouseListener(handler);
add(slide, BorderLayout.SOUTH);
add(myPanel, BorderLayout.CENTER);
}
private class HandlerClass implements MouseListener{
#Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
Right here is what is new with the code, this method gets the x and y coordinates of a click and sends it to MainClass object myPanel which has the setPosition method; class show before.
myPanel.setPosition(event.getX(),event.getY());
repaint(); }
#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
}
}
}
Here is the MainClass class, it is poorley named as it's not actually the main class...
import java.awt.;
import javax.swing.;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Panel extends JPanel{
private int x1,y1;
private int d = 10;
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.CYAN);
g.fillOval(x1, y1, d, d);
}
public void checkDiameter(int newD)
{
//New format for if statements
d = (newD >= 0 ? newD //if
: 10//else
);
repaint();
}
public void setPosition(int newX, int newY) {
this.x1 = newX;
this.y1 = newY;
repaint();
}
public Dimension getPreferredSize(){
return new Dimension(200,200);
}
public Dimension getMinimumSize(){
return getPreferredSize();
}
}
I am prety new on LibGDX,and trying to create a circle which shows the remaning time of the game.For that purpose I find this called RadialSprite,but dont know how to apply it.
what I have treid so far like this.
public class GameAreaScreen implements Screen{
Skin skin;
Stage stage;
Table table= new Table();
int a,b,c,result;
float bosluk=2;
private TextField txtFieldOperator;
private TextField txtFieldC;
private TextField txtFieldA;
private TextField txtFieldB;
private TextField txtFieldEqual;
private float time=10;
private RadialSprite rd;
private MyGdxGame game;
public GameAreaScreen(final MyGdxGame gam){
this.game=gam;
create();
}
public void create(){
Texture txturecircle = new Texture(Gdx.files.internal("circle.png"));;
TextureRegion regions= new TextureRegion(txturecircle);
rd=new RadialSprite(regions);
stage = new Stage();
Gdx.input.setInputProcessor(stage);
Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
txtFieldA= new TextField("a Vallue", skin);
txtFieldB= new TextField("b Vallue", skin);
txtFieldC= new TextField("c Vallue", skin);
txtFieldOperator= new TextField("op", skin);
txtFieldEqual= new TextField("=", skin);
table.add(txtFieldA).pad(bosluk).width(30);
table.add(txtFieldOperator).pad(bosluk).width(30);
table.add(txtFieldB).width(30);
table.add(txtFieldEqual).pad(bosluk).width(30);
table.add(txtFieldC).pad(bosluk).width(30);
table.setFillParent(true);
// stage.addActor(table);
}
#Override
public void show() {
// TODO Auto-generated method stub
}
#Override
public void render(float delta) {
// TODO Auto-generated method stub
time -= delta;
rd.setAngle(time/10f * 360f);
Log.d("TIME",String.valueOf(time));
Log.d("angel",String.valueOf(rd.getAngle()));
Gdx.gl.glClearColor(0, 0, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
game.batch.begin();
rd.draw(game.batch, 100,100,36);
game.batch.end();
//stage.draw();
//stage.setDebugAll(true);
}
#Override
public void resize(int width, int height) {
// 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 hide() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
MyGdxGame class
public class MyGdxGame extends Game{
SpriteBatch batch;
BitmapFont font;
public void create() {
batch = new SpriteBatch();
// Use LibGDX's default Arial font.
font = new BitmapFont();
this.setScreen(new MenuScreen(this));
}
public void render() {
super.render(); // important!
}
public void dispose() {
batch.dispose();
font.dispose();
}
}
**Iam trying to do something like this.
any help or direction wellcome
i'm new to libgdx and i can't figure out what i do wrong.
In my game I just want to switch between 2 screens (first is menuScreen, second is gameScreen). I have created these two screens and one game class, it all looks okay. But when i call my method setScreen(new GameScreen()) nothing happens. Also i should say, that i used screen and game classes in my previous project and all was ok, when i compare my current code to previous one i do not see any differences, so it is very curiously.
Here is my MenuScreen class:
MenuScreen implements Screen {
private SpriteBatch batch ;
private Texture texture;
private float timePassed;
public MenuScreen() {
timePassed = 0;
batch = new SpriteBatch();
texture = new Texture("texture.png");
#Override
public void render (float delta) {
timePassed += Gdx.graphics.getDeltaTime();
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (timePassed > 5) {GameClass.getInstance().setScreen(new GameScreen());} //it looks strange, but it's to check if all works properly
batch.begin();
batch.draw(texture, 0, 0);
batch.end();
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
}
#Override
public void hide() {
}
#Override
public void resume() {
}
#Override
public void pause() {
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}}
Here is my GameScreen class:
GameScreen implements Screen {
private SpriteBatch batch2 ;
private Texture texture2;
public MenuScreen() {
batch2 = new SpriteBatch();
texture2 = new Texture("texture2.png");
}
#Override
public void render (float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch2.begin();
batch2.draw(texture2, 0, 0);
batch2.end();
}
#Override
public void dispose() {
batch2.dispose();
texture2.dispose();
}
#Override
public void hide() {
}
#Override
public void resume() {
}
#Override
public void pause() {
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}}
And Game class:
public class ClassGame extends Game {
public static ClassGame getInstance(){
ClassGame instance = new ClassGame();
return instance;
}
#Override
public Screen getScreen() {
return super.getScreen();
}
#Override
public void setScreen(Screen screen) {
super.setScreen(screen);
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void render() {
super.render();
}
#Override
public void resume() {
super.resume();
}
#Override
public void pause() {
super.pause();
}
#Override
public void dispose() {
super.dispose();
}
public ClassGame() {
super();
}
#Override
public void create() {
MenuScreen menuScreen = new MenuScreen();
setScreen(menuScreen);
}}
This is how I did for the same problem, i had three classes for two screen to switch between them. Here I am switching between second and third using a image actor as button.I also used two constructors in each class, one of them constructors with no arguments, below is my code hope you will get it.
My main class code:
public class First extends Game{
private Game game;
public Main(){ game=this; }
public void create() {
game.setScreen(new Second(game)); }}
Below is my first screen class code
public class Second implements Screen{
private Game second;
public Second(Game second){this.second=second;}
public Second(){}
// your code
Stage myStage=new Stage(new ScreenViewport());
Group myGroup=new Group();
Image secondButton=new Image(new Texture(Gdx.files.internal("image.png")));
public void show(){
myGroup.addActor(secondButton);
secondButton.addListener(new InputListener(){
second.setScreen(new Third(second)); });}
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
myStage.act(Gdx.graphics.getDeltaTime());
myStage.draw(); }
public void resize(int width, int height) {}
public void pause(){}
public void resume(){}
public void hide(){}
public void dispose(){}}}
And my next screen class code
public class Third implements Screen{
private Game third;
public Third(Game third){
this.third=third;}
public Third(){}
// your code
Stage myStage=new Stage(new ScreenViewport());
Group myGroup=new Group();
Image thirdButton=new Image(new Texture(Gdx.files.internal("image.png")));
public void show() {
myGroup.addActor(thirdButton);
thirdButton.addListener(new InputListener(){
third.setScreen(new Third(third));});}
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
myStage.act(Gdx.graphics.getDeltaTime());
myStage.draw();}
public void resize(int width, int height) { }
public void pause(){}
public void resume(){}
public void hide(){}
public void dispose(){}}}
I got it, guys. In my previous project i declared instance field outside of getInstance(). Like this :
public static GameClass instance = new GameClass();
public static GameClass getInstance(){
return instance;
}
But i still need your help. I don't know how this small thing wasn't letting me switch screens.
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 trying to display 2 different screens, changing when the user touches the screen. So far with the code below the screens change but the text just keeps overlapping and piling up. I need to dispose of EVERYTHING on the screen before switching.
One of the 2 similar pages(only the text is different on the 2)
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
public class MainMenu implements Screen {
OrthographicCamera camera;
SpriteBatch batch;
Screens game;
BitmapFont font;
public MainMenu(Screens game) {
this.game = game;
}
#Override
public void dispose() {
batch.dispose();
font.dispose();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void render(float delta) {
CharSequence str = "Main Menu";
batch = new SpriteBatch();
font = new BitmapFont();
batch.begin();
font.draw(batch, str, 200, 200);
batch.end();
if (Gdx.input.justTouched()) // use your own criterion here
game.setScreen(game.anotherScreen);
}
#Override
public void show() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
}
Screens.java
package com.me.mygdxgame;
import com.badlogic.gdx.Game;
public class Screens extends Game {
MainMenu mainMenuScreen;
AnotherScreen anotherScreen;
#Override
public void create() {
mainMenuScreen = new MainMenu(this);
anotherScreen = new AnotherScreen(this);
setScreen(mainMenuScreen);
}
}
Change your render function to:
#Override
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //clears the buffer
CharSequence str = "Main Menu";
batch = new SpriteBatch();
font = new BitmapFont();
batch.begin();
font.draw(batch, str, 200, 200);
batch.end();
if (Gdx.input.justTouched()) // use your own criterion here
game.setScreen(game.anotherScreen);
}