libGDX中的计时器错误

时间:2015-04-24 18:23:17

标签: java android libgdx

计时器中的new Task ()有问题。编译器仍然在new Task ()

找到错误
  

错误:(52,28)错误:找不到符号类任务

我不知道为什么会这样。

我在Android Studio中编程。

这是我的代码:

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Timer;

public class flashgame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture logo;
    Timer ti;
    BitmapFont font;
    int sch;
    int scw;
    String sc_he;
    String sc_wi;
    int imgh;
    int imgw;

    @Override
    public void create () {
        batch = new SpriteBatch();
        sch = Gdx.graphics.getHeight();
        sc_he = sch+"";
        scw = Gdx.graphics.getWidth();
        sc_wi = scw+"";
        logo = new Texture(Gdx.files.internal("logo.png"));
        imgsize(logo);
        font = new BitmapFont();
        font.setColor(Color.RED);
    }

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

        Timer.schedule(new Task() {
            @Override
            public void run() {
                batch.begin();
                batch.setColor(0, 0, 0, 1);
                batch.draw(logo, scw / 2 - imgw / 2, sch / 2 - imgh / 2);
                batch.end();
            }
        }, 1.0 / 60.0);
    }

    public void imgsize (Texture id) {
        imgh = id.getHeight();
        imgw = id.getWidth();
    }
}

2 个答案:

答案 0 :(得分:1)

在AIDE中有一些错误,在行中:

Timer.schedule(new Timer.Task() { ... }, 1.0 / 60.0);

1.0 / 60.0返回double,但构造函数等待float。 这对我有用:

Timer.schedule(new Timer.Task() { ... }, 1.0f / 60.0f );

答案 1 :(得分:0)

在代码的顶部,此类没有导入。它最初是在Timer目录中定义的。

所以你需要导入这个类

import com.badlogic.gdx.utils.Timer.Task;

标题的最终结果是:

...
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;