8 Steps for intergrating Scoreloop with Libgdx

Scoreloop is really a must-have for your Android games. While it’s a piece of cake to use with plain Android, it took me ages to implement it with Libgdx. This tutorial is inspirerd by this one

1. Sign up for a free Scoreloop acount and download the sdk
2. Add the scoreloop-core.jar to the Android project
3. Create a new interface in the Libgdx project:


 public interface ActionResolver {
 public void bootstrap();
 public void showScoreloop();
 public void submitScore(int mode, int score);
 public void refreshScores();
 }
 

4. Create a new class in the Android Project


 public class GameApplication extends Application {
 //Declare the Scoreloop Client
 private static Client client;
 static void init(final Context android_game_context) {
 if (client == null) {
 client = new Client(android_game_context,
 "Your scoreloop key", null);
 }
 }
@Override
 public void onCreate() {
 super.onCreate();
 init(this);
 ScoreloopManagerSingleton.init(this,
 "Your scoreloop key");
 }
@Override
public void onTerminate() {
super.onTerminate();
ScoreloopManagerSingleton.destroy();
}
}

5. Create annother class in the Android Project:


 public class ScoreloopHandler {
 private GameAndroidActivity mContext; //Your class here which extends AndroidApplication
 public ScoreloopHandler(GameAndroidActivity context) {
 mContext = context;
 }
public void submitScore(int level, int scoreValue) {
 final Score score = new Score((double) scoreValue, null);
 score.setLevel(level);
 final ScoreController myScoreController = new ScoreController((RequestControllerObserver) new ScoreSubmitObserver());
 myScoreController.submitScore(score);
 }
public void getRankingForScore(int scoreValue) {
 Score score = new Score((double) scoreValue, null);
 RankingController controller = new RankingController(new RankingRequestObserver());
 controller.loadRankingForScore(score);
 }
public void getGlobalHighscores() {
 ScoresController myScoresController = new ScoresController(new GlobalRankObserver());
 myScoresController.setSearchList(SearchList.getGlobalScoreSearchList());
 myScoresController.setRangeLength(15);
 myScoresController.loadRangeForUser(Session.getCurrentSession().getUser());
 }
public void getTodayHighscores() {
 ScoresController myScoresController = new ScoresController(new DailyRankObserver());
 myScoresController.setSearchList(SearchList.getTwentyFourHourScoreSearchList());
 myScoresController.setRangeLength(15);
 myScoresController.loadRangeForUser(Session.getCurrentSession().getUser());
 }
private class RankingRequestObserver implements RequestControllerObserver {
 @Override
 public void requestControllerDidFail(RequestController arg0,
 Exception arg1) {
 }
@Override
 public void requestControllerDidReceiveResponse(RequestController controller) {
 Ranking ranking = ((RankingController) controller).getRanking();
 final int rank = ranking.getRank();
 mContext.postRunnable(new Runnable() {
 @Override
 public void run() {
 //This code runs on the "libgdx" thread.
 //Use a local variable to store the rank in the MainProject and set it here
 }
 });
 }
 }
private class ScoreSubmitObserver implements RequestControllerObserver {
@Override
 public void requestControllerDidFail(final RequestController requestController,
 final Exception exception) {
 Log.v("Scoreloop", "score submitted exception " + exception.getMessage());
 }
@Override
 public void requestControllerDidReceiveResponse(final RequestController requestController) {
 Log.v("Scoreloop", "score submitted successsfully");
}
 }
private class DailyRankObserver implements RequestControllerObserver {
@Override
 public void requestControllerDidFail(RequestController arg0,
 Exception arg1) {
}
@Override
 public void requestControllerDidReceiveResponse(RequestController controller) {
 final List retrievedScores = ((ScoresController) controller).getScores();
 mContext.postRunnable(new Runnable() {
 @Override
 public void run() {
 //This code runs on the "libgdx" thread.
 //Use a local variable to store the daily ranks in the MainProject and set it here
 }
 });
 }
 }
private class GlobalRankObserver implements RequestControllerObserver {
@Override
 public void requestControllerDidFail(RequestController arg0,
 Exception arg1) {
}
@Override
public void requestControllerDidReceiveResponse(RequestController controller) {
final List retrievedScores = ((ScoresController) controller).getScores();
mContext.postRunnable(new Runnable() {
@Override
public void run() {
//This code runs on the "libgdx" thread.
//Use a local variable to store the global ranks in the MainProject and set it here
}
});
}
}
}


6. Let the AndroidActivity class implement ActionResolver and add a Handler to bind the main thread:


public class GameAndroidActivity extends AndroidApplication implements
ActionResolver {
protected ActionResolver actionresolver;
Handler uiThread;
QuestionGameAndroidActivity mContext;
ScoreloopHandler handler;
uiThread = new Handler();
handler = new ScoreloopHandler(mContext);

7. Let the main Game (in your main project) recieve and instance of actionResolver:


public MainGame(ActionResolver resolver) {
this.actionResolver = resolver;
...
if (actionResolver != null) {
actionResolver.bootstrap();
}
}
public void saveScore(int mode, int score) {
 // Submit to score loop
 if (actionResolver != null)
 actionResolver.submitScore(mode, score);
 }
public void showScoreloop() {
if (actionResolver != null) {
actionResolver.showScoreloop();
}
}
}


8. Finally, update the Manifest so that it registers the AndroidApplication:


application android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:name="GameApplication" android:debuggable="true"
</activity

Advertisements

9 thoughts on “8 Steps for intergrating Scoreloop with Libgdx

  1. it says ScoreLoopManagerSingleton cannot be resolved,telling me to create a class of that name first… 😐

  2. I think yo only need use:
    Client.init(this, secret, null);
    instead of ScoreloopManagerSingleton.init(this, “Your scoreloop key”);
    For some reason there is not ScoreloopManagerSingleton in the ui package.

    you should check the class simpleapplication.java in the demo apps of the documentation

  3. Pingback: Scoreloop hangs on “connecting” when displayin higscores on Android video

    • To be more specific – I used your example, I can run the game, but when I start the game, it gets stuck on “connecting” message. I don’t know, what’s the problem…

      • I haven’t used scoreloop for a while, but I can give you an example app. Send an e-mail to reneb83 [at] hotmail [dot] com and I’ll reply.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s