Athos
Athos

Reputation: 681

Need gameover activity

I'm new to android, and I'm trying my best to create a game. I'm trying to create a gameover screen for my game, and I though the best way to do that was to create an activity for a gameover screen. However, I'm not sure how to go about doing this.The problem seems to be that I cannot create an intent anywhere other than the activity class. So I can't see when the game should end, and then create a new activity without it being in the activity class. So I'm having trouble connecting my game model to my activity class, so that when the player dies, it would trigger a gameover activity. Where in my activity class should I put this information?

Upvotes: 3

Views: 2267

Answers (2)

Ribose
Ribose

Reputation: 2233

You would place code such as (assuming your activity is GameOverScreen extends Activity):

Intent gameOverScreen = new Intent(this, GameOverScreen.class);
startActivity(gameOverScreen);

To do so from outside the activity (since startActivity() is a public method of the Activity class), you'd just do like this, using the instance of your game activity that I call gameScreen in this example:

Intent gameOverScreen = new Intent(gameScreen, GameOverScreen.class);
gameScreen.startActivity(gameOverScreen);

You should probably have a reference to the instance of your game activity stored to the model as a Context object at least for resources.

Upvotes: 1

Nikola Despotoski
Nikola Despotoski

Reputation: 50588

  </activity>
             <activity android:name=".gameoveractivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="name-of-activity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

Add this in your Manifest.xml...

Then from the code invoke this activity like this

  Intent i = new Intent("name-of-activity-you-declared-in-the-manifest");
          this.startActivity(i);

I guess you should put this after reaching conditions for game over. Let's say after loosing 3 lives, you get game over activity.

If you are doing game over with something like TRY AGAIN. Then you should use this.startActivityForResult(i, result). And then, override the onActivityResult() method in the class from where you call the game over activity.

Upvotes: 0

Related Questions