繁体   English   中英

如何以定义的速度连续旋转精灵? 有关游戏编程的一般问题

[英]How to rotate sprite continuously at a defined speed? And general questions about game programming

我有一个渲染的图像:

public void render(float delta) {
    batch.begin();
    batch.draw(new Sprite(new Texture("img1.png"), 64, 64), x, y);
    batch.end()

其中x和y用于根据屏幕分辨率将精灵居中放置在屏幕中间。

  1. 我知道我可以使用rotate(float degrees)来旋转它,但是如何连续进行呢? 我想我需要以某种方式使用增量时间,例如x += 50 * Gdx.graphics.getDeltaTime(); 但这不会使其连续旋转。 图像将显示一帧,然后再跳到另一帧(似乎是逐帧转换)。

  2. 如果我想让(一个)精灵进行多次碰撞检测(例如, 这里的每个彩色弧的反应都不同,是否可以在与弧一起移动的顶部上覆盖一个不可见的圆并在该圆上使用数学运算来解决此问题?每个彩色圆弧都有一个单独的子图形,有没有更简单的方法?

  3. 对于游戏,可以不使用布局/ XML来设计关卡(也就是对所有内容进行硬编码)吗? 这是否会使使应用程序兼容多个屏幕分辨率变得更加困难?

我对此并不陌生,所以我希望这些问题有意义。

以这种方式永远旋转精灵

public class TestGame extends Game {

    SpriteBatch spriteBatch;
    Sprite ball;
    Texture ballTex;
    private int spriteRotationSpeed=1;

    @Override
    public void create() {

        spriteBatch=new SpriteBatch();
        ballTex=new Texture("image/bone.png");
        ball=new Sprite(ballTex);
        ball.setSize(50,50);
        ball.setOrigin(25,25);
        ball.setPosition(0,0);


    }

    @Override
    public void render() {
        super.render();

        Gdx.gl.glClearColor(1,1,1,1);
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        spriteBatch.begin();
        ball.draw(spriteBatch);
        spriteBatch.end();
        rotateSprite();
    }

    private void rotateSprite(){

        float rotation=ball.getRotation();
        rotation+=spriteRotationSpeed;
        if(rotation>360)
            rotation-=360;
        ball.setRotation(rotation);
    }

    @Override
    public void resize(int width, int height) {
        super.resize(width, height);
    }

    @Override
    public void dispose() {
        super.dispose();
        ballTex.dispose();
        spriteBatch.dispose();
    }
}

暂无
暂无

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

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