简体   繁体   English

LibGDX使用touchDragged()事件旋转2D图像

[英]LibGDX Rotate 2D image with touchDragged() event

Objective: to rotate an image in the center of the screen with movement equal to left or right touchDragged event. 目标:旋转屏幕中心的图像,移动等于左或右touchDragged事件。

Right now I have a basic Stage that is created and adds an actor (centerMass.png) to the stage. 现在我创建了一个基本舞台,并在舞台上添加了一个actor(centerMass.png)。 it is created and rendered like this: 它是这样创建和渲染的:

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();

    }
}

I then have a separate class file that contains the CenterMass class, extending Image. 然后我有一个单独的类文件,其中包含CenterMass类,扩展了Image。 I am familiar enough to know I could extend Actor, but I am not sure the benefit I would gain using Actor vs Image. 我很熟悉我可以扩展Actor,但我不确定使用Actor vs Image会获得什么好处。

In the CenterMass class I create the texture, set bounds, set touchable and center it on the screen. 在CenterMass类中,我创建纹理,设置边界,设置可触摸并将其居中在屏幕上。

Inside CenterMass class I also have an InputListener listening for events. 在CenterMass类中,我还有一个InputListener监听事件。 I have an override set for touchDragged where I am trying to get the X and Y of the drag, and use that to set the rotate actions accordingly. 我有一个touchDragged的覆盖设置,我试图获取拖动的X和Y,并使用它来相应地设置旋转动作。 That class looks like this: 那个班看起来像这样:

//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);
    }
}

The Problem: I have not been able to get it to rotate the way I would like. 问题:我无法按照我想要的方式进行旋转。 I have been able to get it to shift around in unpredictable ways. 我已经能够以不可预测的方式转变它。 Any guidance would be much appreciated. 任何指导都将非常感谢。

As from you code it seems everything is good. 从你的代码来看似乎一切都很好。 except you don't set any origin of the image. 除了你没有设置图像的任何原点。 without setting the origin it is by default set to 0,0.(bottom left of your image) So if yow want to rotate the image with origin to centre you have to set the origin to imageWidth/2. 没有设置原点它默认设置为0,0。(图像的左下角)因此,如果你想将原点旋转到中心,你必须将原点设置为imageWidth / 2。 imageHeight/2. imageHeight / 2。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM