简体   繁体   English

控制精灵旋转(它像疯了似的旋转)

[英]Control sprite rotation (it's spinning like crazy)

So, I have created a texture, and then a sprite. 因此,我创建了一个纹理,然后创建了一个精灵。 On my render() method, I am check for user input. 在我的render()方法上,我正在检查用户输入。 If the user has touched/clicked, then I want my sprite to rotate 90 degrees ONCE. 如果用户已经触摸/单击,那么我希望我的精灵一次旋转90度。

Right now the rotation works. 现在轮换工作了。 However, it rotates multiple times per click! 但是,它每次点击旋转多次!

How can I make it rotate only once per touch? 如何使其每次触摸只能旋转一次? I have a feeling that I might have to use delta time, and that occurs because the render method is being called frequently, but I don't know how to fix it... Thanks! 我有一种可能需要使用增量时间的感觉,这是因为经常调用render方法,但是我不知道如何解决它……谢谢!

public class MyGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;
    Sprite sprite;

    @Override
    public void create () {
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");
        sprite = new Sprite(img);
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        sprite.draw(batch);
        batch.end();

        if (Gdx.input.isTouched()) {
            rotateRight();
        }
    }

    private void rotateRight() {
        sprite.setRotation(sprite.getRotation() - 90);
    }
}

Right now you are polling input inside of your render method. 现在,您正在轮询render方法中的输入。 Polling simply checks the status of the input (is it touched or not) and does not care for any actual "event" occurred. 轮询仅检查输入的状态(是否被触摸),并不关心实际发生的任何“事件”。

Instead of this you need to look for input events via event handling as this will give you access to the actual event of the screen being touched or untouched. 取而代之的是,您需要通过事件处理来查找输入事件,因为这将使您能够访问触摸或未触摸屏幕的实际事件。 You can do this by implementing InputProcessor which will give you access to override a bunch of touch event methods so you can execute your rotate method only on the event of the touch. 您可以通过实现InputProcessor来做到这一点,该方法将使您能够重写一系列的触摸事件方法,以便仅在发生触摸事件时才可以执行rotate方法。

Example: 例:

public void update() 
{
     Gdx.input.setInputProcessor(new InputProcessor() {
        @Override
        public boolean TouchDown(int x, int y, int pointer, int button) {
            if (button == Input.Buttons.LEFT) {
                rotateRight();
                return true;
            }
            return false
        }
    });
}

Don't forget to call your update() method from your render method. 不要忘记从render方法中调用update()方法。 I put it in a separate function just for readability and not to clog up your rendering code. 我将其放在单独的函数中只是为了提高可读性,而不会阻塞您的渲染代码。

You can also have your class implement InputProcessor and override the methods in your class so you do not have to do it inline as I did above. 您还可以让您的类实现InputProcessor并覆盖您类中的方法,因此您不必像上面那样内联地进行操作。

if (Gdx.input.justTouched() && Gdx.input.isTouched()) {
    rotateRight();
}

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

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