我已经阅读了很多关于如何在我的游戏中实施Google Play服务的教程和Google文档,所有内容都是如何在Android项目中实现的。但是,LIBGDX还有其他几个软件包,即核心项目,核心游戏引擎。 我想要求帮助的是: 例如,如果在我的核心项目中,玩家完成“匹配”并获得分数,并且我想将该分数转发给Google Play服务,我如何调用MainActivity来处理“分数”?
答案 0 :(得分:2)
假设你有一个游戏的“桌面”和“机器人”版本。在“核心”项目中创建一个处理所有这些Google Play服务事件的界面。例如:
public interface GameEventListener {
// Called when a score is to be submitted
pubic void submitScore(int score);
}
您的游戏实例化现在应该会收到此界面的实现:
public class MyGame extends Game {
public MyGame(GameEventListener listener) {
// Keep the reference to this listener, you'll be calling it from your game
}
}
然后,你必须让你的DesktopLauncher和你的AndroidLauncher实现这个界面,并在你创建游戏时将自己作为参数传递:
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);
}
}
现在,只要您的“匹配”结束,将消息发送给您的听众,如果您正在运行Android游戏版本,它将调用您的MainActivity的“submitScore”实现:
private void onMatchOver() {
listener.submitScore(match.getFinalScore());
}
希望它有所帮助。