LibGDX Fade Out / Fade In Screen Transition - java

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);
}
}
}
}

Related

Clickable textures

I'm kind of new to LibGdx and android studio.
I'm trying to create clickable textures, one for play and one for credits.
Both should be opening a new empty screen/event.
public void create() {
batch = new SpriteBatch();
img = new Texture("Main_Screen.png");
music = Gdx.audio.newMusic(Gdx.files.internal("bgmusic.wav"));
music.play();
music.setLooping(true);
credits = new Texture("credits.png");
play = new Texture("play.png");
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, 0, 0);
batch.draw(play, 340, 1400);
batch.draw(credits, 340, 400);
batch.end();
}
However i'm unsure on how to do this since i'm also creating the background with a texture, so i'd be very happy if someone could assist me with helping me out.
Use Sprite instead of Texture, that holds the geometry, color, and texture information for drawing 2D sprites.
MyGdxGame
public class MyGdxGame extends Game {
public Screen menuScreen,creditsScreen,playScreen;
#Override
public void create () {
menuScreen=new MenuScreen(this);
creditsScreen=new CreditsScreen();
playScreen=new PlayScreen();
setScreen(menuScreen);
}
}
MenuScreen
public class MenuScreen extends InputAdapter implements Screen {
SpriteBatch batch;
Texture background,play,credits;
Sprite backgoundSprite,playSprite,creditsSprite;
private ExtendViewport extendViewport;
OrthographicCamera cam;
private float w=480;
private float h=800;
private Vector3 vector3;
MyGdxGame game;
Music music;
public MenuScreen(MyGdxGame game){
this.game=game;
}
#Override
public void show() {
batch = new SpriteBatch();
cam = new OrthographicCamera();
extendViewport=new ExtendViewport(w,h,cam);
vector3=new Vector3();
background = new Texture("Main_Screen.png");
play=new Texture("play.png");
credits=new Texture("credits.png");
backgoundSprite=new Sprite(background);
backgoundSprite.setSize(w,h); // If resources are not in context of your viewport
backgoundSprite.setPosition(0,0); //Default Position
playSprite=new Sprite(play);
playSprite.setSize(100,100);
playSprite.setPosition(w/2-playSprite.getWidth()/2,h/2+100);
creditsSprite=new Sprite(credits);
creditsSprite.setSize(100,100);
creditsSprite.setPosition(w/2-creditsSprite.getWidth()/2,h/2-100);
Gdx.input.setInputProcessor(this);
music = Gdx.audio.newMusic(Gdx.files.internal("bgmusic.wav"));
music.play();
music.setLooping(true);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(cam.combined);
batch.begin();
backgoundSprite.draw(batch);
playSprite.draw(batch);
creditsSprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
extendViewport.update(width,height);
cam.position.x = w /2;
cam.position.y = h/2;
cam.update();
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
vector3.set(screenX,screenY,0);
Vector3 position=cam.unproject(vector3);
if(playSprite.getBoundingRectangle().contains(position.x,position.y)) {
game.setScreen(game.playScreen);
}
if(creditsSprite.getBoundingRectangle().contains(position.x,position.y)){
game.setScreen(game.creditsScreen);
}
return super.touchDown(screenX, screenY, pointer, button);
}
#Override
public void pause() { }
#Override
public void resume() { }
#Override
public void hide() { }
#Override
public void dispose() {
batch.dispose();
background.dispose();
play.dispose();
credits.dispose();
}
}
CreditsScreen
public class CreditsScreen implements Screen {
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#Override
public void resize(int width, int height) { }
#Override
public void pause() { }
#Override
public void resume() { }
#Override
public void hide() { }
#Override
public void dispose() { }
}
PlayScreen
public class PlayScreen implements Screen {
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#Override
public void resize(int width, int height) { }
#Override
public void pause() { }
#Override
public void resume() { }
#Override
public void hide() { }
#Override
public void dispose() { }
}
Welcome to libGDX!
If I understand your question correctly, you are trying to figure out how to change screens in your game. At a high level, this is what you'd need to do:
Draw your button texture on the screen.
Use Gdx.input to detect when the player lifts their mouse button (and if it's over one of your buttons when they do it)
Switch to the new screen (presumably with a new background) when this happens.
I would recommend that you first complete the "A Simple Game" and "Extending the Simple Game" tutorials in order to familiarize yourself with the basics of libGDX and the Game and Screen classes.
Next, try using Scene2D.UI (there are links at the bottom of the second tutorial) to add a Stage and TextButtons to your main menu.
Hopefully that helps you get started- there is a lot of helpful information on that libGDX wiki which I think you will find helpful.

How fix in libgdx blinks screen in Desktop Mode?

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
}

LibGDX basic stage draw error

I'm trying to create a game, but my code keeps giving me error:
Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: no uniform with name 'u_projModelView' in shader
at com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:287)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:277)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:507)
at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformMatrix(ShaderProgram.java:498)
at com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20.flush(ImmediateModeRenderer20.java:147)
at com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20.end(ImmediateModeRenderer20.java:160)
at com.badlogic.gdx.graphics.glutils.ShapeRenderer.end(ShapeRenderer.java:1104)
at com.badlogic.gdx.graphics.glutils.ShapeRenderer.check(ShapeRenderer.java:1092)
at com.badlogic.gdx.graphics.glutils.ShapeRenderer.rect(ShapeRenderer.java:389)
at com.badlogic.gdx.scenes.scene2d.ui.Table.drawDebugRects(Table.java:1224)
at com.badlogic.gdx.scenes.scene2d.ui.Table.drawDebug(Table.java:1204)
at com.badlogic.gdx.scenes.scene2d.Group.drawDebugChildren(Group.java:156)
at com.badlogic.gdx.scenes.scene2d.Group.drawDebug(Group.java:139)
at com.badlogic.gdx.scenes.scene2d.Stage.drawDebug(Stage.java:169)
at com.badlogic.gdx.scenes.scene2d.Stage.draw(Stage.java:132)
at net.lukshe.lukshegame2.screens.MainMenu.render(MainMenu.java:71)
at com.badlogic.gdx.Game.render(Game.java:46)
at net.lukshe.lukshegame2.LuksheGame.render(LuksheGame.java:23)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
MainMenu.java:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
public class MainMenu implements Screen {
private Stage stage; //done
private TextureAtlas atlas; //done
private Skin skin; //done
private Table table; //done
private TextButton buttonplay, buttonexit;
private BitmapFont white; //done
private Label heading;
#Override
public void show() {
stage = new Stage();
atlas = new TextureAtlas("ui/button.pack");
skin = new Skin(atlas);
table = new Table(skin);
table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
white = new BitmapFont(Gdx.files.internal("font/white.fnt"), false);
//Set the button style
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("buttonup");
textButtonStyle.down = skin.getDrawable("buttondown");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = white;
textButtonStyle.fontColor = Color.BLACK;
//Creating exit button
buttonexit = new TextButton("Exit", textButtonStyle);
buttonexit.pad(20);
//Add button to the table
table.add(buttonexit);
//Debug
table.debug();
//Add table to the stage
stage.addActor(table);
}
#Override
public void render(float delta) {
//OpenGL settings
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
//Render at the speed of delta
stage.act(delta);
//Draw stage
stage.draw();
}
LuksheGame.java:
public class LuksheGame extends Game {
public static final String NAME = "Lukse game (LibGDX)", VERSION = "Pre-Alpha 0.0.0.2";
#Override
public void create() {
setScreen(new Splash());
}
#Override
public void dispose() {
super.dispose();
}
#Override
public void render() {
super.render();
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void pause() {
super.pause();
}
#Override
public void resume() {
super.resume();
}
}
If someone can help me, please help.
EDIT:
MainMenu.java is called from Splash.java. In Splash class I'm using Tween engine to increase and decrease opacity of the splash screen.
Splash.java:
public class Splash implements Screen {
private SpriteBatch batch;
private Sprite splash;
private TweenManager twm;
#Override
public void show() {
batch = new SpriteBatch();
twm = new TweenManager();
Tween.registerAccessor(Sprite.class, new SpriteAccessor());
Texture splashtexture = new Texture(Gdx.files.internal("images/splash.png"));
splash = new Sprite(splashtexture);
splash.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Tween.set(splash, SpriteAccessor.alpha).target(0).start(twm);
Tween.to(splash, SpriteAccessor.alpha, 2).target(1).repeatYoyo(1, 2).setCallback(new TweenCallback() {
#Override
public void onEvent(int type, BaseTween<?> source) {
((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu());
}
}).start(twm);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
twm.update(delta);
batch.begin();
splash.draw(batch);
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() {
batch.dispose();
splash.getTexture().dispose();
}
}

libGDX actor draw() not called

Please forgive me for my english.
Began to explore libGDX and have a problem. When I add actor on stage, method draw() not called.
Tried to apply the method to draw a straight line, texture successfully drawn but it is not an actor, and this method is not correct.
Help please.
SpiderHunt.java
package com.spiderhunt;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.spiderhunt.screens.MainMenu;
public class SpiderHunt extends Game{
private SpriteBatch batch;
public MainMenu mainMenu;
private static SpiderHunt instance = new SpiderHunt();
public SpiderHunt(){
}
public static SpiderHunt getInstance() {
return instance;
}
public void create () {
//load textures
Assets.load();
batch = new SpriteBatch();
mainMenu = new MainMenu(batch);
this.setScreen(mainMenu);
}
public void showMainMenu(){
setScreen(mainMenu);
}
public void render (float delta) {
}
public void resize(int width, int height) {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
}
}
MainMenu.java
package com.spiderhunt.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.spiderhunt.Assets;
import com.spiderhunt.buttons.btnPlay;
public class MainMenu implements Screen {
public btnPlay playButton;
public Stage stage;
public SpriteBatch batch;
class GoToGameListener extends ClickListener {
#Override
public void clicked(InputEvent event, float x, float y) {
//some code for click or push
}
}
public MainMenu(SpriteBatch batch_1) {
batch = batch_1;
stage = new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);
Gdx.input.setInputProcessor(stage);
playButton = new btnPlay(); //make actor
stage.addActor(playButton);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//make background
batch.begin();
batch.draw(Assets.bgMenuRegion, 0, 0, 540, 960);
batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw(); //this action must do method draw() from actor but it not called !!!!!!!!!!
//batch.begin();
//playButton.draw(batch, 0); THIS CODE DRAW BUTTON, BUT IT NOT CORRECTLY ACTOR
//batch.end();
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
#Override
public void hide() {
// 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
}
}
btnPlay.java
package com.spiderhunt.buttons;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.spiderhunt.Assets;
public class btnPlay extends Actor {
public btnPlay(){
setSize(100, 40);
setPosition(100, 100);
}
public void draw(SpriteBatch batch, float parentAlpha) {
//!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
batch.setColor(getColor());
batch.draw(Assets.btnPlayRegion, 0, 0);
}
}
Assets.java
package com.spiderhunt;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class Assets {
public static Texture atlas;
//backgrounds
public static TextureRegion bgMenuRegion;
public static TextureRegion bgSelectLevelRegion;
//buttons
public static TextureRegion btnPlayRegion;
//objects
public static TextureRegion objFlyRegion;
public static void load(){
atlas = new Texture("atlas.png");
bgMenuRegion = new TextureRegion(atlas, 0, 0, 540, 960);
btnPlayRegion = new TextureRegion(atlas, 1111, 1244, 418, 112);
}
}
Thanks for the help.
And now i find my bug.
I replace SpriteBatch to Batch and it work.
change
public void draw(SpriteBatch batch, float parentAlpha) {
//!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
batch.setColor(getColor());
batch.draw(Assets.btnPlayRegion, 0, 0);
}
to
#Override
public void draw(Batch batch, float parentAlpha) {
//!!!!!!!!!!!!!!!Error in this place. Draw() not called from stage
batch.setColor(getColor());
batch.draw(Assets.btnPlayRegion, 0, 0);
}
The problem I believe is at this line:
new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), batch);
Remove batch from the Stage Initialization change it to this:
new Stage(new StretchViewport( Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
You are passing the batch from the Screen to the stage and you are calling batch.end() before stage.draw(). However stage has its own batch so you do not have to pass a batch for the stage to draw. If for your own reasons (e.g. batch projection matrix configuration) you still want your Screen batch passed to the stage do not call batch.end() before stage.draw() since your batch of the stage and your main batch are the same. Call batch.end() after stage.draw()

Sprite flicker while moving libgdx

This code makes image Border flicker(Flash) while moving left,right,down or up. Why image border flash wile moving even if I use Screen class render() method delta value.
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
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;
import com.badlogic.gdx.math.Vector3;
public class MoveSpriteExample extends GdxTest implements InputProcessor {
Texture texture;
SpriteBatch batch;
OrthographicCamera camera;
Vector3 spritePosition = new Vector3();
Sprite sprite;
public void resize (int width, int height) {
}
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
batch = new SpriteBatch();
camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(false, w, h);
texture = new Texture(Gdx.files.internal("data/grasswall.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
sprite = new Sprite(texture);
sprite.setSize(32, 32);
spritePosition.y=100;
sprite.setPosition(spritePosition.x,spritePosition.x);
lastUpdateTime = System.nanoTime();
}
public void Update(float Delta)
{
if (Gdx.input.isKeyPressed(Keys.D)==true)
{
spritePosition.x += 150*(Delta / 1000000000.0);
}else if (Gdx.input.isKeyPressed( Keys.A)==true)
{
spritePosition.x -= 150*(Delta / 1000000000.0);
}
else if (Gdx.input.isKeyPressed( Keys.Z)==true)
{
spritePosition.y -= 150*(Delta / 1000000000.0);
}
else if (Gdx.input.isKeyPressed( Keys.W)==true)
{
spritePosition.y += 150*(Delta / 1000000000.0);
}
}
float lastUpdateTime;
float currentTime;
public void render() {
currentTime = System.nanoTime();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
Update(currentTime - lastUpdateTime);
sprite.setPosition(spritePosition.x,spritePosition.y);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
lastUpdateTime = currentTime;
}
}
pls provide code with sample
As i said in the last post of you, you should take a look at the Tutorial!
First of all you do not need to calculate the delta time yourself. I already postet how you get it in the last post for you.
Ill give you a super short example with a moving sprite without flickering.
At first here is the ApplicationListener. (not a GDXtest)
public class MainClass implements ApplicationListener {
private Screen currentScreen = null;
#Override
public void create() {
Texture.setEnforcePotImages(false);
this.currentScreen = new TestScreen();
}
#Override
public void dispose() {
this.currentScreen.dispose();
}
#Override
public void render() {
this.currentScreen.render(Gdx.graphics.getDeltaTime());
}
#Override
public void resize(int width, int height) {
this.currentScreen.resize(width, height);
}
#Override
public void pause() {
this.currentScreen.pause();
}
#Override
public void resume() {
this.currentScreen.resume();
;
}
}
It's preaty simple and as you can see it does call the render of the screen automatically with the delta time! this.currentScreen.render(Gdx.graphics.getDeltaTime()).
The next thing you need is a simple Screen. So something that does implement the Screeninterface. Id really recommand that you use a Scene2D setup with a stage. But here is an example without.
public class TestScreen implements Screen {
private Sprite mySprite;
private SpriteBatch batch = new SpriteBatch();
public TestScreen() {
this.mySprite = new Sprite(new Texture(
Gdx.files.internal("data/appicon.png")));
this.mySprite.setPosition(50f, 50f);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
this.update(delta);
this.batch.begin();
this.mySprite.draw(batch);
this.batch.end();
}
public void update(float delta) {
if (Gdx.input.isKeyPressed(Keys.D) == true) {
mySprite.setX(mySprite.getX() + 150 * delta);
} else if (Gdx.input.isKeyPressed(Keys.A) == true) {
mySprite.setX(mySprite.getX() - 150 * delta);
} else if (Gdx.input.isKeyPressed(Keys.Z) == true) {
mySprite.setY(mySprite.getY() - 150 * delta);
} else if (Gdx.input.isKeyPressed(Keys.W) == true) {
mySprite.setY(mySprite.getY() + 150 * delta);
}
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
Regardas and do take a look at the tutorial. (Here is again the beginning of it)

Categories

Resources