【问题标题】:LibGDX Rotate 2D image with touchDragged() eventLibGDX 使用 touchDragged() 事件旋转 2D 图像
【发布时间】:2016-05-25 07:06:50
【问题描述】:

目标:在屏幕中心旋转图像,移动等于左或右touchDragged事件。

现在我已经创建了一个基本的舞台,并在舞台上添加了一个演员 (centerMass.png)。它是这样创建和渲染的:

public class Application extends ApplicationAdapter {
Stage stageGamePlay;

@Override
public void create () {
    //setup game stage variables
    stageGamePlay = new Stage(new ScreenViewport());
    stageGamePlay.addActor(new CenterMass(new Texture(Gdx.files.internal("centerMass.png"))));

    Gdx.input.setInputProcessor(stageGamePlay);

}

@Override
public void render () {
    Gdx.gl.glClearColor(255f/255, 249f/255, 236f/255, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    //before drawing, updating actions that have changed
    stageGamePlay.act(Gdx.graphics.getDeltaTime());
    stageGamePlay.draw();

    }
}

然后我有一个单独的类文件,其中包含 CenterMass 类,扩展 Image。我很熟悉,知道我可以扩展 Actor,但我不确定使用 Actor vs Image 会获得什么好处。

在 CenterMass 类中,我创建纹理、设置边界、设置可触摸并在屏幕上居中。

在 CenterMass 类中,我还有一个 InputListener 监听事件。我为 touchDragged 设置了一个覆盖设置,我试图在其中获取拖动的 X 和 Y,并使用它来相应地设置旋转动作。该类如下所示:

//extend Image vs Actor classes
public class CenterMass extends Image {
public CenterMass(Texture centerMassSprite) {
    //let parent be aware
    super(centerMassSprite);
    setBounds(getX(), getY(), getWidth(), getHeight());
    setTouchable(Touchable.enabled);
    setPosition(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
    setRotation(90f);


    addListener(new InputListener(){
        private int dragX, dragY;
        private float duration;
        private float rotateBy = 30f;

        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {
            //get
            float dX = (float)(x-dragX)/(float)Gdx.graphics.getWidth();
            float dY = (float)(dragY-y)/(float)Gdx.graphics.getHeight();

            duration = 1.0f; // 1 second

            Actions.sequence(
                    Actions.parallel(
                            Actions.rotateBy(rotateBy, duration),
                            Actions.moveBy( dX, dY, duration)
                    )
            );


        }
    });
}

@Override
protected void positionChanged() {
    //super.positionChanged();
}

@Override
public void draw(Batch batch, float parentAlpha) {
    //draw needs to be available for changing color and rotation, I think
    batch.setColor(this.getColor());
    //cast back to texture because we use Image vs Actor and want to rotate and change color safely
    ((TextureRegionDrawable)getDrawable()).draw(batch, getX(), getY(),
            getOriginX(), getOriginY(),
            getWidth(), getHeight(),
            getScaleX(), getScaleY(),
            getRotation());
}

@Override
public void act(float delta) {
    super.act(delta);
    }
}

问题: 我无法让它按照我想要的方式旋转。我已经能够让它以不可预知的方式移动。任何指导将不胜感激。

【问题讨论】:

  • 能否请您添加有关所需旋转的图像或动画。
  • @Tauqir 不确定您的意思,我使用的图像是放置在屏幕中间的简单 16x16 图像。图像的旋转将随着屏幕上的左右拖动而移动。
  • 请使用矩阵进行旋转。

标签: android libgdx scene2d


【解决方案1】:

从您的代码看来,一切都很好。除非您没有设置图像的任何来源。在不设置原点的情况下,默认设置为 0,0。(图像的左下角) 因此,如果您想将原点旋转到中心的图像,您必须将原点设置为 imageWidth/2。图像高度/2。

 setOrigin(imageWidth/2,imageHeight/2)// something like this

【讨论】:

  • 当我在 centerMass 类中设置原点时:setOrigin(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2); 图像完全消失。当我把它放在 touchDragged 覆盖中时,只要我触摸屏幕,图像就会消失。
  • 不是屏幕宽度和高度你必须使用图像的宽度和高度
猜你喜欢
  • 2012-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多