發現,開始一個新的意圖可能不通知GAMEOVER的用戶以正確的方式後,我現在runOnUiThread,Runnable接口和對話框struggeling ..顯示對話框中的遊戲用帆布
問:如何以及在哪裏我會實現一個Dialog.show()來通知用戶遊戲已結束?在我的日誌中,它一切正常(所以我設法讓消息遊戲結束,當它應該)。在某個時候,布爾GameManger.LevelEnded切換爲true。當發生這種情況時,我想顯示對話框。
我嘗試過使用runOnUiThread,Runnable和Async線程。無法弄清楚。
我有一個遊戲。活動AppelzActivity:
public class AppelzActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = AppelzActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
// requesting to turn the title OFF
requestWindowFeature(Window.FEATURE_NO_TITLE);
// making it full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set our MainGamePanel as the View
setContentView(new MainGamePanel(this));
Log.d(TAG, "View added");
}
}
我MainGamePanel類:
public class MainGamePanel extends SurfaceView implements
SurfaceHolder.Callback {
// DECLARING VARS (REMOVED FOR READABILITY)
public MainGamePanel(Context context) {
super(context);
getHolder().addCallback(this);
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
GameManager.panel = this;
width = display.getWidth();
height = display.getHeight();
RectScreen.set(0,0,width,height);
// CREATING CONTROLS, MISSION AND VISIBLE OBJECTS (REMOVED FOR READABILITY)
thread = new MainThread(getHolder(), this);
setFocusable(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread.setRunning(true);
}
// HANDELING ON TOUCH EVENTS AND SUCH (REMOVED FOR READABILITY)
@Override
protected void onDraw(Canvas canvas) {
// fills the canvas with black
canvas.drawColor(Color.BLACK);
backGnd.draw(canvas);
basket.draw(canvas);
for (int i = 0; i < AppleList.size();i++){
AppleList.get(i).draw(canvas);
}
btnMoreFruit.draw(canvas);
btnLessFruit.draw(canvas);
if (GameManager.count){
glyphsCount.drawString(MainThread.canvas, Integer.toString(GameManager.i), (width/2-120), (height/2-120));
}
}
}
最後,MainThread類:
public class MainThread extends Thread {
private static final String TAG = MainThread.class.getSimpleName();
private SurfaceHolder surfaceHolder;
private MainGamePanel gamePanel;
private DialogManager dialogManager;
public static boolean running;
public static Canvas canvas;
public void setRunning(boolean running) {
MainThread.running = running;
}
public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) {
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
this.dialogManager = dialogManager;
}
@Override
public void run() {
// Canvas canvas;
Log.d(TAG, "Starting game loop");
while (running) {
MainThread.canvas = null;
// try locking the canvas for exclusive pixel editing on the surface
try {
MainThread.canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
// update game state
this.gamePanel.update();
// render state to the screen
// draws the canvas on the panel
this.gamePanel.onDraw(MainThread.canvas);
}
} finally {
// in case of an exception the surface is not left in
// an inconsistent state
if (MainThread.canvas != null) {
surfaceHolder.unlockCanvasAndPost(MainThread.canvas);
}
} // end finally
}
}
}
Basket.java - 這是在遊戲中的對象。正如你所看到的,如果它是在一定的位置上,我開始倒計時:
public class basket {
private Bitmap bitmap; // the actual bitmap
private int x; // the X coordinate
private int y; // the Y coordinate'
public static int width;
public static int height;
public float X = 100;
public float Y = 100;
private boolean touched; // if droid is touched/picked up
public basket(Bitmap bitmap, int x, int y) {
this.bitmap = bitmap;
width = bitmap.getWidth();
height = bitmap.getHeight();
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
public int getWidth(){
return bitmap.getWidth();
}
public int getHeight(){
return bitmap.getHeight();
}
public int getX() {
return x;
}
public void setX(float x) {
// if basket is further than slotmargin
if (x + width/2 > SlotManager.SlotmarginX) {
// if basket is NOT in CheckOut area
if (this.Y < MainGamePanel.height - 80 - height/2){
x = SlotManager.SlotmarginX - width/2;
} else {
// if basket IS in CheckOut area
x = MainGamePanel.width - width/2;
// Start the countdown timer if not already running
if (!GameManager.count){
GameManager.count=true;
// --- HERE I START COUNTDOWN
GameManager.handler.postDelayed(GameManager.runnable, 0);
}
}
} else {
// Basket is NOT in CheckOut area; stop countdown timer
GameManager.count=false;
}
this.X = x;
}
public int getY() {
return y;
}
public void setY(int y) {
if (y + height/2 > MainGamePanel.height - 80){
y = MainGamePanel.height - 80 - height/2;
}
this.Y = y;
}
public boolean isTouched() {
return touched;
}
public void setTouched(boolean touched) {
this.touched = touched;
}
public void draw(Canvas canvas) {
canvas.drawBitmap(bitmap, X - width/2, Y - height/2, null);
}
public void handleActionDown(int eventX, int eventY) {
if (eventX >= (X - width/ 2) && (eventX <= (X + width/2))) {
if (eventY >= (Y - height/ 2) && (eventY <= (Y + height/ 2))) {
// basket touched
setTouched(true);
} else {
setTouched(false);
}
} else {
setTouched(false);
}
}
}
在遊戲管理倒計時:
@Override
public void run()
{
if (SlotManager.alSlottedFruitList.size() > 0){
//TODO Create Full screen countdown timer
Log.d("", "Countdown Timer in GameManager " + i);
i--;
if (count && i > -1) // stop timer when count is false
{
handler.postDelayed(runnable, 1000);
} else {
if (i == -1){
Log.d("", "RoundUp the Level");
// --- HERE I WANT TO DO A LEVEL END AND SHOW THE DIALOG
}
// reset Countdown (if basket outside checkout)
i = 6;
}
} else {
// reset Countdown (if slot has no fruits)
i = 6;
}
}
};
public static void startCountDown() {
handler.postDelayed(GameManager.runnable, 0);
}
public static void finishRound() {
listener.onRoundFinished();
}
public static List<Integer> missionlist;
public static void CreateMission(){
int maxtotalitems = 6;
missionlist = new ArrayList<Integer>();
Random Rnd = new Random();
for (int FruitType = 0; FruitType < FruitManager.fruitResources.size();FruitType++){
for (int x = 0; x < Rnd.nextInt(maxtotalitems); x++){
missionlist.add(FruitType);
}}
// --- HERE I WANT TO SHOW A DIALOG CONTAINING THE MISSION
for (int x = 0; x < FruitManager.fruitResources.size();x++){
Log.d("MISSION", "Mission for " + x + " : " + Collections.frequency(missionlist, x));
}
// Let's show the Dialog containing the mission
}
public void LevelEnd(){
// First, count the Fruits in the slot
SlotManager.countSlotInv();
List<Integer> list = new ArrayList<Integer>();
// Fill itemIds with the types of fruits
for (apple a : SlotManager.alSlottedFruitList){
Integer itemId = Integer.valueOf(a.FruitType);
list.add(itemId);
}
for (int key = 0; key < 3 ; key++) {
boolean succeed = CheckMissionComplete(key, Collections.frequency(list, key));
System.out.println(key + " : " + Collections.frequency(list, key));
Log.d("MISSION", "Number : " + key + " : succeed = " + succeed);
}
listener.onRoundFinished();
}
public boolean CheckMissionComplete(int key, int amount) {
//CheckMission
if (amount == Collections.frequency(missionlist, key)){
return true;
} else {
return false;
}
}
public GameManager(GameListener listener) {
this.listener = listener;
}
}
你可以請註明您要對話框正是 – Sameer 2012-04-27 05:29:06
薩米爾一條線,我已經添加了類籃。這是遊戲中的一個對象。如果你把籃子放在特定的位置,倒計時開始。如果倒計時完成(5秒),我開始一個例程來檢查用戶對特定任務的輸入。然後,我想顯示對話框。提前致謝! – 2012-04-27 06:44:03
我想你是從Thread調用帖子無效,所以你不能顯示對話框。爲了顯示對話框,基本需求是活動上下文。第二,它可以直接在線程內部顯示,以便當條件滿足時從表面視圖類中選擇活動的上下文,然後取出一個處理程序,然後顯示對話框。 – Sameer 2012-04-27 07:35:53