简体   繁体   English

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

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

I have an image which is rendered: 我有一个渲染的图像:

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

where x and y are used to center the sprite in the middle of the screen depending upon the screen's resolution. 其中x和y用于根据屏幕分辨率将精灵居中放置在屏幕中间。

  1. I know I can use rotate(float degrees) to rotate it, but how do I do so continuously? 我知道我可以使用rotate(float degrees)来旋转它,但是如何连续进行呢? I think I need to use delta time in some way, for example, x += 50 * Gdx.graphics.getDeltaTime(); 我想我需要以某种方式使用增量时间,例如x += 50 * Gdx.graphics.getDeltaTime(); but this does not rotate it continuously. 但这不会使其连续旋转。 The image is displayed for one frame before jumping elsewhere for one frame (seems like it's translating frame by frame instead). 图像将显示一帧,然后再跳到另一帧(似乎是逐帧转换)。

  2. If I want the (one) sprite to have multiple collision detections (for example, each colored arc here reacts differently, can overlaying an invisible circle on top that moves along with the arc and using math on that circle solve this problem? Instead of having a separate sprite for each colored arc. Is there an easier way to do this? 如果我想让(一个)精灵进行多次碰撞检测(例如, 这里的每个彩色弧的反应都不同,是否可以在与弧一起移动的顶部上覆盖一个不可见的圆并在该圆上使用数学运算来解决此问题?每个彩色圆弧都有一个单独的子图形,有没有更简单的方法?

  3. For a game, is it okay to not use layouts/XML for designing levels (ie hard coding everything instead)? 对于游戏,可以不使用布局/ XML来设计关卡(也就是对所有内容进行硬编码)吗? Will this make it more difficult to make apps compatible for multiple screen resolutions? 这是否会使使应用程序兼容多个屏幕分辨率变得更加困难?

I'm new to this so I hope these questions make sense. 我对此并不陌生,所以我希望这些问题有意义。

Rotate your sprite forever in this way 以这种方式永远旋转精灵

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