LibGDX problems rotating sprite

一个人想着一个人 提交于 2020-02-05 07:31:21

问题


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.


回答1:


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

With setOrigin(widthSprite\2, heightSprite\2)

That action rotate sprite itself.




回答2:


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.




回答3:


Try

sprite.setOriginCenter();

This should help



来源:https://stackoverflow.com/questions/22151098/libgdx-problems-rotating-sprite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!