Reputation: 573
I've read so many tutorials and Google documentation on how to implement Google Play Services into my Game, all in how are implemented solely in the Android project. However, LIBGDX has several other packages, namely the core project, where the core Game engine goes. What I would I like to ask help for is: For example, if in my core project, the player finishes the "match" and gets a score, and I would like to relay that score to Google Play Services, how do I call to the MainActivity to handle the "score"?
Upvotes: 0
Views: 378
Reputation: 1203
Let's say you have a "desktop" and "android" version of your game. Create an interface in your "core" project that handles all these Google Play Services events. For instance:
public interface GameEventListener {
// Called when a score is to be submitted
pubic void submitScore(int score);
}
Your game instantiation should now receive an implementation of this interface:
public class MyGame extends Game {
public MyGame(GameEventListener listener) {
// Keep the reference to this listener, you'll be calling it from your game
}
}
Then, you have to make your DesktopLauncher and your AndroidLauncher implement this interface and pass themselves as an argument when you create your game:
DesktopLauncher:
public class DesktopLauncher implements GameEventListener {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = Constants.APP_WIDTH;
config.height = Constants.APP_HEIGHT;
new LwjglApplication(new MyGame(new GameEventListener() {
@Override
public void submitScore(int score) {
// Handle the event as you wish for your desktop version
Gdx.app.log("DesktopLauncher", "submitScore");
}
}), config);
}
}
AndroidLauncher (MainActivity):
public class AndroidLauncher extends AndroidApplication implements GameEventListener {
// ...
@Override
public void onCreate(Bundle savedInstanceState) {
// ... example code not intended to work, take only what you need
// Create the layout
RelativeLayout layout = new RelativeLayout(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// Game view. Pass the activity as the argument to your game class
View gameView = initializeForView(new MyGame(this), config);
layout.addView(gameView);
setContentView(layout);
}
@Override
public void submitScore(int score) {
// Submit your score to Google Play Services
gameServicesClient.submitScore(score);
}
}
Now, whenever your "match" is over, send the message to your listener and it will call your MainActivity's implementation of "submitScore" if you are running the Android version of the game:
private void onMatchOver() {
listener.submitScore(match.getFinalScore());
}
Hope it helps.
Upvotes: 2