I've been developing a game for android but it is very important to know that I didn't use a single Activity until now. My game is working quite good until now but I want to make use of one activity for some settings because this is way more easy to change.. I've been reading some similar topics but I didn't succeed in my purpose yet. So i need to get an intent to the SettingsActivity coming from a tap on the screen in bounds of 5,5,75,75 as you see in the code.. As I said I've been reading some similar topics and use the code but it didn't work.. Thanks for helping
public class MainMenuScreen extends Screen {
public MainMenuScreen(Game game) {
super(game);
}
@Override
public void update(float deltaTime) {
Graphics g = game.getGraphics();
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_UP) {
if(inBounds(event,5,5,75,75)){
// Get an Intent to open the SettingsActivity
}if(inBounds(event,85,350,200,75)){
System.out.println("Start Last level");
if(SampleGame.progress==0)
Settings.currentLevel=0;
if(SampleGame.progress==1)
Settings.currentLevel=1;
if(SampleGame.progress==2)
Settings.currentLevel=2;
if(SampleGame.progress==3)
Settings.currentLevel=3;
if(SampleGame.progress==4)
Settings.currentLevel=4;
Settings.save(game.getFileIO());
GameScreen.state=GameState.Ready;
game.setScreen(new GameScreen(game));
}if(inBounds(event,500,350,275,75)){
game.setScreen(new LevelSelectionScreen(game));
}
}
}
}
private boolean inBounds(TouchEvent event, int x, int y, int width,
int height) {
if (event.x > x && event.x < x + width - 1 && event.y > y
&& event.y < y + height - 1)
return true;
else
return false;
}
@Override
public void paint(float deltaTime) {
Graphics g = game.getGraphics();
g.drawImage(Assets.menu, 0, 0);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
@Override
public void backButton() {
android.os.Process.killProcess(android.os.Process.myPid());
}
}
From the code I can see you are following the book Beginning Android Games
. This method (single activity with "screens") indeed works great for game development.
If you do want to add extra activities it would be best to implement their starting/handling inside the Game
(AndroidGame
) class, which implements the main activity.
In order to do this you could add a newActivity
method inside this (in the same place as the setScreen
methods). It's parameters can vary depending on what you need: you could pass an Intent directly, or you could pass a Class instead. It all depends on how much freedom you want when re-using the framework.
Here is an basic example of what you can add:
public abstract class AndroidGame extends Activity implements Game {
public void newActivity(Intent intent) {
startActivity( intent );
}
public void newActivity(Class<?> cls) {
startActivity( new Intent( this, cls ) );
}
}
These are just two ways to achieve your goal, and they are both very basic. Have a look at the Activity and Intent documentation for the various ways in which you can launch activities. Additionally, if you want to receive info back from the activity you can use startActivityForResult and implement onActivityResult in the Game
class as well (maybe with the option to store the last result so it can be processed by your game.