Libgdx viewports integration - java

Can someone just give me a real quick answer on how i would implement a viewport so this sprite would fill the whole screen?
Thanks
Current code (not working):
public SplashScreen(Jump game)
{
this.game = game;
cam = new OrthographicCamera();
cam.setToOrtho(false, 1920, 1080);
sb = new SpriteBatch();
}
public void show()
{
}
public void render(float delta)
{
Gdx.gl20.glClearColor(0.2F, 0.6F, 1F, 1F);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(Assets.splash_spr_background, 0, 0);
sb.end();
}

On
#Override
public void resize(int width, int height) {
stage.getViewport().update(Gdx.graphics.getWidth(),
Gdx.graphics.getHeight(), false);
}
as spritebatch does not have a viewport. You have to control this using stage or cam.
you can do that.

Related

How to make a sprite jump up and return to original position GDXLib

I'm very new to programming so bear with me here...
I'm making a basic 2d android game. I am trying to simply make a sprite jump up in the air a bit and then land back to where it originally started. The sprite that I want to jump is "ball2". This is my code so far (Basically just added textures and the sprite in the correct position to start the jump but haven't done anything else).
Any help is appreciated.
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Texture background;
Texture ball;
Texture spike1;
Texture spike2;
#Override
public void create () {
batch = new SpriteBatch();
background = new Texture("gamebackground.png");
ball = new Texture("ball2.png");
ball.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
spike1 = new Texture("spike1.png");
spike1.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
spike2 = new Texture("spike2.png");
}
#Override
public void render () {
batch.begin();
float scaleFactor = 2.0f;
batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.draw(ball, 80, 145, ball.getWidth() * scaleFactor, ball.getHeight() * scaleFactor);
batch.end();
}
#Override
public void render () {
batch.begin();
float scaleFactor = 2.0f;
batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.draw(ball, 80, 145, ball.getWidth() * scaleFactor, ball.getHeight() * scaleFactor);
batch.end();
Gdx.input.setInputProcessor(new InputAdapter () {
#Override
public boolean keyDown (int keycode) {
if(keycode==Keys.UP)
{
ball.setHeight(ball.getHeight()+50);
}
return true;
}
}
}
Then you need to add the timer down down or you could add down button. Not sure if you have spikes on the top as well. I don't know what it's supposed to look like.

libGDX FitViewport works not on Android

I have this approach which works and scales as expected on Windows Desktop:
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch m_batch;
FitViewport m_viewport;
OrthographicCamera m_camera;
private Texture img;
private BitmapFont m_font;
#Override
public void create() {
Gdx.app.setLogLevel(Application.LOG_NONE);
m_batch = new SpriteBatch();
m_camera = new OrthographicCamera();
m_camera.position.set(640 / 2f, 400 / 2f, 0);
m_camera.update();
m_viewport = new FitViewport(640, 400, m_camera);
img = new Texture("badlogic.jpg");
m_font = new BitmapFont();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
m_batch.begin();
m_font.draw(m_batch, "X000000000000000000000000000000000000000000000000000000000000000000000000000000X", 0, m_camera.viewportHeight);
m_batch.draw(img, 0, 0);
m_batch.end();
}
#Override
public void resize(int width, int height) {
m_viewport.update(width, height);
}
}
Initial Window Size:
Bigger Window Size
But if I deploy to Android (in this case Nexus 7 2013) it seems not to scale properly:
Why does it not work? How to solve this?
You forgot to use the camera in your sprite batch. Call
spriteBatch.setProjectionMatrix(viewport.getCamera().combined);
before spriteBatch.begin();.

Libgdx Texture is flickering when being moved

I have an image that I want to move to the right. To do this I increase its x position by 200f*Gdx.graphics.getDeltaTime(); every time render is called. I've read other posts where people had similar problems but were they were not multiplying by delta or Gdx.graphics.getDeltaTime(). Because I obviously do this why is my image flickering/vibrating when its moving???
My code:
public class gm extends Game{
OrthographicCamera cam;
SpriteBatch batch;
Texture img;
float x = 0;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
cam = new OrthographicCamera();
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
#Override
public void render () {
super.render();
//x += 200f*Gdx.graphics.getDeltaTime();
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(img, x += 100f*Gdx.graphics.getDeltaTime(), 0);
batch.end();
}
}

Why is this not displaying my background image title.png?

apologies in advance for the very newbie question, currently trying to learn libGDX, every tutorial suggests this should work, its correctly displaying the text button in the centre of the screen however it isnt showing the background image title.png.
ive played around with it quite a bit to try and get it to work with no luck, any help you can give me would be greatly appreciated!
public class MainMenu implements Screen {
ZebraGems game;
Stage stage;
BitmapFont font;
BitmapFont blackfont;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
TextButton button;
Texture titleTexture;
Sprite titleSprite;
public MainMenu(ZebraGems game){
this.game = game;
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
batch.begin();
titleSprite.draw(batch);
stage.draw();
batch.end();
}
#Override
public void resize(int width, int height) {
if (stage == null);
stage = new Stage (width, height, true);
stage.clear();
titleTexture = new Texture("data/title.png");
titleSprite = new Sprite(titleTexture);
titleSprite.setColor(1,1,1,0);
titleSprite.setSize(480, 800);
titleSprite.setPosition (0, 0);
Gdx.input.setInputProcessor(stage);
TextButtonStyle style = new TextButtonStyle();
style.up = skin.getDrawable("play");
style.down = skin.getDrawable("playpressed");
style.font = blackfont;
button = new TextButton("", style);
button.setWidth(213);
button.setHeight(94);
button.setX(Gdx.graphics.getWidth() /2 - button.getWidth() /2);
button.setY(Gdx.graphics.getHeight() /2 - button.getHeight() /2);
stage.addActor(button);
}
#Override
public void show() {
batch = new SpriteBatch();
atlas = new TextureAtlas("data/playButton.pack");
skin = new Skin();
skin.addRegions(atlas);
blackfont = new BitmapFont(Gdx.files.internal("data/blackfont.fnt"));
}
#Override
public void hide() {
dispose();
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
skin.dispose();
atlas.dispose();
stage.dispose();
blackfont.dispose();
}
}
Move stage.draw() out of the batch.begin() and batch.end().
Furthermore in case you are using the latest nighty build of libgdx, you should add stage.getViewport().update(width, height, true) to your resize(...) method.
Furthermore you are doing this titleSprite.setColor(1,1,1,0) which sets the alpha of the background sprite to 0 which means 100% transparent = invisible.
Do this instead: titleSprite.setColor(1,1,1,1).
In total your render should look like this:
batch.begin();
titleSprite.draw(batch);
batch.end();
stage.act(delta);
stage.draw();

libgdx with scene2d and actor is not displaying the sprite

I am testing out Libgdx and Scene2d. I expected this small program to display a logo, but it draws a black screen only. Any idea what am I missing?
public class MyGame implements ApplicationListener {
private Stage stage;
#Override
public void create() {
stage = new Stage(800, 800, false);
Gdx.input.setInputProcessor(stage);
MyActor actor = new MyActor();
stage.addActor(actor);
}
#Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
#Override
public void dispose() {
stage.dispose();
}
#Override
public void resize(int width, int height) {
stage.setViewport(800, 800, false);
}
}
public class MyActor extends Actor {
Sprite sprite;
public MyActor() {
sprite = new Sprite();
sprite.setTexture(new Texture("data/libgdx.png"));
setWidth(sprite.getWidth());
setHeight(sprite.getHeight());
setBounds(0, 0, getWidth(), getHeight());
setTouchable(Touchable.enabled);
setX(0);
setY(0);
}
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
batch.draw(sprite, getX(), getY());
}
}
Construct the sprite with the texture and use Gdx.file.internal:
sprite = new Sprite(new Texture(Gdx.files.internal("data/libgdx.png")));
Anyway, if you just want to display and act on images, you might prefer to use Image class:
private Stage stage;
private Texture texture;
#Override
public void create() {
stage = new Stage();
Gdx.input.setInputProcessor(stage);
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
com.badlogic.gdx.scenes.scene2d.ui.Image actor = new com.badlogic.gdx.scenes.scene2d.ui.Image(region);
stage.addActor(actor);
}
#Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
I was getting a black screen too until I explicitly set Actor's height (setHeight(height)) and width (setWidth(width)) to the Sprite's values.
tex = new Texture(Gdx.files.internal("happy.png"));
Image happy = new Image(tex);
/* happy.setBounds(happy.getX(), happy.getY(), happy.getWidth(), happy.getHeight()); not needed if using full image */
stage.addActor(happy);
Your problem is most likely this line, in the draw method
batch.draw(sprite, getX(), getY());
The code I have seen when drawing sprites is
sprite.draw(batch);

Categories