LibGDX problems rotating sprite

2019-08-05 11:56发布

OK so I'm really confused I've rotated sprites before and had no problem such as rotating a boat as it moves through an ocean, but for some reason I'm having a really big problem this time. So I create a texture in an assets file, but not static textures. I load the texture using the following:

class Assets{
    Texture img;
    public Assets(){
        img = new Texture(Gdx.files.internal("images/PNG.png")

And then I call the assets in the main class by calling:

Assets assets = new Assets()

And then I have a class that is an animator just for this main character because his animation is so different and varied from other characters.

class Animations{
    Guy MYGUY;
    Texture firstTexture;
    ArrayList<Texture> running;
    Sprite CurrentSprite;
    public Animations(Texture standingStill, Guy myGuy){
        MYGUY = myGuy;
        firstTexture = standingStill;
        running = new ArrayList<Texture>();
        running.add(firstTexture);
        CurrentSprite = new Sprite(firstTexture);

    public void update (int direction, int state){
         CurrentSprite.setPosition(MYGUY.X, MYGUY.Y)
        // I have a switch here, but it does nothing yet because I haven't added in different actions for the character.
        //However I do have a switch for direction, because that is important right now
        switch(state){
        case Guy.LEFT:
            CurrentSprite.set rotation(180);
        //yes there are more, but even rotating 180 won't work correctly
        }

Then I have a renderer class to draw everything, i have the object MyGuy in an object for the world called myLand and I draw it with:

myLand.GUY.animation.CurrentSprite(batch);

So my problem arises on the rotation, whenever it rotates 180 degrees it seems to always rotate around the coordinates (0, 0) instead of the center of the sprite. So it usually ends up where I move like five to the right, but then if I try to go left it does double the distance backwards, but the camera position stays the same, and the guy usually disappears off the left or right side of the screen.

3条回答
劫难
2楼-- · 2019-08-05 12:14

Try use rotate(...)method instead of setRotation(...).

With setOrigin(widthSprite\2, heightSprite\2)

That action rotate sprite itself.

查看更多
干净又极端
3楼-- · 2019-08-05 12:30

Try

sprite.setOriginCenter();

This should help

查看更多
SAY GOODBYE
4楼-- · 2019-08-05 12:31

Instead of rotating the sprite, just flip it with this single line:

CurrentSprite.flip(true, false);

the first boolean is X flip (that's what you want to set as true when going left) and the second is the Y flip.

查看更多
登录 后发表回答