0
\$\begingroup\$

Does anyone have a clue that will make my bullet sprite fade from yellow to white? As the bullet is created it will be white but when it trails on it will turn yellow. Any ideas how? Is it a simple process or does it require a lot of code writing?

\$\endgroup\$
2
\$\begingroup\$

You could change the color of the sprite batch when it is drawing the bullet, and then back to white, like this:

public void render(float delta) {
    batch.begin();
    // some rendering
    worldRenderer.renderBullets(batch);
    // some other rendering
    batch.end();
}

And, in renderBullets() of the world renderer you do the following:

public void renderBullets(final SpriteBatch batch) {
    for (final Bullet bullet : world.getBullets()) {
        if (bullet.position.x > <certain x pos> || bullet.position.y > <certain y pos> 
           || bullet.position.x < <certain x pos> || bullet.position.y < <certain y pos>) {
            batch.setColor(Color.YELLOW);
        }
        batch.draw(bullet.texture, bullet.position.x, bullet.position.y);
        batch.setColor(Color.WHITE);
    }
}

Explaining the code above: You first iterate through all the bullets you currently have.

Then, depending in which direction you are shooting, you need to check the bullet position, relative to some certain position close to the starting position of rendering your bullet, so you know when to tell the sprite batch it is time to tint the bullet texture color to yellow. The ">" and "<" checks are meant to cover the possibilities of shooting in the right and up directions and in the left and down directions respectively, depending on your implementation.

You then set the batch color to YELLOW, so this will tint the rendering of the bullet texture. After the bullet has been rendered, you restore the WHITE color of the batch, to let it render other stuff in normal way.

Hope this works for you. If you have questions, please let me know.

\$\endgroup\$
  • \$\begingroup\$ Haven't tried it yet but I will test it out and give you a star if it works. \$\endgroup\$ – G3tinmybelly Mar 20 '14 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.