Sprite doesn't display in Libgdx - java

I make simply aplication in LibGDX. I have got two textures (background and sprite). The background is diplayed ok but I don't see the sprite. I have tried many solutions but nothing works. I don't know what is going on. Thanks for help.
Here is my code:
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
Texture.setEnforcePotImages(false);
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("graphic/bg1.png"));
bad = new Texture(Gdx.files.internal("graphic/b.png"));
layerOne = new TextureRegion(texture);
spriteb = new Sprite(bad);
rbg = new ParallaxBackground(new ParallaxLayer[]{
new ParallaxLayer(layerOne, new Vector2(),new Vector2()),
}, w, h,new Vector2());
}
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
rbg.render();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
spriteb.setPosition(400, 200);
spriteb.draw(batch);
batch.end();
}

Your camera viewport is too small for the Sprite position
camera = new OrthographicCamera(1, h/w);
...
spriteb.setPosition(400, 200);
The sprite is being rendered far outside of the viewport.
Try setting your camera like this:
camera = new OrthographicCamera(w, (h/w)*h);

Related

Java LibGDX: Tiledmap not rendering at the top and right side

I'm trying to get a tiledmap to render over the whole screen. The problem is that the top or right 100px, everything is just black there. My tiledmap is around 100 tiles, so it should be able to render more than what it renders.
This is how it looks right now:
1: This bar is expected as I do, currently, not have anything to render there.
2 & 3: These bars are unexpected as there are more parts of my tiledmap to render there.
I've already tried changing my Viewport from StretchViewport to FitViewport as well as FillViewport, but that doesn't change anything.
Here's my constructor in the PlayScreen class:
public Play(GameStateManager gsm) {
super(gsm);
//set up box2d
world = new World(new Vector2(0, -20.81f), true);
cl = new MyContactListener();
world.setContactListener(cl);
b2dr = new Box2DDebugRenderer();
//set up viewport
vw = new StretchViewport(1280, 720, cam);
cam.update();
//create player
createPlayer();
//create tiles
createTiles();
//setup box2D cam
b2dCam = new OrthographicCamera();
b2dCam.setToOrtho(false, Game.V_WIDTH / PPM, Game.V_HEIGHT / PPM);
}
Here's my createTiles() method:
private void createTiles(){
loader = new TmxMapLoader();
tileMap = loader.load("data/lvl1.tmx");
tmr = new OrthogonalTiledMapRenderer(tileMap);
cam.update();
//get the size of the tiles
tileSize = tileMap.getProperties().get("tilewidth", Integer.class);
//create box2d bodies for all tiledmap layers
TiledMapTileLayer layer;
layer = (TiledMapTileLayer) tileMap.getLayers().get("red");
createLayer(layer, B2DVars.BIT_RED);
layer = (TiledMapTileLayer) tileMap.getLayers().get("orange");
createLayer(layer, B2DVars.BIT_ORANGE);
layer = (TiledMapTileLayer) tileMap.getLayers().get("yellow");
createLayer(layer, B2DVars.BIT_YELLOW);
layer = (TiledMapTileLayer) tileMap.getLayers().get("green");
createLayer(layer, B2DVars.BIT_GREEN);
layer = (TiledMapTileLayer) tileMap.getLayers().get("blue");
createLayer(layer, B2DVars.BIT_BLUE);
layer = (TiledMapTileLayer) tileMap.getLayers().get("pink");
createLayer(layer, B2DVars.BIT_PINK);
}
The render() method:
public void render(float delta) {
//clear screen
Gdx.gl.glClearColor(0.619607843f, 0.79607843137f, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//set cam to follow player
cam.position.set(
player.getPosition().x * PPM + Game.V_WIDTH / 4,
Game.V_HEIGHT / 2,
0
);
//draw tile map
tmr.setView(cam);
tmr.render();
//draw player
sb.setProjectionMatrix(cam.combined);
player.render(sb);
//draw box2d world
b2dr.render(world, b2dCam.combined);
//responsive resizing
resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
And here's the create() method inside my main game class:
public void create () {
Gdx.input.setInputProcessor(new MyInputProcessor());
//asset managing
res = new Content();
res.loadTexture("data/player/player.png", "player");
sb = new SpriteBatch();
cam = new OrthographicCamera();
cam.setToOrtho(false, V_WIDTH, V_HEIGHT);
hudCam = new OrthographicCamera();
hudCam.setToOrtho(false, V_WIDTH, V_HEIGHT);
gsm = new GameStateManager(this);
}
I did not expect this game to get those black bars, I've created other games before and they have never gotten those. How can I remove them (2 & 3 on the image)?

Rendering sprites in Libgdx on tilemap

I'm trying to render a sprite/texture on top of a tiled map in libgdx/android studio. Note that my map is 20x25 with 8 pixel tiles and splitTiles is a Texture Region.
public void show() {
cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.setToOrtho(false, 20, 25);
batch = new SpriteBatch();
map = new TmxMapLoader().load("centipedeMap.tmx");
renderer = new OrthogonalTiledMapRenderer(map,1/8f)
}
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.setView(cam);
renderer.render();
player.update();
ex.update();
batch.setProjectionMatrix(cam.combined);
cam.update();
batch.begin();
Texture tilesImage = new Texture(Gdx.files.internal("tile.png"));
TextureRegion[][] splitTiles = TextureRegion.split(tilesImage, 8, 8);
batch.draw(splitTiles[0][0],50,50);
batch.end();
}
Your camera viewport dimension is 20, 25 and you're drawing your TextureRegion at 50,50 so your image is rendered out of screen so not visible.
Recommendation : Texture is heavy object so try to avoid creating multiple instance of same Texture.
you've camera viewport is in dimension of 20,25 that is same as tileMap(20,25) dimension so camera 1 unit denotes one cell of TileMap.
you can detect collision with Tiles,it is not necessary to create Rectangle object.

Android, creating custom ImageView shape

I need to create a view that is filled with a dynamic color at runtime. I've accomplished this task however the actual view needs to have a "peak" on the right side. Typically ImageView's are only square or rectangular. Does anyone know how I would accomplish adding a triangular peak to the view?
For example:
You can override the onDraw event of the ImageView and do whatever you need with the canvas object.
public class MyImageView extends ImageView {
...
#Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Point[] point = ... //create three points here for the triangle
Paint paint = ... // create your desired color
drawTriangle(canvas, point, paint);
}
And for the triagle draw (see this SO answer):
private void drawTriangle(Canvas canvas, Point[] point, Paint paint) {
float [] points = new float[8];
points[0] = point[0].x;
points[1] = point[0].y;
points[2] = point[1].x;
points[3] = point[1].y;
points[4] = point[2].x;
points[5] = point[2].y;
points[6] = point[0].x;
points[7] = point[0].y;
canvas.drawVertices(VertexMode.TRIANGLES, 8, points, 0, null, 0, null, 0, null, 0, 0, paint);
Path path = new Path();
path.moveTo(point[0].x , point[0].y);
path.lineTo(point[1].x,point[1].y);
path.lineTo(point[2].x,point[2].y);
canvas.drawPath(path,paint);
}

Making a ellipse object in libgx java android game

I have tried to make a ellipse object in libgdx. Sorry if i can't describe properly but im a newbie in java.
My code looks like:
public class GameScreen implements Screen{
MyGame game;
OrthographicCamera camera;
SpriteBatch batch;
...
Ellipse playBounds;
public GameScreen(MyGame game) {
this.game = game;
camera = new OrthographicCamera();
camera.setToOrtho(false, 1080, 1920);
batch = new SpriteBatch();
state = GAME_READY;
touchPoint = new Vector3();
pauseBounds = new com.badlogic.gdx.math.Rectangle(1080-128,1920-128,128,128);
playBounds= new Ellipse()
}
...
public void render(float delta) {
Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
generalupdate();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(Assets.sprite_bg, 0, 0);
switch (state){
case GAME_READY:{
batch.draw(Assets.sprite_startScreen, 0, 0);
batch.draw(Assets.sprite_playButton,218,800,644,225);
break;
}
Basically it draws background, a welcome screen and a button(with "play" on it)
So here i made a touch detection.
if (Gdx.input.justTouched()) {
camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
if (state==GAME_READY);
if (playBounds.contains(touchPoint.x, touchPoint.y)) {
state=GAME_RUNNING;
Drawing works fine but the problem is when i touch the button it doesnt work instead if i touch near it game starts as it should
Alright, ignoring the several errors in the code which I will just assume were made here instead of the actual code, I believe the problem could be that you are not setting the values in the Ellipse. By this I mean the width, height, x, and y.
An nice way to do this would be to use the constructor:
Ellipse(float x, float y, float width, float height)
instead of just :
Ellipse()
That way you can set the values right away. Refer to this website for more info in Ellipses for LibGDX.
If that doesn't solve your problem you may have to post a little more of the relevant parts of your code.

Why doesn't SpriteBatch draw anything (LibGdx)?

This is from day 6 of the flappy bird recreation tutorial
-http://www.kilobolt.com/day-6-adding-graphics---welcome-to-the-necropolis.html
Here is the image file i am using for texture in my game. It is a 256px x 64px .png file.
Here is the class that I used for loading the texture and the specific TextureRegion(part of the texure) that I want the SpriteBatch to draw.
public class AssetLoader {
public static Texture texture;
public static TextureRegion bg;
public static void load() {
texture = new Texture(Gdx.files.internal("data/texture.png"));
bg = new TextureRegion(texture, 0, 0, 136, 43);
}
}
And I call AssertLoader.load(), along with setting up game screen from
public class MyGdxGame extends Game{
#Override
public void create() {
AssetLoader.load();
setScreen(new GameScreen());
}
}
And inside GameScreen.java
public class GameScreen implements Screen {
//delegate render task
private GameRenderer renderer;
public GameScreen() {
float screenHeight = Gdx.graphics.getHeight();
float screenWidth = Gdx.graphics.getWidth();
float gameWidth = 136;
float gameHeight = screenHeight / (screenWidth / gameWidth);
renderer = new GameRenderer((int)gameHeight);
}
}
And inside GameRederer, the class I delegate to render the game
public class GameRenderer {
private int gameHeight;
private SpriteBatch batch;
private OrthographicCamera cam;
public GameRenderer(int gameHeight) {
this.gameHeight = gameHeight;
cam = new OrthographicCamera();
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
cam.setToOrtho(true, 136, gameHeight);
}
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.disableBlending();
batch.draw(AssetLoader.bg, 0, (gameHeight/2) + 23, 136, 43);
batch.end()
}
}
What I get when I run the desktop version of the game is the black screen shown above(black because i set the background to black with these lines of code
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Does anyone know why the SpritchBatch drawing isn't showing up? I extracted the texture portion of the texture I wanted with this line of code(starts from 0,0, width of 136, height of 43) - used GIMP - to find out portion to cut.
bg = new TextureRegion(texture, 0, 0, 136, 43);
I also put a few log statements(removed them from view) to ensure that before drawing, bg's width and height were set correctly, which they were. And the issue can't be game height because I used a print statement and found that to be 204 which means that this expression, (gameHeight/2) + 23 will evaluate to 125 which is in bounds between 0 and game height.
I checked out other threads as well.
My issue can't be libgdx spritebatch not rendering textures because the SpriteBatch should overwrite the background.
And it can't be LibGDX: Android SpriteBatch not drawing because i am running mine on desktop, not andorid.
could be that you have to first put cam.setToOrtho(true, 136, gameHeight);before the batch, so I can not confirm hopefully help
public GameRenderer(int gameHeight) {
this.gameHeight = gameHeight;
cam = new OrthographicCamera();
batch = new SpriteBatch();
cam.setToOrtho(true, 136, gameHeight);
batch.setProjectionMatrix(cam.combined);
}
If anyone's having a similar issue, the way I solved the problem was to call
batch.setProjectionMatrix(cam.combined);
after
cam.setToOrtho(true, 136, gameHeight);
Which didn't really make sense to me because it's still the same Matrix4 in Camera.java, that is
public final Matrix4 combined = new Matrix4();
Was hoping someone else could clarify that.

Categories