0

I have a problem with the new Task () in the timer. Compiler still find error at the new Task ()

Error:(52, 28) error: cannot find symbol class Task

I do not know why this is happening.

I programming in Android Studio.

This is my code:

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();
    }
}
Ziem
  • 6,059
  • 7
  • 49
  • 83
TheFlashes
  • 29
  • 6
  • Type `new Timer.Task` instead of `new Task`. What version of libgdx are you using? By the way, tasks are not for drawing code. I think when its time for the engine to run the task, it will run right before your `render` method is called, so your screen will be immediately cleared after drawing stuff, and you'll never see it. Are you trying to implement a one-frame delay on all your drawing? – Tenfour04 Apr 24 '15 at 20:47

2 Answers2

1

Have some error in AIDE, in line:

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

1.0 / 60.0 return double, but constructor wait float. This works for me:

Timer.schedule(new Timer.Task() { ... }, 1.0f / 60.0f );
RaggaRay
  • 11
  • 4
0

At the top of your code there is no import for this class. It is originally defined inside the Timer directory.

So you need to import this class

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

the final result of your header is this:

...
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;
gibertoni
  • 1,273
  • 12
  • 20
  • When I add `import com.badlogic.gdx.utils.Timer.Task;` working `new Task()` but has trouble with `Timer.` > *Error:(55, 9) error: cannot find symbol variable Timer* – TheFlashes Apr 24 '15 at 19:41
  • Try my second import. It will import everything that Timer has. maybe something else is missing – gibertoni Apr 24 '15 at 19:42
  • Nothing has changed [http://i.imgur.com/cDtzwR1.png](http://i.imgur.com/cDtzwR1.png) – TheFlashes Apr 24 '15 at 19:48
  • I see. It may be that the `*` character does not work as in a regular java application. When you said you used my first import, did you by chance removed any of your previous ones? If so, dont. Try using the instructions here to automatically detect missing imports: http://stackoverflow.com/questions/16615038/what-is-the-shortcut-to-auto-import-all-in-android-studio – gibertoni Apr 24 '15 at 20:03
  • Added these libraries: `import com.badlogic.gdx.utils.Timer;` `import com.badlogic.gdx.utils.Timer.*;` but now ruined the whole timer [http://i.imgur.com/j7fTfdN.png](http://i.imgur.com/j7fTfdN.png) it seems to me that this library is bugged – TheFlashes Apr 24 '15 at 20:14
  • It also does not work [http://i.imgur.com/OVoJsJR.png](http://i.imgur.com/OVoJsJR.png) – TheFlashes Apr 24 '15 at 20:28