2011-06-06 68 views
1

我有一個按鈕,我觸摸進入遊戲模式。所有按鈕都會啓動相同的活動,但規則和物理會根據您所觸摸的內容進行更改。我可以用什麼來追蹤哪個按鈕被按下?

我想追蹤按鈕,以便我可以知道人們是否選擇了經典模式或訓練模式,並相應地設置規則。我將如何做到這一點?

這裏是我從菜單中開始我的遊戲模式:

MenuElement classic = mElements.get(0); 
    MenuElement training = mElements.get(1); 

     if(touchX > classic.mX && touchX < classic.mX + classic.mBitmap.getWidth() 
      && touchY > classic.mY && touchY < classic.mY + classic.mBitmap.getHeight()) 
     { 
      aux = "Starting game"; 
      Context context = com.Juggle2.Menu.this.getContext(); 
      Intent intent = new Intent(context, StartGame.class); 
      intent.putExtra("rule", 1); 
      context.startActivity(intent); 
     } 

     if(touchX > training.mX && touchX < training.mX + classic.mBitmap.getWidth() 
      && touchY > training.mY && touchY < training.mY + training.mBitmap.getHeight()) 
     { 
      aux = "Starting training"; 
      Context context = com.Juggle2.Menu.this.getContext(); 
      Intent intent = new Intent(context, StartGame.class); 
      intent.putExtra("rule", 2); 
      context.startActivity(intent); 
     } 

這裏是哪裏就有奇蹟:

public class StartGame extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(new Panel (this));  //Start the game 

    } 
} 

現在我需要額外的那個包是我的小組訪問類,這是一個SurfaceView。


哦,我知道了:

我找到了一種方法做我想要的少了很多步驟

public class Global{ 
    public static int rules = 0; 
} 

而且現在就可以訪問這些規則,無論何時何地我希望通過輸入Global.rules

這看起來很簡單,事後看來。

回答

0

我找到了一種方法做我想要的少了很多步驟

public class Global{ 
    public static int rules = 0; 
} 

而且現在就可以訪問這些規則,無論何時何地我想通過鍵入Global.rules

0

你應該在所有這些完成後使用圖書館。否則,你將不得不 1)收集統計 2)將它們上傳到一些地方,你可以看一下結果,這部分可能會非常棘手自行

做不過Google Analytics(分析)已經這樣做都是爲了你。也許其他人... 這裏是starter page

問候, 斯特凡

+0

我不想跟蹤按下按鈕以達到度量目的,我想跟蹤按鈕,以便我可以知道人們是否選擇了「經典」模式或「訓練」模式,並相應地設置規則 – Houseman 2011-06-06 19:49:59

2

您可以添加一個「額外」你Intent s表示啓動活動。

Intent intent = new Intent(context, StartGame.class); 
intent.putExtra(String key, X value); 
startActivity(intent); 

然後,您可以通過獲得您的StartGame活動這些額外:

Bundle extras = getIntent().getExtras(); 
X value = extras.getX(String key); 

其中X是某種類型的數據(例如,字符串,整數,長,雙等)。

更新

對不起,我沒看過您的評論不夠緊密。這裏有一個射擊的東西,可能會爲你工作:

public class StartGame extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    Bundle extras = getIntent().getExtras(); 
    Panel mPanel = new Panel(this, extras); 

    setContentView(mPanel);  //Start the game 
    } 
} 

,改變你的面板構造函數接受一個Bundle參數,所以你必須在初始化這些值。讓我知道這是如何工作的。

+0

謝謝,但我的活動只是啓動SurfaceView,這是我需要這些變量的地方。我怎樣才能將這些變量進一步傳遞給一個類? – Houseman 2011-06-06 19:59:04

+0

編輯原始答案 – 2011-06-06 20:08:30

+0

再次謝謝,但我沒有通過一個活動進一步。該活動僅將內容視圖設置爲我的SurfaceView。我會發布我的活動,向你展示我在說什麼。 *編輯*好的,我用更多的信息編輯了我的問題。我希望這些附加內容最終可以被我的Panel類訪問 – Houseman 2011-06-06 20:10:24