MenuScreen problem with loading assets

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

MenuScreen problem with loading assets

Postby Haso94 » Thu May 16, 2019 4:54 pm

Hi,

It's me again :D currently I am sitting on the MenuScreen but can not get it to show. I am getting an error with my assetmanager that the assets are not loaded eventhough I call the method finishloading from assetmanager.


Code: Select all
package de.paluno.game.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;

import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import de.paluno.game.SEPGame;
import de.paluno.game.de.paluno.game.assetTools.Assetdescriptor;

public class MenuScreen implements Screen {

   public SEPGame game;
   public OrthographicCamera menuCamera;
   public Viewport menuPort;
   public Texture menuBackground;
   public SpriteBatch menuBatch;
   public Sprite menuSprite;
   public AssetManager assetManager;



   public MenuScreen(SEPGame game) {
      game = new SEPGame();
      assetManager = new AssetManager();
      menuBackground = assetManager.get(Assetdescriptor.backGround);
   }



   public void show() {

         //assetManager.load(Assetdescriptor.font);
         assetManager.load(Assetdescriptor.backGround);
         assetManager.load(Assetdescriptor.buttonNotePressed);
         assetManager.load(Assetdescriptor.buttonPressed);
         assetManager.finishLoading();

   }




   public void setUpMenu(){



   }



   @Override
   public void render(float delta) {
      Gdx.gl.glClearColor(1, 0, 0, 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() {

   }

   public void hide() {

   }

   @Override
   public void dispose() {

   }
}




Code: Select all
package de.paluno.game.de.paluno.game.assetTools;

public class Assetpath {

    public static final String UI_FONT = "Menu/Kenny_Future.ttf";
    public static final String backGround= "Menu/backgroundDesert.png";
    public static final String buttonNotPressed = "Menu/PNG/blue_button00.png";
    public static final String buttonPressed = "Menu/PNG/blue_button01.png";






    public Assetpath() {
    }
}





Code: Select all
package de.paluno.game.de.paluno.game.assetTools;

import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;

public class Assetdescriptor {

    public static AssetDescriptor<BitmapFont> font =
            new AssetDescriptor<BitmapFont>(Assetpath.UI_FONT, BitmapFont.class);
    public static AssetDescriptor<Texture> backGround =
            new AssetDescriptor<Texture>(Assetpath.backGround, Texture.class);
    public static AssetDescriptor<Texture> buttonNotePressed =
            new AssetDescriptor<Texture>(Assetpath.buttonNotPressed, Texture.class);
    public static AssetDescriptor<Texture> buttonPressed =
            new AssetDescriptor<Texture>(Assetpath.buttonPressed, Texture.class);


    public Assetdescriptor() {
    }
}




this is my code and this is my error: Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: Menu/backgroundDesert.png


I tried to call the show() method in the construktor so the assets are loaded but then I get nullpointerexception. Can someone point me maybe my mistake?
Haso94
 
Posts: 24
Joined: Mon Apr 22, 2019 9:11 pm

Re: MenuScreen problem with loading assets

Postby Haso94 » Thu May 16, 2019 7:19 pm

So I kind of solved my error it was pretty simple, but I can not load my asset on to the screen it only shows the red color from libgdx. I think it is a pretty simple mistake but I am just overlooking it :?: maybe someone can give me a pointer as to what I have to do.

Menu:
Code: Select all
public class MenuScreen implements Screen {

    public SEPGame game;
    public OrthographicCamera menuCamera;
    public Viewport menuPort;
    public Texture menuBackground;
    public SpriteBatch menuBatch;
    public Sprite menuSprite;
    public AssetManager assetManager;
    public Viewport viewport;
    public Stage stage;


    public MenuScreen(SEPGame game) {
        game = new SEPGame();
        assetManager = new AssetManager();
    }


    public void show() {
        viewport = new FitViewport(SEPGame.WORLD_PIXEL_WIDTH, SEPGame.WORLD_PIXEL_HEIGHT);
        stage = new Stage(viewport);

            //assetManager.load(Assetdescriptor.font);
            assetManager.load(Assetdescriptor.backGround);
            assetManager.load(Assetdescriptor.buttonNotePressed);
            assetManager.load(Assetdescriptor.buttonPressed);
            assetManager.finishLoading();
        initUI();


    }


    public void initUI(){

        Table table = new Table();

        TextureAtlas backGround = assetManager.get(Assetdescriptor.backGround);


        TextureAtlas buttonNotPressed = assetManager.get(Assetdescriptor.buttonNotePressed);
        TextureAtlas buttonPressed = assetManager.get(Assetdescriptor.buttonPressed);

        TextureRegion backGroundRegion = backGround.findRegion(Regionnames.backGround);
        TextureRegion buttonNotPresseds =buttonNotPressed.findRegion(Regionnames.buttonNotPressed);
        TextureRegion buttonPresseds =buttonPressed.findRegion(Regionnames.buttonPressed);


        // SEtup Table

        table.center();
        table.setFillParent(true);
        table.pack();

        table.setBackground(new TextureRegionDrawable(backGroundRegion));
        stage.addActor(table);


    }

    public void setUpMenu() {


    }


    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        show();



    }


AssetDescriptor:
Code: Select all
public class Assetdescriptor {

    public static AssetDescriptor<BitmapFont> font =
            new AssetDescriptor<BitmapFont>(Assetpath.UI_FONT, BitmapFont.class);

    public static AssetDescriptor<TextureAtlas> backGround =
            new AssetDescriptor<TextureAtlas>(Assetpath.backGround, TextureAtlas.class);
    public static AssetDescriptor<TextureAtlas> buttonNotePressed =
            new AssetDescriptor<TextureAtlas>(Assetpath.buttonNotPressed, TextureAtlas.class);
    public static AssetDescriptor<TextureAtlas> buttonPressed =
            new AssetDescriptor<TextureAtlas>(Assetpath.buttonPressed, TextureAtlas.class);


    public Assetdescriptor() {
    }
}




Assetpath:
Code: Select all
package de.paluno.game.de.paluno.game.assetTools;

public class Assetpath {

    public static final String UI_FONT = "Menu/Kenney_Future.ttf";
    public static final String backGround= "Menu/MenuUi.atlas";
    public static final String buttonNotPressed = "Menu/MenuUi.atlas";
    public static final String buttonPressed = "Menu/MenuUi.atlas";
   
    public Assetpath() {
    }
}




Regionnames:

Code: Select all
package de.paluno.game.de.paluno.game.assetTools;

public class Regionnames {

    public static final String backGround = "backgroundDesert";
    public static final String buttonNotPressed = "blue_button00";
    public static final String buttonPressed = "blue_button01";
    public static final String Highscore = "check-mark";
    public static final String quit = "easy";

    private Regionnames() {
    }

}

Haso94
 
Posts: 24
Joined: Mon Apr 22, 2019 9:11 pm

Re: MenuScreen problem with loading assets

Postby Haso94 » Thu May 16, 2019 9:39 pm

sorry to spam so much, but I kinda solved it :D just have to write into the imagebutton if that is possible, but we will see
Haso94
 
Posts: 24
Joined: Mon Apr 22, 2019 9:11 pm

Re: MenuScreen problem with loading assets

Postby evilentity » Fri May 17, 2019 11:17 am

You should not be calling show() from render(). You need to call act() and draw() on stage
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4851
Joined: Wed Aug 24, 2011 11:37 am

Re: MenuScreen problem with loading assets

Postby Haso94 » Mon May 20, 2019 11:48 am

Hey Thanks for the answer sorry for the late reply. I solved it thanks :). But not I have a funny bug, when I switch screens (From Menu to Playscreen) themenu is not shown but is still working in progress, when I click on my touchpad where normaly the button is placed on my screen, eventhough I am in Playscreen its clicking the button. I tried disposing it but it didnt work, maybe someone here can help me? I think it has something to do with dispose, I have to dispose the stage with I am drawing I think, but it didnt work out :/


Code: Select all
package de.paluno.game.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
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.*;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;

import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import de.paluno.game.SEPGame;
import de.paluno.game.de.paluno.game.assetTools.Assetdescriptor;
import de.paluno.game.de.paluno.game.assetTools.Regionnames;

public class MenuScreen implements Screen {

    public SEPGame game;
    public SpriteBatch menuBatch;
    public AssetManager assetManager;
    public Viewport viewport;
    public Stage stage;
    public Stage titleStage;


    public MenuScreen(SEPGame game) {
        this.game = game;
        assetManager = new AssetManager();
        menuBatch = new SpriteBatch();

        viewport = new FitViewport(SEPGame.WORLD_PIXEL_WIDTH, SEPGame.WORLD_PIXEL_HEIGHT);
        stage = new Stage(viewport, menuBatch);
        titleStage = new Stage(viewport, menuBatch);
        Gdx.input.setInputProcessor(stage);

        assetManager.load(Assetdescriptor.font);
        assetManager.load(Assetdescriptor.backGround);
        assetManager.load(Assetdescriptor.buttonNotePressed);
        assetManager.load(Assetdescriptor.buttonPressed);
        assetManager.load(Assetdescriptor.skinUI);
        assetManager.finishLoading();
        initUI();


    }

    public void play() {

        System.out.println(game);

        game.setScreen(new Playscreen(game));
    }


    public void show() {


    }


    public void initUI() {

        Table table = new Table();

        TextureAtlas menuUi = assetManager.get(Assetdescriptor.backGround);
        Skin menuSkin = assetManager.get(Assetdescriptor.skinUI);
        // Background image
        TextureRegion backGroundRegion = menuUi.findRegion(Regionnames.backGround);

        //Button
        //Buttons

        TextButton CoOpButton = new TextButton("COOP MODE", menuSkin);
        CoOpButton.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                play();
            }
        });
        stage.dispose();

        TextButton survivalMode = new TextButton("SURVIVAL MODE", menuSkin);
        survivalMode.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                play();
            }
        });


        TextButton options = new TextButton("EINSTELLUNGEN", menuSkin);
        options.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                play();
            }
        });

        TextButton highScore = new TextButton("HIGHSCORE TABELLE", menuSkin);
        highScore.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                play();
            }
        });
        TextButton quit = new TextButton("BEENDEN", menuSkin);
        quit.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                Gdx.app.exit();
            }
        });

        Table buttonTable = new Table(menuSkin);
        buttonTable.defaults().pad(5);

        buttonTable.add(CoOpButton).row();
        buttonTable.add(survivalMode).row();
        buttonTable.add(options).row();
        buttonTable.add(highScore).row();
        buttonTable.add(quit).row();
        buttonTable.center().row();


        // SEtup Table
        table.add(buttonTable);
        table.center();
        table.setFillParent(true);
        table.pack();
        table.setBackground(new TextureRegionDrawable(backGroundRegion));
        stage.addActor(table);


    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.draw();


    }

    @Override
    public void resize(int width, int height) {

    }


    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    public void hide() {

    }

    @Override
    public void dispose() {
        stage.dispose();


    }


}




Code: Select all
package de.paluno.game.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;

import de.paluno.game.SEPGame;
import de.paluno.game.gameobjects.Flower;
import de.paluno.game.gameobjects.Tank;
import de.paluno.game.gameobjects.Target;
import de.paluno.game.gameobjects.WorldMap;

public class Playscreen extends WorldMap implements Screen {

   public World world;
   public SpriteBatch batch;
   public float timeToSimulate;
   private SEPGame Maingame;
   SpriteBatch sb;
   public Tank tank;
   public Target ziel;   
   
   public float width = Gdx.graphics.getWidth();
   public float heights = Gdx.graphics.getHeight();
   public WorldMap worldMap;
   
   public TextureAtlas atlas;

   public Playscreen(SEPGame Maingame) {
      ziel = new Target(MathUtils.random(Gdx.graphics.getWidth()-48),MathUtils.random(Gdx.graphics.getHeight()-48),world);
      tank = new Tank(world,this);
      atlas = new TextureAtlas("TanksGesamt.atlas");
      
      worldMap = new WorldMap();
      this.Maingame = Maingame;
   }   
   public void show() {

   }

   public void create() {

   }
   public void update(float fval) {
      world.step(1 / 60f, 6, 2);

      mapRenderer.setView(mainCamera);
      
   }
   
   public void render(float delta) {
      Gdx.gl.glClearColor(1, 0, 0, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      
      worldMap.render();
      worldMap.mainCamera.update();
      
        tank.render();
        tank.moveSprite();
        tank.ControllerInput();
      
        ziel.render();
      
       
        collision();
        if (collision()) {
           ziel = new Target(MathUtils.random(Gdx.graphics.getWidth()-48),MathUtils.random(Gdx.graphics.getHeight()-48),world);
        }
     }
   public boolean collision() {
      boolean col = false;
      Rectangle rectangle2 = ziel.bounds();
      for(Flower f: tank.flowers) {
         
         if(f.getRec().overlaps(rectangle2)){
            col= true;
         }else {
             col= false;
         }
      }
      return col;
   }

   public void resize() {
      worldMap.gamePort.update(SEPGame.WORLD_PIXEL_WIDTH, SEPGame.WORLD_PIXEL_HEIGHT);

   }


   public World getworld() {
      return world;
   }

   public void physicsPhase(float delta) {

   }

   public void renderPhase(float delta) {

   }

   public void updatePhase(float delta) {

   }

   public void registerAfterUpdate(Object gameObject) {

   }

   private void register(Object gameObject) {

   }

   public void forgetAfterUpdate(Object gameObject) {

   }

   protected void forget(Object gameObject) {

   }

   @Override
   public void resize(int width, int height) {
      // 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

   }
   @Override
   public void hide() {
      // TODO Auto-generated method stub
      
   }

}



Here are my two classes, hopefully someone give me a pointer
Haso94
 
Posts: 24
Joined: Mon Apr 22, 2019 9:11 pm

Re: MenuScreen problem with loading assets

Postby evilentity » Mon May 20, 2019 2:58 pm

The stage is still set as input processor in PlayScreen. It doesnt magically go away when you change the screen.
Looking for a freelancer? PM me!
Check out libgdx discord server!
evilentity
 
Posts: 4851
Joined: Wed Aug 24, 2011 11:37 am

Re: MenuScreen problem with loading assets

Postby Haso94 » Wed May 22, 2019 12:42 am

Hey thanks, that was it
Haso94
 
Posts: 24
Joined: Mon Apr 22, 2019 9:11 pm


Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest