Cannot resolve setScreen() method - libgdx

I'm not new to libgdx, but when i ended up with my previous university project, and started new one, Android studio or IDEA cannot resolve setScreen method, other stuff works fine. Any ideas ? Hope for help. (project absolutely clear).

Creating a project in LibGdx gives you your core file which implements the ApplicationListener.
What I gather you are referring to is extending the Game class with with you set Screen classes with.
With the ApplicationListener.
public class HelloWorld implements ApplicationListener {
private SpriteBatch batch;
private BitmapFont font;
#Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont();
font.setColor(Color.RED);
}
#Override
public void dispose() {
batch.dispose();
font.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
font.draw(batch, "Hello World", 200, 200);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
What you're after: (taken from https://github.com/libgdx/libgdx/wiki/Extending-the-simple-game)
public class Drop extends Game {
public SpriteBatch batch;
public BitmapFont font;
public void create() {
batch = new SpriteBatch();
//Use LibGDX's default Arial font.
font = new BitmapFont();
this.setScreen(new MainMenuScreen(this));
}
public void render() {
super.render(); //important!
}
public void dispose() {
batch.dispose();
font.dispose();
}
}
Which allows you to change screens whenever you need:
public class MainMenuScreen implements Screen {
final Drop game;
OrthographicCamera camera;
public MainMenuScreen(final Drop game) {
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
}
//...Rest of class omitted for succinctness.
}

For default, gdx main Class will extends ApplicationAdapter, you need extends Game class for get use of setScreen()

Related

How can I show texts like score or time in libgdx?

I have used the stage method. But it doesn't work. I am new in game developing. Help me out please.
You can use Label, If you're using scene2d in this way :
public class GdxText extends ApplicationAdapter {
Stage stage;
Label scoreLabel;
#Override
public void create() {
stage=new Stage();
Label.LabelStyle labelStyle=new Label.LabelStyle(new BitmapFont(), Color.RED);
scoreLabel=new Label(String.format("%03d",0),labelStyle);
Table table =new Table();
table.defaults().pad(2);
table.add(new Label("SCORE :",labelStyle));
table.add(scoreLabel);
table.setPosition(200,300);
stage.addActor(table);
}
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act();
}
public void increase(){
CharSequence value=scoreLabel.getText();
int v= Integer.valueOf(value.toString());
scoreLabel.setText(String.format("%03d", ++v));
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width,height,true);
}
#Override
public void dispose() {
stage.dispose();
}
}
I am using default font for this test, you can use own font. Whenever you want to increase value of score call increase() method, that increase your score by one.
Output :

Libgdx Actor Scened2d Draw method does not draw nothing

I've started a very small Libgdx project based in Scene2d. I downloaed last version of LGDX. I have tried to find some imformation but nothing clear.
The problem is that I only get a Black Screen of the Death. Nothing happens
I have followed the code, with logs and I'm sure I arrive to Draw method in my Actor with no results:
Thank you in advance.
public class ActorBall extends Actor implements Disposable {
private Texture ballTexture;
private TextureRegion ballTextureRegion;
public ActorBall() {
bolaTexture = new Texture("redball.png");
ballTextureRegion = new TextureRegion(ballTexture, 300,300);
setSize(300,300);
}
#Override
public void dispose(){
bolaTexture.dispose();
}
#Override
public void draw(Batch batch, float parentAlpha) {
Color col = getColor();
batch.setColor(col.r,col.g, col.b,col.a * parentAlpha);
Gdx.app.log("App","where are you");
batch.draw(ballTextureRegion,getX(),getY(),getOriginX(),
getOriginY(),getWidth(), getHeight(), getScaleX(), getScaleY(),getRotation());
}
#Override
public void act(float delta) {
super.act(delta);
}
}
My screen class extended from Screen:
public class scene extends scenebase {
private final OrthographicCamera camera;
private MyGdxGame game ;
private Stage stage; // los Stages son inputprocessors
private ActorBall ball;
public scene(MyGdxGame game) {
super(game);
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
stage = new Stage(new ScreenViewport());
ball = new ActorBall();
ball.setPosition(0,0);
stage.addActor(ball);
}
#Override
public void render(float delta) {
// super.render(delta);
Gdx.gl.glClearColor(0,0,0.0f,1);
Gdx.gl.glClear (GL20.GL_COLOR_BUFFER_BIT);
camera.update();
// game.batch.setProjectionMatrix(camera.combined);
stage.draw();
stage.act();
}
#Override
public void resize(int width, int height) {
stage.getViewport().update (width,height);
}
}
And last my main game:
public class MyGdxGame extends Game {
private AssetManager manager;
private scene screenscene;
#Override
public void create() {
manager = new AssetManager();
manager.load("redball.png", Texture.class);
manager.finishLoading();
// Enter the loading screen to load the assets.
screenscene = new scene(this);
setScreen(screenscene);
}
#Override
public void render () {
super.render(); // This is very important!!!!!!!!
Gdx.gl.glClearColor(0, 0, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
public AssetManager getManager() {
return this.manager;
}
}
#Override
public void render () {
super.render(); // This is very important!!!!!!!!
Gdx.gl.glClearColor(0, 0, 0.1f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
What this does is first render everything and then clear the screen. You need to first clear it and then render.

Could not load tiled map in LIBGDX

I have been following this tutorial LIBGDX . I followed the similar steps as per the tutorial but the map is not loaded in the background as per the tutorial. I checked the code but I couldn't find the reason .
Here is my code,
public class PlayScreen implements Screen {
private MyJungleGame game;
Texture texture;
private OrthographicCamera gamecam;
private FitViewport gamePort;
private HudClass hud;
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
public PlayScreen(MyJungleGame game) {
this.game = game;
gamecam = new OrthographicCamera();
gamePort = new FitViewport(MyJungleGame.V_WIDTH, MyJungleGame.V_HEIGHT);
hud = new HudClass(game.batch);
maploader = new TmxMapLoader();
map = maploader.load("tiledmap.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
gamecam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
}
public void update(float dt) {
handleInput(dt);
gamecam.update();
renderer.setView(gamecam); // it wil draw wat the game cam can see
}
public void handleInput(float dt) { //(----4)
if (Gdx.input.isTouched()) {
gamecam.position.x += 100 * dt;
}
}
#Override
public void show() {
}
#Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
#Override
public void resize(int width, int height) {
gamePort.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
This is my main game code . I have attached the screenshot of the output .
As you can see it is filled with black background but I need the tiled map to get loaded. I am completely new to this. Please help
I just found it. The problem is in the gameport. I didn't pass the value of camera to the gameport. I just changed the code from
gamePort = new FitViewport(MyJungleGame.V_WIDTH, MyJungleGame.V_HEIGHT);
to something like,
gamePort = new FitViewport(MyJungleGame.V_WIDTH, MyJungleGame.V_HEIGHT,gamecam);

New to libgdx; glitchy graphic when drawing scene with scene2D

I'm trying to display a simple text for a menu UI using scene2D, but for some reason nothing is being displayed here. The screen displays pure black.
public class ScreenMenu implements Screen {
MyGame myGame;
SpriteBatch batch;
Stage stage;
Label labelNewGame, labelContinue, labelCredits;
public ScreenMenu(MyGame myGame) {
this.myGame = myGame;
}
#Override
public void show() {
init();
BitmapFont font = initFont();
initLabels(font);
initStage();
}
private void init() {
batch = new SpriteBatch();
}
private BitmapFont initFont() {
return new FontLoader().getMichroma();
}
private void initLabels(BitmapFont font) {
Label.LabelStyle labelStyle = new Label.LabelStyle(font, Color.WHITE);
labelNewGame = new Label("New Game", labelStyle);
labelContinue = new Label("Continue", labelStyle);
labelCredits = new Label("Credits", labelStyle);
}
private void initStage() {
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);
stage.addActor(labelNewGame);
}
#Override
public void render(float delta) {
GlHelper.clearScreen();
stage.act(delta);
stage.draw();
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
myGame.dispose();
batch.dispose();
stage.dispose();
}
}
The following class contains the clearScreen function. If I don't run it, the entire screen becomes super glitchy, but I can see the New Game text.
public class GlHelper {
public static void clearScreen() {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
public static void clearScreen(float red, float green, float blue, float alpha) {
Gdx.gl.glClearColor(red, green, blue, alpha);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
}
Do you use?
setBounds(x, y, w, h);
before to add stage for example:
labelNewGame.setBounds(0, 0, 100, 100);
maybe he can help you.
I feel stupid now. In the FontLoader implementation I set the FreeTypeFontParameter color to Color.BLACK. Apparently it overrides the Color.WHITE argument from LabelStyle.

Setting new Screen in LibGDX

I have two clases, one for main methods and the other for splash. However, switching between these two doesn't work. I wanted to draw an image in the Splash class, but it turned black instantly. And when I moved the code from the splash to main class, the image appeared.
Main class:
public class Main extends Game {
public static int WIDTH, HEIGHT;
public void create () {
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
setScreen(new Splash());
}
public void render () { }
public void dispose() { super.dispose(); }
public void pause() { super.pause(); }
public void resize(int width, int height) { super.resize(width, height); }
public void resume() { }
}
Splash class:
public class Splash implements Screen {
private Sprite splash;
private SpriteBatch sb;
public void show() {
sb = new SpriteBatch();
Texture splashTexture = new Texture(Gdx.files.internal("res/img/splash.png"));
splash = new Sprite(splashTexture);
splash.setSize(Main.WIDTH, Main.HEIGHT);
Gdx.gl.glClearColor(0, 0, 0, 1);
}
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
sb.begin();
splash.draw(sb);
sb.end();
}
public void resize(int width, int height) { }
public void resume() { }
public void dispose() { }
public void hide() { }
public void pause() { }
}
Any idea, what could cause the problem of not rendering the image in Splash class?
UPDATE 1: I have discovered, that the render() method inside the Splash class doesn't even get called (but the show() method does)
You should call the render method from your game class. If you dont call it noone will do that for you. You shouldn't override the render method of the game class which you do and you do it empty so it wont call anything for you.
Lets take a look into the Game class. It does implement all methods from the ApplicationListener interface (well not the create()). So there is no need for you to override anything of it. Simply delete your:
public void render () { }
public void dispose() { super.dispose(); }
public void pause() { super.pause(); }
public void resize(int width, int height) { super.resize(width, height); }
public void resume() { }
and it should work fine. Even though these are useless methods. They do nothing than calling the super class so why you write those. If you have not written that it automatically calls the super methods.
But okay if you want to handle the stuff yourself like calling dispose on a screen or calling the init before you set a new screen you need to write your own "game" class which implements the ApplicationListener interface.
To give you an idea on how to do this ill post a small example which i use for some tests:
public class MyGameClass implements ApplicationListener {
private Screen curScreen; // the current screen
#Override
public void create(){
Gdx.graphics.getWidth();
Gdx.graphics.getHeight();
Intro temp = new Intro(this);//just an example of the first screen to load up
setScreen(temp);
}
public void setScreen(Screen s) {
if (curScreen != null) {
curScreen.hide();
curScreen.dispose();
}
curScreen = s;
curScreen.show();
curScreen.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
s.init();
}
#Override
public void dispose() {
curScreen.dispose();
}
#Override
public void render() {
curScreen.render(Gdx.graphics.getDeltaTime()); //call the rendermethod with the delta time as parameter
}
#Override
public void resize(int width, int height) {
curScreen.resize(width, height);
}
#Override
public void pause() {
curScreen.pause();
}
#Override
public void resume() {
curScreen.resume();
}
}