开发者

What can I use to track which button was pressed?

I have a button that I touch to enter a game mode. All buttons start the same activity, but the rules and physics are changed depending on what you touch.

I want to track the button pressed so that I can know if people selected Classic mode or Training mode, and set the rules accordingly. How would I do this?

Here is how I start my game modes from the menu:

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);
        }

And here is where it goes:

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

    }
}

Now I need that bundle of extras to be accessible by my Panel class, which is a SurfaceView.


Oh, I got it:

I found a way to do what I wanted in a lot less steps

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

And now I can just access those rules whenever and wherever I want by typing Global.rules

That seems so simple, in hindsight.


You can add an "extra" to your Intents that start your activities.

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

You can then get these extras in your StartGame Activity through:

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

where X is some type of data (e.g., String, int, long, double, etc.).

Update

Sorry, I didn't read your comment closely enough. Here's a shot at something that might work for you:

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
    }
}

And change your Panel constructor to accept a Bundle parameter, so you have those values on initialization. Let me know how that works.


You should use a librairy where all this is done. Otherwise you will have to 1) collect the stats 2) upload them to some place where you can look at the results, this part can be tricky to do by yourself

But google analytics does this all for you already. Maybe others too... Here is the starter page.

Regards, Stéphane


I found a way to do what I wanted in a lot less steps

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

And now I can just access those rules whenever and wherever I want by typing Global.rules

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜