1
votes

I am creating a game and trying to set up a splash screen.

Whenever I render the sprite that i want to tween to by using the sprite.draw method which looks like this:

@Override
public void render(float delta) 
{
    Gdx.gl20.glClearColor(0.2F, 0.5F, 1F, 1F);

    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

    tm.update(delta);

    cam.update();

    sb.setProjectionMatrix(cam.combined);

    sb.begin();

    Assets.splash_spr_bg.draw(sb);

    sb.end();

}

The tweening works great except i can only see 1/4 of the picture on my screen, it is completely out of position.

And whenever I try to use this code in order to render the sprite by using the spritebatch to draw it, which looks like this:

@Override
public void render(float delta) 
{
    Gdx.gl20.glClearColor(0.2F, 0.5F, 1F, 1F);

    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

    tm.update(delta);

    cam.update();

    sb.setProjectionMatrix(cam.combined);

    sb.begin();

    sb.draw(Assets.splash_spr_bg, 0, 0);

    sb.end();

}

I can see the background great, great quality, correct size and position and all that. However, the tweening doesn't work at all; nothing happens.

Why does this not work? How can I fix it?


Here is some other code.

Initializion of the TweenHandler class:

package com.heavenapps.jumpdodge.handlers;

import com.badlogic.gdx.graphics.g2d.Sprite;

import aurelienribon.tweenengine.TweenAccessor;

public class TweenHandler implements TweenAccessor<Sprite> 
{

public static final int ALPHA = 1;

@Override
public int getValues(Sprite target, int tweenType, float[] returnValues) 
{
    switch(tweenType)
    {
    case ALPHA:
        returnValues[0] = target.getColor().a;
        return 1;
    default:
        return 0;
    }
}

@Override
public void setValues(Sprite target, int tweenType, float[] newValues) 
{
    switch(tweenType)
    {
    case ALPHA:
        target.setColor(1, 1, 1, newValues[0]);
        break;
    }
}

}

Initializion of the sprite/texture:

 package com.heavenapps.jumpdodge.handlers;

 import com.badlogic.gdx.Gdx;
 import com.badlogic.gdx.graphics.Texture;
 import com.badlogic.gdx.graphics.Texture.TextureFilter;
 import com.badlogic.gdx.graphics.g2d.Sprite;

 public class Assets 
 {

    public static Texture splash_tex_bg;

    public static Sprite splash_spr_bg;

    public static void init()
    {
    // Splash Screen

    splash_tex_bg = new Texture(Gdx.files.internal("Splash Screen/Background.png"));

    splash_tex_bg.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    splash_spr_bg = new Sprite(splash_tex_bg);

    splash_spr_bg.setOrigin(splash_spr_bg.getWidth() / 2, splash_spr_bg.getHeight() / 2);

    splash_spr_bg.setPosition(Gdx.graphics.getWidth(),    Gdx.graphics.getHeight());

    splash_spr_bg.setColor(1, 1, 1, 0);


}

}

Usage of the tweening:

public void fadeSplashScreen()
{
    Tween.to(Assets.splash_spr_bg, TweenHandler.ALPHA, 2F).target(1).ease(TweenEquations.easeInBounce).start(tm);
}
1

1 Answers

0
votes

You need to set the sprite's position if you want to draw it using your first method. (sprite.draw(spriteBatch)). And you need to use your first method if you want to use the sprite's color, which is being controlled by the tween.

It looks like you did give the background sprite an initial position, but you put it off screen. So change

splash_spr_bg.setPosition(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

to

splash_spr_bg.setPosition(0, 0);

The reason the second method you showed (spriteBatch.draw(sprite, x, y)) draws it in the correct position is that this method of drawing ignores the fact that it's a sprite and just draws the texture region owned by the sprite in whatever position you give it. And this method doesn't fade the sprite because it's ignoring the fact that it is a sprite (which has a color).