michaeljan
michaeljan

Reputation: 301

SetTexture and Sprites not working as intended! (LibGDX)

I searched for and wide on the internet and I have yet to find an answer. I have an object called GameIcon which extends Sprite. Everything about it is okay except for the texture. Here is my code for the GameIcon class.

package com.xx4everPixelatedxx.gaterunner.sprites;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.xx4everPixelatedxx.gaterunner.GateRunner;

import javax.xml.soap.Text;

/**
 * Created by Michael Jan on 8/17/2015.
 */
public class GameIcon extends Sprite {
    private int vX = 3;
    private int vY = 3;
    private int r = 9;
    private int rotation;

    private Vector3 position;

    private Texture texture;

    public GameIcon(int x, int y) {
        position = new Vector3(x, y, 0);
        texture = new Texture(Gdx.files.internal("icon_players/icon1.png"));
        setTexture(texture);
    }

    public void update() {
        position.add(vX, vY, 0);
        rotation = rotation + r;

        rotation = rotation % 360;

        setRotation(rotation);
        setOriginCenter();
    }

    public void addPosition(int x, int y) {
        position.add(x, y, 0);
        setOriginCenter();
    }

    public void negateVelocityX() {
        vX = -vX;
    }

    public void negateRotation() {
        r = -r;
    }

    public Vector3 getPosition() {
        return position;
    }

    public int getvY() {
        return vY;
    }

    public void setvY(int vY) {
        this.vY = vY;
    }

    public int getvX() {
        return vX;
    }

    public void setvX(int vX) {
        this.vX = vX;
    }
}

Here is the icon1.png: https://i.gyazo.com/1d52f5e58b227f08809f6c14ae4c94a4.png

Here is what I am getting: https://i.gyazo.com/1881a9392955af34de5c55b9b8fac391.png

Note that the rotation of the image and the particles are intended. The problem is that the big square should be the texture (icon1.png), and I do not know how to fix this.

(Not enough reputation to post pictures)

Upvotes: 3

Views: 724

Answers (1)

Manuel Miranda
Manuel Miranda

Reputation: 823

I'm not familiar with LibGDX, but this may have something to do with the fact that you are maybe overwriting TextureRegion.texture. Could you try to user your parent class Sprite(Texture) constructor like this:

...
public class GameIcon extends Sprite {
   private int vX = 3;
   private int vY = 3;
   private int r = 9;
   private int rotation;

   private Vector3 position;

   //private Texture texture; 

   public GameIcon(int x, int y) {
       super(new Texture(Gdx.files.internal("icon_players/icon1.png")))
       position = new Vector3(x, y, 0);
   }
...

As stated by Tenfour04 in comments, this method works because the parent's constructor applies the width and height of the texture, while setTexture() does not.

Upvotes: 3

Related Questions