0

I'm very new to AndEngine. It's a good and powerful engine, however, there has been couple of frustrations such as not being able to draw circles (btw, is there any ways to do that?!)

But my concern is that I have a board game, and I simply want to show a Toast message. I put the command after all scene.attachChild() inside public Scene onCreateScene(), but when trying to run the game, it ruins

public class MainActivity extends SimpleBaseGameActivity {
    private static int CAMERA_WIDTH ;
    private static int CAMERA_HEIGHT;
    //  private int numRows= 8;
    //  private int numColumns= 10;

    private Font mFont;
    private ITexture mFaceTexture;
    private ITextureRegion mFaceTextureRegion;

    private ITexture boardTexture;
    private ITextureRegion boardTextureRegion;

    @Override
    public EngineOptions onCreateEngineOptions() {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        CAMERA_WIDTH = metrics.widthPixels;
        CAMERA_HEIGHT = metrics.heightPixels;

        final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
    }

    @Override
    public void onCreateResources() throws IOException {
        this.mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 16);
        this.mFont.load();

        this.boardTexture = new AssetBitmapTexture(this.getTextureManager(), this.getAssets(), "board.png");
        this.boardTextureRegion = TextureRegionFactory.extractFromTexture(this.boardTexture);
        this.boardTexture.load();

        this.mFaceTexture = new AssetBitmapTexture(this.getTextureManager(), this.getAssets(), "GraduationIcon.png");
        this.mFaceTextureRegion = TextureRegionFactory.extractFromTexture(this.mFaceTexture);
        this.mFaceTexture.load();


    }

    @Override
    public Scene onCreateScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());
        final Scene scene = new Scene();
        scene.getBackground().setColor(Color.BLACK);

        /* Create the rectangles. */
        final Sprite board = new Sprite(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, this.boardTextureRegion, this.getVertexBufferObjectManager());
        board.setScale(.5f);


        /* Create the sprite and add it to the scene. */
        final Sprite face = new Sprite(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, this.mFaceTextureRegion, this.getVertexBufferObjectManager());

        /* draw a line */
        float x1=CAMERA_WIDTH/2;
        float y1=CAMERA_HEIGHT/2;
        float x2=CAMERA_WIDTH *7/8;
        float y2=CAMERA_HEIGHT /2;
        Random random = new Random(123456789);
        float lineWidth=2;

        final Line line = new Line(x1+20, y1, x2, y2, lineWidth, this.getVertexBufferObjectManager());
        line.setColor(random.nextFloat(), random.nextFloat(), random.nextFloat());

        /* draw a text   */
        final Text text = new Text(x2, y2 , this.mFont, "Week when you are graduated\n (you are 50!)", new TextOptions(HorizontalAlign.CENTER), this.getVertexBufferObjectManager());

        /*add graphics to the scene  */
        scene.attachChild(board);
        scene.attachChild(face);
        scene.attachChild(line);
        scene.attachChild(text);
        Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
        return scene;

    }

}

What is wrong with it? :?

And here is the logcat error

07-14 10:14:22.301: E/AndroidRuntime(21709): FATAL EXCEPTION: Thread-3085
07-14 10:14:22.301: E/AndroidRuntime(21709): Process: com.example.hello, PID: 21709
07-14 10:14:22.301: E/AndroidRuntime(21709): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
07-14 10:14:22.301: E/AndroidRuntime(21709):    at android.os.Handler.<init>(Handler.java:200)
07-14 10:14:22.301: E/AndroidRuntime(21709):    at android.os.Handler.<init>(Handler.java:114)
07-14 10:14:22.301: E/AndroidRuntime(21709):    at android.widget.Toast$TN.<init>(Toast.java:344)
07-14 10:14:22.301: E/AndroidRuntime(21709):    at android.widget.Toast.<init>(Toast.java:100)
07-14 10:14:22.301: E/AndroidRuntime(21709):    at android.widget.Toast.makeText(Toast.java:258)
07-14 10:14:22.301: E/AndroidRuntime(21709):    at com.example.hello.MainActivity$1.run(MainActivity.java:143)
07-14 10:14:22.301: E/AndroidRuntime(21709):    at java.lang.Thread.run(Thread.java:818)
Tina J
  • 4,983
  • 13
  • 59
  • 125

2 Answers2

1

Try running Toast in UI thread

activity.runOnUiThread(new Runnable(){
    @Override
    public void run(){
        Toast.makeText(activity, "Hello", Toast.LENGTH_LONG).show();
   }
}
Plugie
  • 1,289
  • 17
  • 25
0

They use this Toast instead:

toastOnUiThread("Your message!", Toast.LENGTH_LONG);
Tina J
  • 4,983
  • 13
  • 59
  • 125