I am LibGdx Newbie, and trying to make my iceCream image touchable.
I like to know how to set the input-process(by touch on screen).
Do I need to make another class? When I try to implements the input-process to
my Prac1 class, JAVA doesn't allow me to implements without changing the class abstract. To be specific, I like to make it whenever the user touch the image
,it counts the number of touch. Here is my code and Thank you for help.
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Prac1 extends ApplicationAdapter {
int w,h,tw,th =0;
OrthographicCamera camera;
SpriteBatch batch;
Texture img;
#Override
public void create () {
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(w, h);
camera.position.set(w/2, h/2, 0);
camera.update();
batch = new SpriteBatch();
img = new Texture(Gdx.files.internal("iceCream.png"));
tw = img.getWidth();
th = img.getHeight();
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, camera.position.x - (tw/2), camera.position.y - (th/2));
batch.end();
}
}
You can use InputProcessor to handle user input.
Like this:-
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Prac1 extends ApplicationAdapter {
float w,h,tw,th =0;
OrthographicCamera camera;
SpriteBatch batch;
Sprite img;
#Override
public void create () {
w = Gdx.graphics.getWidth();
h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(w, h);
camera.position.set(w/2, h/2, 0);
camera.update();
batch = new SpriteBatch();
img = new Sprite(new Texture(Gdx.files.internal("iceCream.png")));
tw = img.getWidth();
th = img.getHeight();
img.setBounds( camera.position.x - (tw/2), camera.position.y - (th/2),tw,th);
Gdx.input.setInputProcessor(new InputAdapter(){
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(img.getBoundingRectangle().contains(screenX, screenY))
System.out.println("Image Clicked");
return true;
}
});
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
img.draw(batch);
batch.end();
}
}
replace this code with your code you can easily understand what is happening here.
your can also implement GestureListener to handle gesture events.
Since you need to get touch events from image, you can do that with Stage and Actors. You'll need to create a Stage and Image with your texture, then add Touchable attributes:
iceCreamImg.setTouchable(Touchable.enabled);
iceCreamImg.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.debug(TAG, "touchDown()");
// must return true for touchUp event to occur
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.debug(TAG, "touchUp()");
}
and add Image to the stage. In render method you should add:
stage.act();
stage.draw();
and also set input processor for your stage with
Gdx.input.setInputProcessor(stage);
If you want to use both ApplicationAdapter and InputProcessor in you Class, you have to use the interfaces rather than the abstract: Change you class signature to Prac1 implements ApplicationListener, InputProcessor
Check here for a complete tutorial:
http://www.gamefromscratch.com/post/2013/10/24/LibGDX-Tutorial-5-Handling-Input-Touch-and-gestures.aspx
Related
Currently, I am able to move the box to the left and right through the display boundary, but I am unable to figure out how to move the box up and down. I would like the box to navigate in a circle around the display, e.g move right, then down, then to the left, then up, then continuously moving throughout the display. Here is my code for my entity class:
import java.awt.Rectangle;
import org.lwjgl.opengl.GL11;
class Entity
{
private static enum State { START, LEFT, RIGHT, UP, DOWN};
private Rectangle box;
private State state;
private float speed; // pixels / ms
public Entity(float speed)
{
box = new Rectangle(10, 10, 10, 10);
state = State.START;
this.speed = speed;
}
public void draw()
{
float x = (float)box.getX();
float y = (float)box.getY();
float w = (float)box.getWidth();
float h = (float)box.getWidth();
// draw the square
GL11.glColor3f(0,1,0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x, y);
GL11.glVertex2f(x+w, y);
GL11.glVertex2f(x+w, y+w);
GL11.glVertex2f(x, y+w);
GL11.glEnd();
}
public void update(int delta)
{
switch (state)
{
case START:
state = State.RIGHT;
case RIGHT:
box.translate((int)(speed*delta), 0);
if (box.getX() >= 800)
{
state = State.LEFT;
}
break;
case LEFT:
box.translate((int)(-speed*delta), 0);
if (box.getX() <= 0)
{
state = State.RIGHT;
}
break;
}
}
}
Here is my code for GameLoop:
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.input.Keyboard;
import org.lwjgl.LWJGLException;
public class GameLoop
{
public static final int TARGET_FPS=100;
public static final int SCR_WIDTH=800;
public static final int SCR_HEIGHT=600;
public static void main(String[] args) throws LWJGLException
{
initGL(SCR_WIDTH, SCR_HEIGHT);
Entity e = new Entity(.1f);
long time = (Sys.getTime()*4000)/Sys.getTimerResolution(); // ms
while (! Display.isCloseRequested())
{
long time2 = (Sys.getTime()*4000)/
Sys.getTimerResolution(); // ms
int delta = (int)(time2-time);
System.out.println(delta);
e.update(delta);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
e.draw();
// UPDATE DISPLAY
Display.update();
Display.sync(TARGET_FPS);
time = time2;
}
Display.destroy();
}
public static void initGL(int width, int height) throws LWJGLException
{
// open window of appropriate size
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
Display.setVSyncEnabled(true);
// enable 2D textures
GL11.glEnable(GL11.GL_TEXTURE_2D);
// set "clear" color to black
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// enable alpha blending
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
// set viewport to entire window
GL11.glViewport(0,0,width,height);
// set up orthographic projectionr
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0, 1, -1);
// GLU.gluPerspective(90f, 1.333f, 2f, -2f);
// GL11.glTranslated(0, 0, -500);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
}
Any help would be appreciated. I also need to have the box change colors every time it changes direction.
Correct me if i'm wrong but i think you just need to use box.translate(0,value) to move your Rectangle up or down. Just adapt your left-right movement to the y value of the box. You could also use the State to determine which color it should be drawn in.
For the game I'm making I use Tiled to make and edit maps. However, these maps don't load in the sample I've made so far:
package com.bluezamx.magillion.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.bluezamx.magillion.Magillion;
import com.bluezamx.magillion.utils.Constants;
public class WorldScreen implements Screen {
private Magillion game;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Stage stage;
//Tiled variables
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer maprenderer;
// Box2D variables
private World world;
private Box2DDebugRenderer debug;
public WorldScreen (Magillion game) {
this.game = game;
gameCam = new OrthographicCamera();
mapLoader = new TmxMapLoader();
map = mapLoader.load("TestWorld.tmx");
maprenderer = new OrthogonalTiledMapRenderer(map, 1 / Constants.PPM);
gamePort = new FillViewport(Constants.WIDTH / Constants.PPM, Constants.HEIGHT / Constants.PPM, gameCam);
gameCam.position.set((gamePort.getWorldWidth() / 2), (gamePort.getWorldHeight() / 2), 0);
world = new World(new Vector2(0,0), true);
debug = new Box2DDebugRenderer();
debug.SHAPE_STATIC.set(1, 0, 0, 1);
for(MapObject object : map.getLayers().get(1).getObjects().getByType(RectangleMapObject.class)) {
Rectangle rect = ((RectangleMapObject) object).getRectangle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
body = world.createBody(bdef);
shape.setAsBox(rect.getWidth() / 2, rect.getHeight() / 2);
fdef.shape = shape;
body.createFixture(fdef);
}
}
#Override
public void show() {}
private void update(float dt) {
handleInput(dt);
world.step(1/60f, 6, 2);
player.update(dt);
gameCam.update();
maprenderer.setView(gameCam);
}
#Override
public void render(float dt) {
update(dt);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debug.render(world, gameCam.combined);
game.batch.setProjectionMatrix(gameCam.combined);
maprenderer.render();
game.batch.begin();
player.draw(game.batch);
game.batch.end();
}
#Override
public void resize(int width, int height) {}
#Override
public void pause() {}
#Override
public void resume() {}
#Override
public void hide() {}
#Override
public void dispose() {
map.dispose();
maprenderer.dispose();
world.dispose();
debug.dispose();
}
}
When I run my file (which automatically sets the game screen to this file), I only get a black screen. The map itself doesn't show. The map is 640 * 480 big and placed in the standard android/assets folder.
I tried running my map in other code I found online and it worked there. I couldn't figure out what's wrong with mine, however.
You need to update your viewport with device screen width and height.
#Override
public void resize(int width, int height) {
// use true here to center the camera
gamePort.update(width,height,false);
}
Take a look of this answer, recently I added as solution.
It seems like your map is actually rendered, but simply your Viewport is out of map position.
Try using:
gamecam.setToOrtho(false, gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2);
instead of
gameCam.position.set((gamePort.getWorldWidth() / 2), (gamePort.getWorldHeight() / 2), 0);
On my class I'm implementing ApplicationListener, so on my create method this is what I do:
public void create(){
camera=new Camera();
camera.setToOrtho(false,screenW,screenH);
}
//then on the render method:
public void render(){
camera.update();
}
But then when I use Gdx.input.getY() the result is reversed, when I go up the Y coordinate is less and when I go down the it gets higher.For better understanding:
Have a look at the camera class and the unproject method. That should translate between screen coordinates and world coordinates. (Probably more efficient way to do this, but for illustration):
float x = Gdx.input.getX();
float y = Gdx.input.getY();
float z = 0.0f;
Vector3 screenCoordinates = new Vector3(x, y, z);
Vector3 worldCoordinates = camera.unproject(screenCoordinates);
Then use the worldCoordinates vector for whatever you need it for.
EDIT: Added small working example and screenshot. My screen capture didn't capture the mouse, thus the red "star". But this simple app displays y coordinates in "initial" and "unprojected" coords as you move the mouse around the screen. Try it for yourself. I think this is what you are getting at, no?
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector3;
public class SimpleInputCoords implements ApplicationListener {
private OrthographicCamera camera;
#Override
public void create() {
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
}
#Override
public void render() {
int x = Gdx.input.getX();
int y = Gdx.input.getY();
int z = 0;
System.out.println("UnmodifiedYCoord:"+y);
Vector3 screenCoordinates = new Vector3(x, y, z);
Vector3 worldCoordinates = camera.unproject(screenCoordinates);
System.out.println("UnprojectedYCoord:"+worldCoordinates.y);
}
#Override
public void dispose() {
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
In Java coordinates start in the upper left corner (0,0). On a 100 x 100, (100,0) would be upper right, and (0, 100) would be lower left. So the behavior you are seeing is expected.
http://inetjava.sourceforge.net/lectures/part2_applets/InetJava-2.1-2.2-Introduction-to-AWT-and-Applets_files/image001.gif
I have had trouble playing music using libgdx. My code is as follows:
package com.me.fixGame;
import java.util.Random;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.ui.Table;
import com.badlogic.gdx.utils.Scaling;
import com.sun.jmx.snmp.tasks.Task;
public class fixGame implements ApplicationListener {
SpriteBatch batch;
SpriteBatch spriteBatch;
Texture trash;
Texture paper;
SpriteBatch spritebatch;
Vector2 position;
Vector2 pas;
boolean collide;
boolean countMe=false;
Vector2 size;
Vector2 size2;
Vector2 pos;
Rectangle bounds;
float posSpeed=30;
Rectangle bounds2;
float delay = 1; // seconds
boolean counted= false;
int score = 3;
//Texture Gogreen;
String myScore;
Texture background;
CharSequence str = "Lives left: 3"; // = myScore;
CharSequence line = "Score: 0"; // = myScore;
String myLife;
int life=0;
BitmapFont font;
float x;
float y;
Sound sound;
boolean collision = false;
#Override
public void create() {
//Gogreen = new Texture(Gdx.files.internal("data/gogreenNow.jpg"));
background = new Texture(Gdx.files.internal("data/trash.png"));
x= background.getWidth();
y=background.getHeight();
//float delaySeconds = 1;
spriteBatch = new SpriteBatch();
trash = new Texture(Gdx.files.internal("data/trash.png"));
paper = new Texture(Gdx.files.internal("data/paper1.jpg"));
sound = Gdx.audio.newSound(Gdx.files.internal("data/testjava.mp3"));
position = new Vector2(100, 50);
pos = new Vector2(54, 14);
batch = new SpriteBatch();
BitmapFont font = new BitmapFont();
size2 = new Vector2(trash.getWidth() ,trash.getHeight() );
//size2.y = trash.getHeight();
//size2.x = trash.getWidth();
size = new Vector2(paper.getWidth() ,paper.getHeight());
bounds= new Rectangle(pos.x, pos.y, size.x, size.y);
bounds2= new Rectangle(position.x, position.y, size2.x, size2.y);
}
#Override
public void dispose() {
}
public void update(){
bounds.set(pos.x, pos.y, size.x, size.y);
bounds2.set(position.x, position.y, size2.x, size2.y);
float pos1=Gdx.input.getAccelerometerX();
//if(pos1<0)
// pos1=(-1)*pos1;
position.x = position.x - 5*pos1;
}
#Override
public void render() {
sound.play(0.5f);
sound.dispose();
if(bounds.overlaps(bounds2)){
collision=true;
counted=true;
}else{
collision=false;
}
if(collision==true){
}
if(pos.y<640){
counted=false;
} else if(pos.y > 640 && collision==false && counted==false){
counted=true;
score= score-1;
myScore = "Lives left: " + score;
str = myScore;
}
if(bounds.overlaps(bounds2)){
countMe=true;
life= life+50;
myLife = "Score: " + life;
line = myLife;
}
if(position.x<0){
position.x= position.x+11;
}
if(position.x>425){
position.x= position.x-11;
}
update();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
pos.y=pos.y-posSpeed;
//posSpeed = posSpeed+(2/3);
if(pos.y<0){
pos.y = 700;
Random randomGenerator = new Random();
pos.x = randomGenerator.nextInt(500);
}
BitmapFont font = new BitmapFont();
batch.begin();
batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
if (!collision) {
batch.draw(paper, pos.x, pos.y);
}
//batch.draw(paper, pos.x, pos.y);
batch.draw(trash, position.x, position.y);
font.setScale(3);
font.setColor(0.0f, 0.0f, 1.0f,1.0f);
font.draw(batch, str, 300,900);
font.draw(batch, line, 300, 950);
batch.end();
font.dispose();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
My sound file I call over here:
sound = Gdx.audio.newSound(Gdx.files.internal("data/testjava.mp3"));
And the code I use to play it is:
sound.play();
sound.dispose();
For some reason it does not work on my desktop and my phone. I tested the the sound file actually had some sound in it.
You need to get rid of the sound.dispose(); call right after playing the sound, since it will dispose the sound file ;)
Just put it into your game's dispose(); method, so it will get removed after the game is done.
Also, you shouldn't call sound.play(0.5f); within your render-loop. Remember this function will get called about 60 times a second and you don't want to have the sound start 60 times a second.
So if it's some kind of sound effect that comes with a special event, like firing a bullet or hitting a target etc, just call sound.play(); one time, when you are actually firing the event.
https://github.com/libgdx/libgdx/wiki/Sound-effects
If you want to play background music, you should try streaming the music file. The wiki is awesome for that: https://github.com/libgdx/libgdx/wiki/Streaming-music
Hope it helps :)
In my libgdx game it functions how i want it to but when i exit the game it starts of from where i was before, i want it to restart. The code is as follows.
package com.me.fixGame;
import java.util.Random;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
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.ui.Table;
import com.badlogic.gdx.utils.Scaling;
import com.sun.jmx.snmp.tasks.Task;
public class fixGame implements ApplicationListener {
SpriteBatch batch;
SpriteBatch spriteBatch;
Texture trash;
Texture paper;
SpriteBatch spritebatch;
Vector2 position;
Vector2 pas;
boolean collide;
boolean countMe=false;
Vector2 size;
Vector2 size2;
Vector2 pos;
Rectangle bounds;
float posSpeed=30;
Rectangle bounds2;
float delay = 1; // seconds
boolean counted= false;
int score = 3;
//Texture Gogreen;
String myScore;
Texture background;
CharSequence str = "Lives left: 3"; // = myScore;
CharSequence line = "Score: 0"; // = myScore;
String myLife;
int life=0;
BitmapFont font;
float x;
float y;
boolean collision = false;
#Override
public void create() {
//Gogreen = new Texture(Gdx.files.internal("data/gogreenNow.jpg"));
background = new Texture(Gdx.files.internal("data/trash.png"));
x= background.getWidth();
y=background.getHeight();
//float delaySeconds = 1;
spriteBatch = new SpriteBatch();
trash = new Texture(Gdx.files.internal("data/trash.png"));
paper = new Texture(Gdx.files.internal("data/paper1.jpg"));
position = new Vector2(100, 50);
pos = new Vector2(54, 14);
batch = new SpriteBatch();
BitmapFont font = new BitmapFont();
size2 = new Vector2(trash.getWidth() ,trash.getHeight() );
//size2.y = trash.getHeight();
//size2.x = trash.getWidth();
size = new Vector2(paper.getWidth() ,paper.getHeight());
bounds= new Rectangle(pos.x, pos.y, size.x, size.y);
bounds2= new Rectangle(position.x, position.y, size2.x, size2.y);
}
#Override
public void dispose() {
}
public void update(){
bounds.set(pos.x, pos.y, size.x, size.y);
bounds2.set(position.x, position.y, size2.x, size2.y);
float pos1=Gdx.input.getAccelerometerX();
//if(pos1<0)
// pos1=(-1)*pos1;
position.x = position.x - 5*pos1;
}
#Override
public void render() {
if(bounds.overlaps(bounds2)){
collision=true;
counted=true;
}else{
collision=false;
}
if(collision==true){
}
if(pos.y<640){
counted=false;
} else if(pos.y > 640 && collision==false && counted==false){
counted=true;
score= score-1;
myScore = "Lives left: " + score;
str = myScore;
}
if(bounds.overlaps(bounds2)){
countMe=true;
life= life+50;
myLife = "Score: " + life;
line = myLife;
}
if(position.x<0){
position.x= position.x+11;
}
if(position.x>425){
position.x= position.x-11;
}
update();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
pos.y=pos.y-posSpeed;
//posSpeed = posSpeed+(2/3);
if(pos.y<0){
pos.y = 700;
Random randomGenerator = new Random();
pos.x = randomGenerator.nextInt(500);
}
BitmapFont font = new BitmapFont();
batch.begin();
batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
if (!collision) {
batch.draw(paper, pos.x, pos.y);
}
//batch.draw(paper, pos.x, pos.y);
batch.draw(trash, position.x, position.y);
font.setScale(3);
font.setColor(0.0f, 0.0f, 1.0f,1.0f);
font.draw(batch, str, 300,900);
font.draw(batch, line, 300, 950);
batch.end();
font.dispose();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
I searched through out the web and I could not find anything. Does anybody have any answers?
Any help would be appreciated thanks in advance.
Are you talking about desktop or Android?
Assuming you're talking about Android, when the user exits the game, the pause() function is called. When the user goes back to the game, the resume() function is called.
I would bet that your game would "reset" if you ran some other apps between exiting and resuming the game. Normally people save the state of the game in pause() and then load it in resume(), but for your case, it sounds like you just want to reset it each time.
If all of the above is actually true for you, just reset the game state in the resume() function.
For Android: If the user presses the "Home" button or a call is incoming the games pause() method is called. If the user returns after the call or after some time normally resume() is called. But if the Android OS decided to close your app, create() will be called, and if you do not store savegames i am sure it would reset the game.
In your case the user did not exit the game but "pause" it by pressing "Home" button. To reset the game then, you could call dispose() in your pause() method, and in dispose you simply close your app. On Desktop pause() is called if you switch window or minimize the app, as far as i know. If you do not want to close the app in this case you have to controll, if it is desktop or android.