Sprite flicker while moving libgdx - java

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)

Related

Player doesn't move when attempting to draw from a Player class

So I'm currently in the process of refactoring my code in my game for the player and started a player class:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Player
{
protected String name;
protected float health, maxHealth;
protected Texture texture;
protected int xPos, yPos;
public Player(String name, float maxHealth, Texture texture) {
this(name, maxHealth, texture, 0, 0);
}
public Player(String name, float maxHealth, Texture texture, int xPos, int yPos) {
this.name = name;
this.health = this.maxHealth = maxHealth;
this.texture = texture;
this.xPos = xPos;
this.yPos = yPos;
}
public Texture getTexture() {
return this.texture;
}
public int getX() {
return xPos;
}
public int getY() {
return yPos;
}
public String getName() {
return name;
}
public void draw(SpriteBatch spriteBatch) {
spriteBatch.begin();
spriteBatch.draw(texture, xPos, yPos, 0.5f, 0.5f, 0, 0,
texture.getWidth(), texture.getHeight(), false, false);
spriteBatch.end();
}
public void update(float delta) {
processMovement(delta);
}
public void processMovement(float delta) {
if(Gdx.input.isKeyPressed(Input.Keys.A)) {
xPos -= 50 * delta;
}
if(Gdx.input.isKeyPressed(Input.Keys.D)) {
xPos += 50 * delta;
}
}
}
I'm using an orthographic camera, I haven't added any terrain yet as I'm going to do that next, however I want the player to always stay in the center but have the player move around the terrain when I draw it.
The code I have for creating the camera and drawing my player is as follows:
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
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 GameState implements Screen
{
private PixelGame parent;
private OrthographicCamera camera;
private Player player;
private Texture playerTexture;
private SpriteBatch spriteBatch;
public GameState(PixelGame parent) {
this.parent = parent;
this.playerTexture = new Texture(Gdx.files.internal("player/sprite.png"));
this.player = new Player("Me", 20, playerTexture);
this.spriteBatch = new SpriteBatch();
}
#Override
public void render(float delta) {
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
player.draw(spriteBatch);
player.update(delta);
}
#Override
public void show() {
}
#Override
public void dispose() {
}
#Override
public void resize(int width, int height) {
float ratio = (float)width / (float)height;
camera = new OrthographicCamera(2f * ratio, 2f);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
}
The player sprite doesn't seem to move, but when i change the values to something >75 the player sprite sprints across the screen like no-ones business.
xPos and yPos in your class are ints. I would say that's the problem. Your processMovement() method is called like 100 times per second and 50 * delta is most likely smaller than 1 so it's rounded to 0 because value has to be stored in int variable. Try changing xPos and yPos to floats.
And if that doesn't help (can't be sure without trying out the code) do some debugging. Put break points. See if processMovement() is called at all and if it is then what value variable delta has. How calculation goes.

LibGDX Fade Out / Fade In Screen Transition

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

How to show loading screen class before preferences [Libgdx & android]

So I am programming a game, and implemented preferences to save/load some simple data for sound/high score, nothing too much. The problem is that when I load preferences for the first time on a physical phone (samsung g. s3) it takes about 15sec and on s5 it takes about 5-8secs and in that time it shows black screen even though I have set the loading screen to be shown first, before preferences are used.
In my main application code I set screen to loadingScreen
loadingScreen = new com.package.game.Screens.LoadingScreen(this);
mainScreen = new com.package.game.Screens.MainScreen(this);
gameScreen = new com.package.game.Screens.GameScreen(this);
settingsScreen = new com.package.game.Screens.SettingsScreen(this);
this.setScreen(loadingScreen);
and then in my loading screen I access preferences:
#Override
public void show() {
this.progress_assets = 0f;
this.progress_assets = 0f;
font_loading = app.initFont(50, 1, 255, 255, 255, 1);
queueAssets();
loading_db();
}
private void loading_db() {
if(database.get_first_time()){
//do some introduction for first time run
Gdx.app.log("db","first time worked");
database.set_first_time();
progress_db=1f;
}else{
Gdx.app.log("db","first time is set false");
progress_db=1f;
}
}
and my preferences class:
package com.package.game.Engine;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.package.game.Application;
public class Database {
private Preferences preferences;
//database
private Preferences pref_settings;
private Preferences pref_score;
private Preferences pref_unlocks;
public Database(){
pref_settings = Gdx.app.getPreferences("com.package.game.settings");
pref_score = Gdx.app.getPreferences("com.package.game.score");
pref_unlocks = Gdx.app.getPreferences("com.package.game.unlocks");
}
//checking if first time run
public boolean get_first_time(){
return pref_settings.getBoolean("First_run",true);
}
public void set_first_time(){
pref_settings.putBoolean("First_run",false);
for(int i=0;i<7;i++){
pref_unlocks.putBoolean("unlock_"+i,true);
}
pref_unlocks.flush();
pref_settings.flush();
}
//settings
public boolean getSound(){
return pref_settings.getBoolean("sound_on",true);
}
public void setSound(boolean sound){
pref_settings.putBoolean("sound_on",sound);
pref_settings.flush();
}
//High Score Mode:
//Classic
public int getScore_classic(int place){
return pref_score.getInteger("score_classic_"+place,0);//default value 0 so we could place new score if we havent reached it
}
public void setScore_classic(int place,int scored){
pref_score.putInteger("score_classic_"+place,scored);
pref_score.flush();
}
//Recipe
public int getScoreRec(int place){
return pref_score.getInteger("score_recipe_"+place,0);//default value 0 so we could place new score if we havent reached it
}
public void setScoreRec(int place,int scored){
pref_score.putInteger("score_recipe_"+place,scored);
pref_score.flush();
}
//food unlocks
public boolean getFoodUnlock(int unlockID){
return pref_unlocks.getBoolean("unlock_"+unlockID,false);
}
public void setFoodUnlock(int unlockID,boolean state){
pref_unlocks.putBoolean("unlock_"+unlockID,state);
pref_unlocks.flush();
}
}
I'm not sure if creating 3 pref. files is good, but i would like to know how to show screen before pref. file being created/loaded.
Edit: Adding my main code
package com.mindutis.game;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
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.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.mindutis.game.Screens.LoadingScreen;
public class Application extends Game {
public static int V_WIDTH ;
public static int V_HEIGHT;
public OrthographicCamera camera;
public SpriteBatch batch;
public AssetManager assets;
public com.mindutis.game.Screens.LoadingScreen loadingScreen;
public com.mindutis.game.Screens.SplashScreen splashScreen;
public com.mindutis.game.Screens.MainScreen mainScreen;
public com.mindutis.game.Screens.GameScreen gameScreen;
public com.mindutis.game.Screens.SettingsScreen settingsScreen;
public BitmapFont f_game_name;
public String t_game_name;
public String[] font_type = new String[2];
private boolean firstFrame = true;
private boolean loading = true;
#Override
public void create() {
assets = new AssetManager();
camera = new OrthographicCamera();
V_WIDTH= Gdx.graphics.getWidth();
V_HEIGHT = Gdx.graphics.getHeight();
camera.setToOrtho(false, V_WIDTH, V_HEIGHT);
batch = new SpriteBatch();
//Global var.
//font initializer
font_type[0] = "fonts/comics_bold.ttf";
font_type[1] = "fonts/vdj.ttf";
//game name
t_game_name = " Catch a\nSandwich";
f_game_name = initFont(70, 0, 255, 255, 255, 1);
loadingScreen = new com.mindutis.game.Screens.LoadingScreen(this);
splashScreen = new com.mindutis.game.Screens.SplashScreen(this);
mainScreen = new com.mindutis.game.Screens.MainScreen(this);
gameScreen = new com.mindutis.game.Screens.GameScreen(this);
settingsScreen = new com.mindutis.game.Screens.SettingsScreen(this);
// this.setScreen(loadingScreen);
}
//int size=font size, int x = font type
public BitmapFont initFont(int size, int type, float red, float green, float blue, float alpha) {
BitmapFont font;
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal(font_type[type]));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = size;
font = generator.generateFont(parameter);
font.setColor(red / 255f, green / 255f, blue / 255f, alpha);
return font;
}
#Override
public void render() {
if (firstFrame){
Gdx.gl.glClearColor(.2f, .67f, .88f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
f_game_name.draw(batch, t_game_name, camera.viewportWidth / 2 - 150, camera.viewportHeight - 100);
batch.end();
firstFrame = false;
} else {
if(loading){
loading=false;
loadEverything();
}
// Notice you don't actually render anything so what you previously drew stays on the screen.
}
super.render();
}
private void loadEverything(){
// load your font, assets, prefs, etc.
this.setScreen(loadingScreen);
// setScreen(loadingScreen);
}
#Override
public void dispose() {
batch.dispose();
assets.dispose();
f_game_name.dispose();
loadingScreen.dispose();
splashScreen.dispose();
mainScreen.dispose();
gameScreen.dispose();
settingsScreen.dispose();
}
}
Loading screen class:
package com.mindutis.game.Screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.mindutis.game.Application;
import com.mindutis.game.Engine.Database;
public class LoadingScreen implements Screen {
private final Application app;
private Database database;
private BitmapFont font_loading;
private boolean load;
private ShapeRenderer shapeRenderer;
private Stage stage;
private String t_loading;
private float progress_assets,progress_db;
private boolean firstFrame = true;
private boolean loading = true;
public LoadingScreen(Application app) {
this.app = app;
database = new Database();
this.stage = new Stage(new FitViewport(com.mindutis.game.Application.V_WIDTH, com.mindutis.game.Application.V_HEIGHT, app.camera));
this.shapeRenderer = new ShapeRenderer();
t_loading = "Loading...";
load=false;
}
private void queueAssets() {
//good food
//bread
app.assets.load("img/Food/breadtop.png", Texture.class);
app.assets.load("img/Food/breadbot.png", Texture.class);
//rest of the food
app.assets.load("img/Food/food_sheet.png", Texture.class);
//misc
app.assets.load("img/Misc/coin.png", Texture.class);
app.assets.load("img/Misc/soon.png", Texture.class);
app.assets.load("img/Misc/shoptriangle.png", Texture.class);
//buttons
app.assets.load("img/Buttons/button.png", Texture.class);
app.assets.load("img/Buttons/soundBT.png", Texture.class);
app.assets.load("img/Buttons/btX.png", Texture.class);
app.assets.load("img/Buttons/btShop1.png", Texture.class);
//human
app.assets.load("img/Human/human.png", Texture.class);
app.assets.load("img/Human/human1.png", Texture.class);
app.assets.load("img/Human/human2.png", Texture.class);
}
#Override
public void show() {
this.progress_assets = 0f;
this.progress_assets = 0f;
font_loading = app.initFont(50, 1, 255, 255, 255, 1);
queueAssets();
loading_db();
}
private void loading_db() {
if(database.get_first_time()){
//do some introduction for first time run
Gdx.app.log("db","first time worked");
database.set_first_time();
progress_db=1f;
}else{
Gdx.app.log("db","first time is set false");
progress_db=1f;
}
}
#Override
public void render(float delta) {
if (firstFrame){
Gdx.gl.glClearColor(.2f, .67f, .88f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
app.batch.begin();
app.f_game_name.draw(app.batch, app.t_game_name, app.camera.viewportWidth / 2 - 150, app.camera.viewportHeight - 100);
app.batch.end();
firstFrame = false;
} else {
if(loading){
loading=false;}
//input
Gdx.input.setCatchBackKey(true);
Gdx.input.setInputProcessor(new InputAdapter() {
#Override
public boolean keyDown(int keycode) {
if (keycode == Input.Keys.BACK) {
// Do nothing
}
return false;
}
});
Gdx.gl.glClearColor(.2f, .67f, .88f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
update(delta);
app.batch.begin();
font_loading.draw(app.batch, t_loading, app.camera.viewportWidth / 2 - 150, 123);
app.f_game_name.draw(app.batch, app.t_game_name, app.camera.viewportWidth / 2 - 150, app.camera.viewportHeight - 100);
app.batch.end();
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.WHITE);
shapeRenderer.rect(32, 50, app.camera.viewportWidth - 64, 16);
shapeRenderer.setColor(Color.GREEN);
shapeRenderer.rect(32, 50, (progress_assets+progress_db)/2 * (app.camera.viewportWidth - 64), 16);
shapeRenderer.end();
}
// Notice you don't actually render anything so what you previously drew stays on the screen.
}
private void update(float delta) {
progress_assets = MathUtils.lerp(progress_assets, app.assets.getProgress(), .1f);
if (app.assets.update() && (progress_assets+progress_db)/2 >= app.assets.getProgress() - .01f) {
app.setScreen(app.mainScreen);
}
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, false);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
shapeRenderer.dispose();
font_loading.dispose();
stage.dispose();
}
}
You can quickly load a single texture to show while everything else loads. Load and show the texture the first time render() is called. Then the second time render() is called, load your stuff, dispose the texture, and switch screens.
public class LoadingScreen extends Screen {
private boolean firstFrame = true;
private Texture texture;
private ExtendViewport viewport;
private MyGame game;
private SpriteBatch batch;
private static final float LOAD_IMAGE_WIDTH = 480, LOAD_IMAGE_HEIGHT = 600;
// Use whatever the loading image dimensions are.
public LoadingScreen (MyGame game, SpriteBatch batch){
this.game = game;
this.batch = batch;
viewport = new ExtendViewport(LOAD_IMAGE_WIDTH, LOAD_IMAGE_HEIGHT);
}
public void resize (int width, int height) {
viewport.update(width, height, false);
viewport.getCamera().position.set(LOAD_IMAGE_WIDTH / 2, LOAD_IMAGE_HEIGHT / 2, 0);
viewport.getCamera().update();
}
public void show (){} // do nothing
public void render (float delta) {
if (firstFrame){
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
viewport.apply();
batch.setProjectionMatrix(viewport.getCamera().combined);
batch.setBlendingDisabled(true);
batch.begin();
batch.draw(texture, 0, 0, LOAD_IMAGE_WIDTH, LOAD_IMAGE_HEIGHT);
batch.end();
firstFrame = false;
} else {
loadEverything();
// Notice you don't actually render anything so what you previously drew stays on the screen.
}
}
private void loadEverything(){
// load your font, assets, prefs, etc.
texture.dispose();
main.setGameScreen();
}
}

How to draw a Sprite in Libgdx/Android?

im working on a libgdx Game where you avoid Asteroids with a spaceship. When i launch my project i only see a white Screen :( Here u can see a big part of my code to understand what iam trying to do.
Main Class:
package com.me.mygdxgame;
import screen.MenuScreen;
import screen.ScreenManager;
public class MyGdxGame implements ApplicationListener {
SpriteBatch batch;
public static int WIDTH = 800 , HEIGHT = 480; // resolution
#Override
public void create() {
batch = new SpriteBatch();
ScreenManager.setScreen(new MenuScreen());
}
#Override
public void dispose() {
if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().dispose();
}
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().update();
}
if(ScreenManager.getCurrentScreen() != null){
ScreenManager.getCurrentScreen().render(batch);
}
}
Screen class:
package screen;
public abstract class Screen {
public abstract void create();
public abstract void render(SpriteBatch batch);
public abstract void update();
public abstract void resize(int width, int height);
public abstract void dispose(int width, int height);
public abstract void dispose();
public abstract void pause();
public abstract void resume();
}
ScreenManager:
public class ScreenManager {
private static Screen currentScreen;
public static void setScreen(Screen screen){
if(currentScreen != null){
currentScreen.dispose();
currentScreen = screen;
currentScreen.create();
}
}
public static Screen getCurrentScreen() {
return currentScreen;
}
}
MenuScreen:
public class MenuScreen extends Screen {
private OrthoCamera cam;
private Spaceship spaceship;
#Override
public void create() {
// TODO Auto-generated method stub
cam = new OrthoCamera();
spaceship = new Spaceship();
}
#Override
public void render(SpriteBatch batch) {
// TODO Auto-generated method stub
batch.setProjectionMatrix(cam.combined);
batch.begin();
spaceship.render(batch);
batch.end();
}
#Override
public void update() {
// TODO Auto-generated method stub
cam.update();
spaceship.update();
}
Spaceship Class:
public class Spaceship extends Entity {
Texture texture;
Sprite sprite;
public Spaceship() {
texture = new Texture("spritesheet.png");
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear );
TextureRegion region = new TextureRegion(texture, 0, 312, 258, 144);
sprite = new Sprite(region);
sprite.setSize(sprite.getWidth(), sprite.getHeight());
sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);
sprite.setPosition(-sprite.getWidth() / 2, -sprite.getHeight() / 2);
}
public void update() {
if (Gdx.input.isTouched()) {
pos.x = Gdx.input.getX() - MyGdxGame.WIDTH / 2;
pos.y = -Gdx.input.getY() + MyGdxGame.HEIGHT / 2;
}
}
#Override
public void render(SpriteBatch batch) {
sprite.draw(batch);
}
The problem should be in your ScreenManager. The method setScreen is incapable of initializing a screen if there is no previous screen:
public static void setScreen(Screen screen){
if(currentScreen != null){
currentScreen.dispose();
currentScreen = screen;
currentScreen.create();
}
}
Should be:
public static void setScreen(Screen screen) {
if (currentScreen != null){
currentScreen.dispose();
}
currentScreen = screen;
currentScreen.create();
}

Libgdx NullPointerException when trying to update viewport

I'm trying to update the game's viewport when the game resizes, but when I start the game, I get a java.lang.NullPointerException:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.utils.viewport.Viewport.apply(Viewport.java:49)
at com.badlogic.gdx.utils.viewport.ExtendViewport.update(ExtendViewport.java:90)
at com.badlogic.gdx.utils.viewport.Viewport.update(Viewport.java:57)
at me.chrisjosten.testgame.screens.MainScreen.resize(MainScreen.java:82)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at me.chrisjosten.testgame.create(MainScreen.java:13)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
In the class, that implements Screen, I have in the resize method, the place where the exception occurs, the following code:
#Override
public void resize(int w, int h) {
viewport.update(w, h);
}
where the viewport is an ExtendViewport created in the constructor of the class. I've tried putting that in the show method to, but then I get the same result.
The full code of the class:
package me.chrisjosten.testgame.screens;
import me.chrisjosten.testgame.MyGame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class MainScreen implements Screen{
private MyGame game;
private BitmapFont font;
private SpriteBatch batch;
private OrthographicCamera camera;
private Viewport viewport;
private boolean goingUp = false;
private float alpha = 1;
private int gameWidth = 100;
private int gameHeight = 100;
private int screenWidth;
private int screenHeight;
public MainScreen(MyGame g) {
game = g;
System.out.println("screen created");
}
#Override
public void show() {
System.out.println("show");
font = new BitmapFont(Gdx.files.internal("fonts/monospace.fnt"));
font.setColor(1, 1, 1, 1);
batch = new SpriteBatch();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.translate(gameWidth / 2, gameHeight / 2);
viewport = new ExtendViewport(gameWidth, gameHeight, camera);
}
#Override
public void render(float delta) {
camera.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (goingUp) {
font.setColor(1, 1, 1, alpha);
alpha += 0.025;
} else {
font.setColor(1, 1, 1, alpha);
alpha -= 0.025;
}
if (alpha <= 0) {
goingUp = true;
alpha = 0;
} else if (alpha >= 1) {
goingUp = false;
alpha = 1;
}
batch.setProjectionMatrix(camera.combined);
batch.begin();
String press = "Press start";
font.draw(batch, press, gameWidth / 2 - font.getBounds(press).width / 2, 1);
batch.end();
}
#Override
public void resize(int w, int h) {
viewport.update(w, h);
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
font.dispose();
}
}
Does anyone know what I have done wrong?
By moving the code from the show method to the constructor, it works.

Categories

Resources