when i run emulator and start application i see blinks screen, but on a PC in DesktopLauncher screen is normal.
I can not understand where I made mistakes.
render method override but it does not help.
class starter:
public class Drop extends Game {
Camera cam;
Game game;
SpriteBatch batch;
int tempGameScore = 0;
int dropsGatchered = 0;
Preferences preferences;//сохраняем игру
#Override
public void create() {
batch = new SpriteBatch();
this.setScreen(new MainMenuScreen(this));
}
#Override
public void render() {
super.render();
}
#Override
public void dispose() {
batch.dispose();
}
MainMenu:
I think I'm working wrong with the scene tool.
Can it is necessary as otherwise to draw a scene?
public class MainMenuScreen implements Screen {
Sprite texture;
final Drop game;
Skin skin;
Stage stage;
OrthographicCamera camera;
ImageButton newGameButton, exit, highScore;
Table table;
ImageButton.ImageButtonStyle btnplayStyle, btnscoreStyle, btnexitStyle;
private static TextureAtlas atlas, backAtlas;
public MainMenuScreen(final Drop gam) {
this.game = gam;
atlas = new TextureAtlas(Gdx.files.internal("texture/texture.pack"), true);
backAtlas = new TextureAtlas(Gdx.files.internal("texture/background.pack"), true);
texture = new Sprite(backAtlas.findRegion("background"));
skin = new Skin();
skin.addRegions(atlas);
table = new Table();
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
stage = new Stage();
stage.addActor(table);
Gdx.input.setInputProcessor(stage);// Make the stage consume events
table();
}
private void table() {
btnplayStyle = new ImageButton.ImageButtonStyle();
btnplayStyle.up = skin.getDrawable("play");//кнопка не нажата
btnplayStyle.over = skin.getDrawable("play");
btnplayStyle.down = skin.getDrawable("play"); // кнопка нажата
newGameButton = new ImageButton(btnplayStyle);
newGameButton.setSize(300, 200);
stage.addActor(newGameButton);
newGameButton.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new GameScreen(game));
}
});
//Button score
btnscoreStyle = new ImageButton.ImageButtonStyle();
btnscoreStyle.up = skin.getDrawable("records");//кнопка не нажата
btnscoreStyle.over = skin.getDrawable("records");
btnscoreStyle.down = skin.getDrawable("records"); // кнопка нажата
highScore = new ImageButton(btnscoreStyle);
highScore.setSize(300, 200);
stage.addActor(highScore);
highScore.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new Score(game) {
});
}
});
//Button EXIT
btnexitStyle = new ImageButton.ImageButtonStyle();
btnexitStyle.up = skin.getDrawable("exit");//кнопка не нажата
btnexitStyle.over = skin.getDrawable("exit");
btnexitStyle.down = skin.getDrawable("exit"); // кнопка нажата
exit = new ImageButton(btnexitStyle);
exit.setSize(300, 200);
stage.addActor(exit);
exit.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
game.batch.begin();
game.batch.draw(texture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
table.add(newGameButton).width(400).height(120);
table.getCell(newGameButton).spaceBottom(30);
table.row();
table.add(highScore).width(400).height(120);
table.getCell(highScore).spaceBottom(30);
table.row();
table.add(exit).width(400).height(120);
table.getCell(exit).spaceBottom(30);
table.row();
game.batch.end();
}
#Override
public void render(float delta) {
stage.act();
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}
I think I'm working wrong with the scene tool
In MainMenuScreen you have to draw things in the render() method, not in the show() method. Like this:
#Override
public void render() {
stage.draw();
// ... possibly more drawing code
}
Related
I have a game screen in which I want to implement both the game itself and the control buttons. I divided this screen into two parts using TABLE.
The control buttons and the game are implemented in different classes. Everything works correctly, except for displaying the game.
How to make the game fit in the top window?
Game Screen code
public class GameScreen implements Screen {
private Main parent;
private Stage stage;
public static SnakeControl snakeControl;
private SpriteBatch batch;
private GameControl game;
public GameScreen(Main main) {
parent = main;
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);
snakeControl = new SnakeControl();
GameAssets.instance().loadAssets();
batch = new SpriteBatch();
game = new GameControl();
}
#Override
public void show() {
Table table = new Table();
table.setFillParent(true);
table.setDebug(true);
table.row();
table.add(game).expand().pad(10f, 5f, 10f, 5f);
table.row();
table.add(snakeControl.get_tSnakeControl()).fillX().height(GameInfo.SCREEN_HEIGHT/3.5f).pad(0f, 5f, 10f, 5f);
stage.addActor(table);
}
#Override
public void render(float delta) {
game.update(Gdx.graphics.getDeltaTime());
clearScreen();
batch.begin();
game.render(batch);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
batch.end();
}
private void clearScreen() {
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#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() {
}
#Override
public void dispose() {
stage.dispose();
batch.dispose();
GameAssets.instance().dispose();
}
The Game code
public class GameControl extends Actor {
private Board board;
private Snake snake;
private float timeState;
private BitmapFont font;
private GameObject food;
private boolean isGameOver;
public GameControl() {
TextureAtlas atlas = GameAssets.instance().get(GameAssets.SNAKE_PACK);
font = GameAssets.instance().get(GameAssets.PIXEL_FONT);
snake = new Snake(atlas);
board = new Board(snake, GameInfo.SCREEN_WIDTH, GameInfo.SCREEN_HEIGHT);
food = board.generateFood();
init();
}
private void init() {
GameSoundsPlayer.init();
GameSoundsPlayer.playMusic(GameAssets.MEMO_SOUND, false);
}
public void update(float delta) {
if (snake.hasLive()) {
timeState += delta;
snake.handleEvents();
if (timeState >= .09f) {
snake.moveBody();
timeState = 0;
}
if (snake.isCrash()) {
snake.reset();
snake.popLife();
GameSoundsPlayer.playSound(GameAssets.CRASH_SOUND, false);
}
if (snake.isFoodTouch(food)) {
GameSoundsPlayer.playSound(GameAssets.EAT_FOOD_SOUND, false);
Scorer.score();
snake.grow();
food = board.generateFood();
}
} else {
gameOver();
if (Gdx.input.isKeyJustPressed(Input.Keys.ANY_KEY)) start();
}
}
private void gameOver() {
isGameOver = true;
GameSoundsPlayer.stopMusic(GameAssets.MEMO_SOUND);
GameSoundsPlayer.playMusic(GameAssets.GAME_OVER_SOUND, false);
}
private void start() {
GameSoundsPlayer.playMusic(GameAssets.MEMO_SOUND, false);
GameSoundsPlayer.stopMusic(GameAssets.GAME_OVER_SOUND);
isGameOver = false;
snake.reset();
snake.restoreHealth();
food = board.generateFood();
Scorer.reset();
}
public void render(SpriteBatch batch) {
board.render(batch);
food.draw(batch);
snake.render(batch);
if (isGameOver) {
font.draw(batch, "GAME OVER", (GameInfo.SCREEN_WIDTH - 100) / 2, (GameInfo.SCREEN_HEIGHT + 100) / 2);
font.draw(batch, "Press any key to continue", (GameInfo.SCREEN_WIDTH - 250) / 2, (GameInfo.SCREEN_HEIGHT + 50) / 2);
}
font.draw(batch, "Player: ", GameInfo.SCALE * 4, GameInfo.SCREEN_HEIGHT - 10);
font.draw(batch, "Score: " + Scorer.getScore(), GameInfo.SCALE / 2, GameInfo.SCREEN_HEIGHT - 10);
font.draw(batch, "Size: " + snake.getBody().size(), GameInfo.SCALE / 2, GameInfo.SCREEN_HEIGHT - 40);
}
}
I used Viewport for this!!!
#Override
public void render(float delta) {
clearScreen();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/3);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
Gdx.gl.glViewport(0, Gdx.graphics.getHeight()/3, Gdx.graphics.getWidth(), (Gdx.graphics.getHeight() - Gdx.graphics.getHeight()/3));
game.update(Gdx.graphics.getDeltaTime());
batch.begin();
game.render(batch);
batch.end();
}
I'm trying to get a nice fade in/ fade out between two of my screens, and after hours of searching online I have come up empty handed. I've attempted various solutions involving Actions to no avail, and I am not to keen on using TweenEngine, but I would appreciate any help!
Below is the closest solution i've found. This one simply delay the time before the screens switch, yet you don't see a fade in any way.
package com.aidanstrong.game;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.ColorAction;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Timer;
public class FadingGame extends Game {
GameScreen gameScreen;
UpgradeScreen upgradeScreen;
private Actor fadeActor = new Actor();
private ShapeRenderer fadeRenderer;
#Override
public void create() {
gameScreen = new GameScreen(this);
upgradeScreen = new UpgradeScreen(this);
setScreen(gameScreen);
fadeRenderer = new ShapeRenderer(8);
}
public void setScreenWithFade (final Screen screen, float duration) {
fadeActor.clearActions();
fadeActor.setColor(Color.CLEAR);
fadeActor.addAction(Actions.sequence(
Actions.color(Color.BLACK, duration/2f, Interpolation.fade),
Actions.run(new Runnable(){public void run(){setScreen(screen);}}),
Actions.color(Color.CLEAR, duration/2f, Interpolation.fade)
));
}
#Override
public void render (){
super.render();
fadeActor.act(Gdx.graphics.getDeltaTime());
float alpha = fadeActor.getColor().a;
if (alpha != 0){
fadeRenderer.begin(ShapeRenderer.ShapeType.Filled);
fadeRenderer.setColor(0, 0, 0, alpha);
fadeRenderer.rect(-1, -1, 2, 2); //full screen rect w/ identity matrix
fadeRenderer.end();
}
}
}
Add topLayer as an Image on your stage and add Action on that Actor. Try this test case:
public class GdxTest extends Game {
FirstScreen firstScreen;
SecondScreen secondScreen;
#Override
public void create() {
firstScreen=new FirstScreen();
secondScreen=new SecondScreen();
setScreen(firstScreen);
}
public static Texture getTexture(){
Pixmap pixmap;
try {
pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
}catch (GdxRuntimeException e)
{
pixmap=new Pixmap(1,1, Pixmap.Format.RGB565);
}
pixmap.setColor(Color.WHITE);
pixmap.drawRectangle(0,0,1,1);
return new Texture(pixmap);
}
}
FirstScreen
public class FirstScreen extends ScreenAdapter {
Stage stage;
Texture texture,white;
#Override
public void show() {
stage=new Stage();
final Image image = new Image(texture=new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth()/2, stage.getHeight()/2, Align.center);
stage.addActor(image);
final Image topLayer = new Image(new TextureRegion(white=GdxTest.getTexture()));
topLayer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
topLayer.setColor(Color.BLACK);
stage.addActor(topLayer);
stage.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
topLayer.addAction(Actions.sequence(Actions.color(Color.BLACK,2),Actions.run(new Runnable() {
#Override
public void run() {
GdxTest gdxTest=((GdxTest)Gdx.app.getApplicationListener());
gdxTest.setScreen(gdxTest.secondScreen);
}
})));
super.clicked(event, x, y);
}
});
topLayer.addAction(Actions.fadeOut(2));
Gdx.input.setInputProcessor(stage);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
stage.dispose();
texture.dispose();
white.dispose();
}
}
SecondScreen
public class SecondScreen extends ScreenAdapter{
Stage stage;
Texture texture,white;
#Override
public void show() {
stage=new Stage();
final Image image = new Image(texture=new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth() / 2, stage.getHeight() / 2, Align.center);
image.setOrigin(Align.center);
image.rotateBy(90);
stage.addActor(image);
final Image topLayer = new Image(new TextureRegion(white=GdxTest.getTexture()));
topLayer.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
topLayer.setColor(Color.BLACK);
stage.addActor(topLayer);
topLayer.addAction(Actions.fadeOut(2));
Gdx.input.setInputProcessor(stage);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
stage.act();
}
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
stage.dispose();
texture.dispose();
white.dispose();
}
}
Or
You can use FBO, at the top of your stage.
EDIT
public class GdxTest extends ApplicationAdapter {
Stage stage;
FrameBuffer frameBuffer;
float delta;
#Override
public void create() {
stage=new Stage();
{
final Image image = new Image(new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth() / 2, stage.getHeight() / 2, Align.center);
stage.addActor(image);
}
{
final Image image = new Image(new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth()/3, stage.getHeight()/3, Align.center);
stage.addActor(image);
}
{
final Image image = new Image(new Texture("badlogic.jpg"));
image.setSize(200, 200);
image.setPosition(stage.getWidth()*.75f, stage.getHeight()*.75f, Align.center);
stage.addActor(image);
}
Gdx.input.setInputProcessor(stage);
delta=1;
}
#Override
public void render() {
if(delta>0)delta-=.01f;
frameBuffer.begin();
Gdx.gl.glClearColor(0,0,0,delta);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
frameBuffer.end();
stage.act();
stage.draw();
stage.getBatch().setProjectionMatrix(stage.getBatch().getProjectionMatrix().idt());
stage.getBatch().begin();
stage.getBatch().draw(frameBuffer.getColorBufferTexture(),-1,1,2,-2);
stage.getBatch().end();
}
#Override
public void dispose() {
stage.dispose();
frameBuffer.dispose();
}
#Override
public void resize(int width, int height) {
if(frameBuffer !=null && (frameBuffer.getWidth()!=width || frameBuffer.getHeight()!=height )) {
frameBuffer.dispose();
frameBuffer=null;
}
if(frameBuffer==null){
try {
frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false);
}catch (GdxRuntimeException e){
frameBuffer=new FrameBuffer(Pixmap.Format.RGB565,width,height,false);
}
}
}
}
I am making a 2d game using libgdx and am adding hexagon shaped actors to a group which is then added to a stage. For a normal camera you can use camera.zoom in the render method to zoom in and out along with camera.translate to pan around the world.
I have been getting the camera used by the stage using stage.getCamera() and I can still call stage.getcamera().translate however there is no stage.getCamera().zoom option.
Here is my code:
//import statements
public class HexGame implements ApplicationListener{
private Stage stage;
private Texture hexTexture;
private Group hexGroup;
private int screenWidth;
private int screenHeight;
#Override
public void create() {
hexTexture = new Texture(Gdx.files.internal("hex.png"));
screenHeight = Gdx.graphics.getHeight();
screenWidth = Gdx.graphics.getWidth();
stage = new Stage(new ScreenViewport());
hexGroup = new HexGroup(screenWidth,screenHeight,hexTexture);
stage.addActor(hexGroup);
}
#Override
public void dispose() {
stage.dispose();
hexTexture.dispose();
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
handleInput();
stage.getCamera().update();
}
private void handleInput() {
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
stage.getCamera().translate(-3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
stage.getCamera().translate(3, 0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
stage.getCamera().translate(0, -3, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
stage.getCamera().translate(0, 3, 0);
}
//This is the part that doesn't work
/*
if (Gdx.input.isKeyPressed(Input.Keys.Z)) {
stage.getCamera().zoom += 0.02;
}
*/
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
Any help is appreciated, and if there is anything else wrong with my code please let me know, I'm new to libgdx. Thanks
Zoom is available in OrthographicCamera class and by default Stage class create a OrthographicCamera
/** Creates a stage with a {#link ScalingViewport} set to {#link Scaling#stretch}. The stage will use its own {#link Batch}
* which will be disposed when the stage is disposed. */
public Stage () {
this(new ScalingViewport(Scaling.stretch, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), new OrthographicCamera()),
new SpriteBatch());
ownsBatch = true;
}
So what you need is to cast your camera to OrthographicCamera:
((OrthographicCamera)stage.getCamera()).zoom += 0.02f;
I need your help for a simple problem.
I implemented my Java game using LibGDX library.
My Game has following structure:
- main menù with some buttons;
- game screen;
- pause menù opened when user press P keyword.
The probles is the this. When I press P, pause menù opens. If I click on MainMenù button in pause menù , the screen go on main menù. After that, If I press on game button , it appears pause menù screen and not game screen.
How can I solve this?
public class MenuPause implements Screen {
private Skin skin;
private Stage stage;
private MyGdxGame game;
private Level level=new Level(true);
private World world;
private GameScreen8Bit GS;
private int era;
/*****Button *******/
private Button saveGame;
private Button loadGame;
private Button continueGame;
private Button exitGame;
private Button mainMenu;
/******************/
private SpriteBatch batch;
private TextureAtlas atlas;
private TextureRegion imagetexture;
public MenuPause(MyGdxGame game, int era){
this.game=game;
}
private void createBasicSkin(){
//Create a font
atlas = new TextureAtlas(Gdx.files.internal("images/textures/game.atlas"));
BitmapFont font = new BitmapFont();
skin = new Skin();
skin.add("default", font);
//Create a texture
Pixmap pixmap = new Pixmap((int)Gdx.graphics.getWidth()/4,(int)Gdx.graphics.getHeight()/10, Pixmap.Format.RGB888);
pixmap.setColor(Color.WHITE);
pixmap.fill();
skin.add("background",new Texture(pixmap));
//Create a button style
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.up = skin.newDrawable("background", Color.GRAY);
textButtonStyle.down = skin.newDrawable("background", Color.DARK_GRAY);
textButtonStyle.checked = skin.newDrawable("background", Color.DARK_GRAY);
textButtonStyle.over = skin.newDrawable("background", Color.LIGHT_GRAY);
textButtonStyle.font = skin.getFont("default");
skin.add("default", textButtonStyle);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Vector3 touchPos = new Vector3();
touchPos.set((Gdx.input.getX()), (Gdx.graphics.getHeight()-Gdx.input.getY()), 0);
if(exitGame.mousePassage(touchPos))
{
exitGame=new Button(atlas.findRegion("exitNoPressed"));
exitGame.setPos(0, 0);
}
else
{
exitGame=new Button(atlas.findRegion("exit"));
exitGame.setPos(0, 0);
}
if(loadGame.mousePassage(touchPos))
{
loadGame=new Button(atlas.findRegion("loadNoPressed"));
loadGame.setPos(Gdx.graphics.getWidth()/2-atlas.findRegion("loadNoPressed").getRegionWidth()/2, (Gdx.graphics.getHeight()/2));
}
else
{
loadGame=new Button(atlas.findRegion("load"));
loadGame.setPos(Gdx.graphics.getWidth()/2-atlas.findRegion("load").getRegionWidth()/2, (Gdx.graphics.getHeight()/2));
}
if(saveGame.mousePassage(touchPos))
{
saveGame=new Button(atlas.findRegion("saveNoPressed"));
saveGame.setPos(0 , Gdx.graphics.getHeight()/3);
}
else
{
saveGame=new Button(atlas.findRegion("save"));
saveGame.setPos(0 , Gdx.graphics.getHeight()/3);
}
if(continueGame.mousePassage(touchPos))
{
continueGame=new Button(atlas.findRegion("continueNoPressed"));
continueGame.setPos((Gdx.graphics.getWidth() - atlas.findRegion("continueNoPressed").getRegionWidth()) , 0);
}
else
{
continueGame=new Button(atlas.findRegion("continue"));
continueGame.setPos((Gdx.graphics.getWidth() - atlas.findRegion("continue").getRegionWidth()) , 0);
}
if(mainMenu.mousePassage(touchPos))
{
mainMenu=new Button(atlas.findRegion("mainNoPressed"));
mainMenu.setPos(Gdx.graphics.getWidth()/2-atlas.findRegion("mainNoPressed").getRegionWidth()/2 , 0);
}
else
{
mainMenu=new Button(atlas.findRegion("main"));
mainMenu.setPos((Gdx.graphics.getWidth()/2-atlas.findRegion("main").getRegionWidth()/2) , 0);
}
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
if(exitGame.isPressed(touchPos)){
int n= JOptionPane.showConfirmDialog(null,"Sei sicuro di volere uscire dal gioco?", "EXIT", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
if(n==0)
{
System.exit(0);
}
}
if(loadGame.isPressed(touchPos)){
new LoadGame(level,false);
}
if(saveGame.isPressed(touchPos)){
SavePanel savePanel = new SavePanel( level, game,false);
savePanel.save();
}
if(mainMenu.isPressed(touchPos)){
dispose();
this.dispose();
game.setScreen(new MainMenu(game,0));
}
if(continueGame.isPressed(touchPos)){
//game.setScreen(new MainMenu(game));//mainMenu non funziona...eccezione
//game.setScreen(new GameScreen8Bit(game,level));
}
}
batch.begin();
batch.draw(imagetexture,0 , 0);
saveGame.draw(batch);
loadGame.draw(batch);
continueGame.draw(batch);
exitGame.draw(batch);
mainMenu.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
batch = new SpriteBatch();
createBasicSkin();
saveGame= new Button(atlas.findRegion("save"));
saveGame.setPos(0 , Gdx.graphics.getHeight()/3);
loadGame=new Button(atlas.findRegion("load"));
loadGame.setPos(Gdx.graphics.getWidth()/2-atlas.findRegion("load").getRegionWidth()/2, (Gdx.graphics.getHeight()/2));
continueGame=new Button(atlas.findRegion("continue"));
continueGame.setPos((Gdx.graphics.getWidth() - atlas.findRegion("continue").getRegionWidth()) , 0);
exitGame=new Button(atlas.findRegion("exit"));
exitGame.setPos(0, 0);
mainMenu=new Button(atlas.findRegion("main"));
mainMenu.setPos(Gdx.graphics.getWidth()/2-atlas.findRegion("load").getRegionWidth()/2, 0);
Texture immagine=new Texture(Gdx.files.internal("data/ghiacciairid.png"));
if(era==1)
{
immagine=new Texture(Gdx.files.internal("data/8bitrid.png"));
}
else if(era==2)
{
immagine=new Texture(Gdx.files.internal("data/16bitrid.png"));
}
else if(era==3)
{
immagine=new Texture(Gdx.files.internal("data/3d.png"));
}
imagetexture =new TextureRegion(immagine,640,480);
imagetexture.setRegionHeight(480);
imagetexture.setRegionWidth(640);
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
I know this is a year old, but I am answering anyway.
Don't create menu items every frame in render. Create once.
public class GameScreen implements Screen{
private State state; //this keeps track of where
public static enum State{
RUNNING, PAUSE, SAVEGAME
}
public MenuPause(MyGdxGame game, int era){
this.game=game;
state = State.RUNNING
}
#Override
public void show() {
//do the loading and creating here
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
switch(state){
case RUNNING: running();
break;
case PAUSE: pause();
break;
case SAVEGAME: savegame();
break;
default: break;
}
}
private void running(){
}
private void pause(){
}
private void savegame(){
}
}
I am having some issues with loading button.pack made in TexturePacker. I really can't find where is the problem. If someone can see it from my GameScreen class please let me know. Thank you very much.
public class GameScreen implements Screen {
Stage stage;
TextureAtlas buttonAtlas;
TextButtonStyle buttonStyle;
TextButton button;
Skin skin;
SpriteBatch batch;
private GameWorld world;
private GameRenderer renderer;
private float runTime;
// This is the constructor, not the class declaration
public GameScreen() {
float screenWidth = Gdx.graphics.getWidth();
float screenHeight = Gdx.graphics.getHeight();
float gameWidth = 544;
float gameHeight = screenHeight / (screenWidth / gameWidth);
world = new GameWorld();
renderer = new GameRenderer(world, (int) gameHeight, (int) gameWidth);
Gdx.input.setInputProcessor(new InputHandler(world));
}
#Override
public void render(float delta) {
runTime += delta;
world.update(delta);
renderer.render(runTime);
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act();
batch.begin();
stage.draw();
batch.end();
}
#Override
public void resize(int width, int height) {
System.out.println("GameScreen - resizing");
}
#Override
public void show() {
System.out.println("GameScreen - show called");
stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(),
true);
skin = new Skin();
buttonAtlas = new TextureAtlas("button.pack");
skin.addRegions(buttonAtlas);
}
#Override
public void hide() {
System.out.println("GameScreen - hide called");
}
#Override
public void pause() {
System.out.println("GameScreen - pause called");
}
#Override
public void resume() {
System.out.println("GameScreen - resume called");
}
#Override
public void dispose() {
// Leave blank
}
}
Every time, no matter what change I make, it says "Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: File not found: button.pack (Internal)"
I checked in assets folder, button.pack and button.png are there :/
Use AssetManager for your game.
Code:
AssetManager am = new AssetManager();
am.load("buttons.pack", TextureAtlas.class);
am.finishLoading();
buttonAtlas = am.get("buttons.pack");
Then you can use your atlas.
And don't forget to put your atlas to game-android/assets directory.