代码之家  ›  专栏  ›  技术社区  ›  user1026996

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

  •  1
  • user1026996  · 技术社区  · 10 年前

    目标:旋转屏幕中心的图像,移动等同于向左或向右触摸拖动事件。

    现在我有了一个基本的舞台,它被创建并向舞台添加了一个演员(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);
        }
    }
    

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

    1 回复  |  直到 10 年前
        1
  •  0
  •   Godslave    10 年前

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

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