Libgdx MainMenuScreen

I am trying to learn how to use the libgdx framework. As in the tutorial ( https://code.google.com/p/libgdx/wiki/ExtendedSimpleApp ) on the libgdx description site, I tried to set up a small main menu. I have imported all the classes that are necessary, but on line 29 there is always an error: "MainMenuScreen cannot be resolved to the type". Here is the source code:

package com.me.mygdxgame; import java.awt.SplashScreen; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class MyGdxGame implements ApplicationListener { private OrthographicCamera camera; private SpriteBatch batch; private Texture texture; private Sprite sprite; private BitmapFont font; @Override public void create() { batch = new SpriteBatch(); //Use LibGDX default Arial font. font = new BitmapFont(); this.setScreen(new MainMenuScreen(this)); } @Override public void dispose() { batch.dispose(); texture.dispose(); } @Override public void render() { Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); batch.setProjectionMatrix(camera.combined); batch.begin(); sprite.draw(batch); batch.end(); } @Override public void resize(int width, int height) { } @Override public void pause() { } @Override public void resume() { } 

}

+4
source share
6 answers

MainMenuScreen is not a standard class, if you read further in the tutorial that you linked, you see that you created an additional class "MainMenuScreen", just create another class called "MainMenuScreen" and add the code from the tutorial.

If this is your first time with java, you might want to google some java tutorials to learn the java and oop funds first before using some external libraries;)

+9
source

you need to create a class that extends the libgdx game class.

 public class MainClass extends Game 

then in your method of creating the main class you must set the screen.

 setScreen(new MainMenuScreen(this)); 

and in your mainmenuclass you need to implement the libgdx screen class.

 public class MainMenuScreen implements Screen 

and you must create such a constructor

 public MainMenuScreen(Game game) { // TODO Auto-generated constructor stub this.game = game; camera = new OrthographicCamera(); camera.setToOrtho(false, scrw, scrh);//scrw is your screen width,scrh is screen height camera.update(); batch = new SpriteBatch(); Gdx.input.setInputProcessor(MainMenuScreen.this);} 

I think this will work ...

+2
source

To start learning libGDX, complete this project: https://github.com/edesdan/libgdx-playground . Maybe this will help you take the first steps in this world.

+1
source

Your basic concept is not clear. To use different screens in one application, you must extend the Game class.

 public class MyGdxGame extends Game implements ApplicationListener{} 

To select a different screen, call:

 setScreen(new XYZScreen(this)); 

this refer to the MyGdxGame object.

Now you need to create a new class called XYZ. Then do whatever you want on the new screen.

+1
source

if you want to use the Screen class, you must extend the main class from the Game class if you use a screen, you cannot use the applicationlistener no more, because on the screen you have all the functions of the application listener redefined ...

all you have to do is move your code to the listener application on "MainMenuScreen"

then you set the screen when creating the Mainclass class to "MainMenuScreen"

Public class MainClass extends game {

 @Override public void create() { setScreen(new MainMenuScreen()); }} 
0
source

I really used screens in a small project that I did, here is the code for one of my screens.

 package com.sample.Main; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class Mainmenu implements Screen { MainGame game; SpriteBatch batch; Mainmenu(MainGame g){ game = g; batch = g.batch; } @Override public void show() { } @Override public void render(float delta) { } @Override public void resize(int width, int height) { } @Override public void pause() { } @Override public void resume() { } @Override public void hide() { } @Override public void dispose() { } } 

And so for the main game code that launches the screen:

 package com.sample.Main; import com.badlogic.gdx.Game; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class MainGame extends Game { SpriteBatch batch; Screen mainscreen; @Override public void create () { batch = new SpriteBatch(); mainscreen = new Mainmenu(this); setScreen(mainscreen); } @Override public void render () { super.render(); } @Override public void dispose () { batch.dispose(); } } 
0
source

Source: https://habr.com/ru/post/1500374/


All Articles