我对AndEngine很新。它是一个强大而强大的引擎,然而,有一些令人沮丧的事情,例如无法画圆圈(顺便说一下,有没有办法做到这一点?!)
但我担心的是我有一个棋盘游戏,而我只想展示Toast的消息。我把命令放在scene.attachChild()
内public Scene onCreateScene()
之后,但是当试图运行游戏时,它就会破坏
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;
}
}
它出了什么问题? :
这是logcat
错误
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)
答案 0 :(得分:1)
尝试在UI线程中运行Toast
activity.runOnUiThread(new Runnable(){
@Override
public void run(){
Toast.makeText(activity, "Hello", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:0)
他们使用此Toast
代替:
toastOnUiThread("Your message!", Toast.LENGTH_LONG);