Gabriel
Gabriel

Reputation: 19

sprite.SetScale Function not working (libGDX)

Hello i'm trying to make a simple game with libGDX but i'm running with some trouble using the sprite class, this is my code:

package com.carebearer;

import com.badlogic.gdx.ApplicationAdapter;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;


public class demo extends ApplicationAdapter{
    SpriteBatch batch;
    Texture img;
    Sprite sprite;

    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture("hello.png"); 
        sprite = new Sprite(img);
        sprite.setScale(.5f);
        sprite.setPosition(Gdx.graphics.getWidth()/2 - sprite.getWidth()/2, 
                           Gdx.graphics.getHeight()/2 - sprite.getHeight()/2);

    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1); //RGBAlpha, 1 = 100%
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(sprite, sprite.getX(), sprite.getY()); 
        batch.end();
    } 
   }

The problem is the setScale function.

Whether or not i use setScale the sprite drawn stays the same (It's essentially doing nothing). What i want it to do is to halve in size the Sprite

I'm pretty sure i'm missing something obvious but i've been stuck with this problem more time than i would like to admit.

Upvotes: 2

Views: 943

Answers (1)

Tenfour04
Tenfour04

Reputation: 93902

You're not using Sprite correctly. Use sprite.draw(batch) not batch.draw(sprite, ...). If you use batch.draw then SpriteBatch treats Sprite like a dumb TextureRegion and ignores all its position and size parameters. And if you manually apply them like you did in your comment, you might as well use the much lighter weight TextureRegion instead of SpriteBatch.

Upvotes: 3

Related Questions