如何将图像设置为在另一个随机位置出现在计时器结束时?

How do i set the image to appear at the end of the timer in another random place?

我正在尝试让图像在计时器结束时重新出现。我不知道我做错了什么。该图像一开始出现在随机位置,仅此而已。 我需要的是启动应用程序,然后在随机位置显示 3 个点,并且每隔 1 秒出现在另一个随机位置

public class Main extends ApplicationAdapter {
    SpriteBatch batch;
    Texture blue;
    Texture red;
    Texture green;
    Sprite spriteb;
    Sprite spriter;
    Sprite spriteg;
    int x;
    int y;
    Random random = new Random();
    private float counter;
    private int number;

    @Override
    public void create () {
        batch = new SpriteBatch();
        blue = new Texture("blue.png");
        red = new Texture("red.png");
        green = new Texture("green.png");
        spriteb = new Sprite(blue);
        spriter = new Sprite(red);
        spriteg = new Sprite(green);
        spriteb.setPosition(random.nextInt(Gdx.graphics.getWidth()), random.nextInt(Gdx.graphics.getHeight()));
        spriter.setPosition(random.nextInt(Gdx.graphics.getWidth()), random.nextInt(Gdx.graphics.getHeight()));
        spriteg.setPosition(random.nextInt(Gdx.graphics.getWidth()), random.nextInt(Gdx.graphics.getHeight()));


    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        spriteb.draw(batch);
        spriter.draw(batch);
        spriteg.draw(batch);
        batch.end();
    }

        public void render (float deltaTime) {
            counter -= deltaTime;
            if (counter <= 0) {
                spriteb.setPosition(random.nextInt(Gdx.graphics.getWidth()), random.nextInt(Gdx.graphics.getHeight()));
                counter += 1000; // add one second
                batch.begin();
                spriteb.draw(batch);
                batch.end();
            }
        }
}

我想在这里保持建设性,但我很难理解在哪里调用什么。

如果我是你,我会添加一些关于在什么地方调用什么的描述,或者你写的完整代码。

现在进入正题

在您的 render() 方法中

boolean regeneratePosition;
count -= delta; 
if(count<=0) 
count = Time;
regeneratePosition = true;
}
if(regeneratePosition) {
//your regenerate position code goes here
}

其他代码保留。

希望对您有所帮助

不要发明轮子。我认为这个解决方案对你有好处:

Timer.schedule(
    new Task(){ 
        @Override 
        public void run() {
             spriteb.setPosition(random.nextInt(Gdx.graphics.getWidth()), random.nextInt(Gdx.graphics.getHeight()));
             spriter.setPosition(random.nextInt(Gdx.graphics.getWidth()), random.nextInt(Gdx.graphics.getHeight()));
             spriteg.setPosition(random.nextInt(Gdx.graphics.getWidth()), random.nextInt(Gdx.graphics.getHeight()));
        } 
    }, delay, interval);

将此代码放入您的 create() 方法中,看看它是否有效。如果仍然无法正常工作,请写信。