Results 1 to 6 of 6
  1. #1
    GameSoft is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default How can I add a background Sprite?

    Hi

    I need to draw a sprite before the map but it never works.

    My Code ( without the background sprite ) :

    Java Code:
    package com.TEST.Dermat.screens;
    
    import com.TEST.Dermat.entities.Player;
    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.Sprite;
    import com.badlogic.gdx.maps.tiled.TiledMap;
    import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
    import com.badlogic.gdx.maps.tiled.TmxMapLoader;
    import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
    
    public class Play implements Screen {
    
    	private TiledMap map;
    	public OrthogonalTiledMapRenderer renderer;
    	private OrthographicCamera camera;
    	
    	private Player player; 
    	
    	@Override
    	public void render(float delta) {
    
    		Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
    	    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    	    renderer.render();
    		
    	    camera.position.set(player.getX() + player.getWidth() / 2, player.getY() + player.getHeight() / 2, 0);
    		camera.update();
    		
    		renderer.setView(camera);
    		renderer.render();
    		
    		renderer.getSpriteBatch().begin();
    		player.draw(renderer.getSpriteBatch());
    		renderer.getSpriteBatch().end();
    		
    	}
    
    	@Override
    	public void resize(int width, int height) {
    		camera.viewportWidth = width / 0.75f;
    		camera.viewportHeight = height / 0.75f;
    		
    	}
    
    	@Override
    	public void show() {
    		
    		map = new TmxMapLoader().load("maps/map.tmx");
    		
    		//TmxMapLoader loader = new TmxMapLoader(); 
    		//map = loader.load("maps/map.tmx");
    		//map = new TmxMapLoader().load("maps/map.tmx");
    
    		renderer = new OrthogonalTiledMapRenderer(map);
    		
    		camera = new OrthographicCamera();
    		
    		player = new Player(new Sprite(new Texture("img/player.png")),(TiledMapTileLayer) map.getLayers().get(0));
    		player.setPosition(11 * player.getCollisionLayer().getTileWidth(), (player.getCollisionLayer().getHeight() - 14) * player.getCollisionLayer().getTileHeight());
    		
    		Gdx.input.setInputProcessor(player);
    		
    	}
    
    	@Override
    	public void hide() {
    		dispose();
    		
    	}
    
    	@Override
    	public void pause() {
    		
    	}
    
    	@Override
    	public void resume() {
    		
    	}
    
    	@Override
    	public void dispose() {
    		map.dispose();
    		renderer.dispose();
    		player.getTexture().dispose();
    	}
    
    }
    Greets
    GS (:

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How can I add a background Sprite?

    it never works.
    Please explain what happens. What does "never works" mean?

    Where do those 3rd party packages come from? Is there a forum for them?
    Have you read the API doc for them? The code isn't using many classes from java SE.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    GameSoft is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: How can I add a background Sprite?

    Hi

    What do you mean with " 3rd party" packages ? I'm using LibGDX.

    My background stays black. I want an image background.
    This is the code that never works:

    <CODE>
    package com.TEST.Dermat.screens;

    import com.TEST.Dermat.entities.Player;
    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.Sprite;
    import com.badlogic.gdx.maps.tiled.TiledMap;
    import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
    import com.badlogic.gdx.maps.tiled.TmxMapLoader;
    import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTi ledMapRenderer;

    public class Play implements Screen {

    private TiledMap map;
    public OrthogonalTiledMapRenderer renderer;
    private OrthographicCamera camera;

    private Player player;

    private SpriteBatch batch; // this is what I have added
    private Texture texture; // this

    public void create () { // this
    batch = new SpriteBatch(); //this
    texture = new Texture(Gdx.files.internal("image.png")); //this
    }

    @Override
    public void render(float delta) {

    Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    renderer.render();

    camera.position.set(player.getX() + player.getWidth() / 2, player.getY() + player.getHeight() / 2, 0);
    camera.update();

    renderer.setView(camera);
    renderer.render();

    batch.begin(); // this
    batch.draw(texture, 10, 10); // this
    batch.end(); // this

    renderer.getSpriteBatch().begin();
    player.draw(renderer.getSpriteBatch());
    renderer.getSpriteBatch().end();

    }

    @Override
    public void resize(int width, int height) {
    camera.viewportWidth = width / 0.75f;
    camera.viewportHeight = height / 0.75f;

    }

    @Override
    public void show() {

    map = new TmxMapLoader().load("maps/map.tmx");

    //TmxMapLoader loader = new TmxMapLoader();
    //map = loader.load("maps/map.tmx");
    //map = new TmxMapLoader().load("maps/map.tmx");

    renderer = new OrthogonalTiledMapRenderer(map);

    camera = new OrthographicCamera();

    player = new Player(new Sprite(new Texture("img/player.png")),(TiledMapTileLayer) map.getLayers().get(0));
    player.setPosition(11 * player.getCollisionLayer().getTileWidth(), (player.getCollisionLayer().getHeight() - 14) * player.getCollisionLayer().getTileHeight());

    Gdx.input.setInputProcessor(player);

    }

    @Override
    public void hide() {
    dispose();

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
    map.dispose();
    renderer.dispose();
    player.getTexture().dispose();
    }

    }
    </CODE>

    Gr
    GS

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How can I add a background Sprite?

    What do you mean with " 3rd party" packages ?
    1st - Java SE
    2nd- your code
    3rd - everything else
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    GameSoft is offline Member
    Join Date
    Apr 2014
    Posts
    3
    Rep Power
    0

    Default Re: How can I add a background Sprite?

    Where do those 3rd party packages come from? Is there a forum for them?
    I use Libgdx ( libgdx )

    When I run it, the application crashes.

    Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.NEVALS.Dermat.screens.Play.render(Play.java:48 )
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.NEVALS.Dermat.Dermat.render(Dermat.java:20)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.m ainLoop(LwjglApplication.java:207)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1 .run(LwjglApplication.java:114)
    Gr
    GL

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: How can I add a background Sprite?

    Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.NEVALS.Dermat.screens.Play.render(Play.java:48 )
    A null value was found when the code at line 48 was executed. Look at line 48 and find where the null value is. Then backtrack to find out why it was null.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. What am I doing wrong in moving this sprite?
    By Wesley.laferriere in forum AWT / Swing
    Replies: 9
    Last Post: 11-06-2012, 10:11 PM
  2. Flickering on a 2D Sprite
    By driiper in forum Java 2D
    Replies: 5
    Last Post: 05-06-2012, 05:30 PM
  3. import sprite
    By Bimz in forum New To Java
    Replies: 0
    Last Post: 08-28-2011, 12:15 PM
  4. Confisuion about sprite.getX() and sprite.getY()
    By Basit781 in forum Java Gaming
    Replies: 0
    Last Post: 01-10-2011, 06:16 AM
  5. Sprite Movement
    By Curtiz in forum Java Gaming
    Replies: 1
    Last Post: 04-26-2010, 01:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •