Swarm Documentation API Reference

Super Jumper - A LibGDX Example


              

SuperJumper is a simple jumping game (powered by LibGDX) in which players get points by collecting coins as they guide their jumper up the game map by bouncing on platforms and springs while dodging enemies.

This guide shows you how to integrate leaderboards and achievements into a LibGDX-based game. It is intended for developers who are already comfortable with the basics of LibGDX and are looking to improve their game by adding social features.

This LibGDX example game has been optimized for mdpi and hdpi devices with screen sizes between 3 and 5 inches. If you'd like to see the final product before following this guide, you can download the complete source code (includes compiled apk). By downloading the complete source code you are agreeing to the Terms and Conditions.

Prerequisites



Import Super Jumper LibGDX Starting Source

  • Import superjumper, superjumper-android, and SwarmConnect/library
  • Do a "Clean" and then "Build Project" on SwarmConnect.
  • Do a "Clean" and then "Build Project" on superjumper-android.


Link Library and Jar

  • Link the superjumper-android project to the SwarmConnect library by going to superjumper-android's Properties > Android > Add... > Choose Swarmconnect/library > Click Apply > Click OK
  • Link the superjumper project to the Swarm.jar by going to superjumper's Properties > Java Build Path > Libraries Tab > Add JARs... > Choose SwarmConnect/libs/Swarm.jar > Click OK
  • Do a "Clean Build Project" on ONLY superjumper
  • Do a "Clean Build Project" on ONLY superjumper-android
  • Finally, if anything gets goofy during this linking process, try closing and re-opening projects, doing clean builds, or re-importing and re-linking.


Merge Manifests

Add the following line to the bottom of project.properties file in superjumper-android. This automatically merges the SwarmConnect manifest into the superjumper-android manifest when you build. Make sure you're running ADT version 20.0.1 or it won't work (we highly recommend ADT version 21.0.1 or newer).
manifestmerger.enabled=true

Create App in Swarm Admin Panel

Create a new app called "Super Jumper Demo" via the Swarm admin panel. Then go to the App Details tab and write down your App ID and App Key. You'll need these very soon.


Activate and Initialize Swarm

Add calls to Swarm.setActive, Swarm.init, and Swarm.setInactive to superjumper-android/src/com.badlogicgames.superjumper/SuperJumperAndroid.java.
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initialize(new SuperJumper(), false);
    
    // Add this method call
    Swarm.setActive(this);
}

// Add everything below here too
public void onResume() {
    super.onResume();
    Swarm.setActive(this);
    
    // Replace MY_APP_ID with your App ID from the Swarm Admin Panel
    // Replace MY_APP_KEY with your string App Key from the Swarm Admin Panel
    Swarm.init(this, MY_APP_D, "MY_APP_KEY");
}

public void onPause() {
    super.onPause();
    Swarm.setInactive(this);
}


Setup Leaderboard

1. Define Leaderboard in Swarm Admin Panel
Set up one leaderboard via the Swarm Admin Panel. Name the leaderboard "Best Jumpers", set it to keep the Best 5 Scores per User, and leave all other values at their defaults. Record your Leaderboard ID number as you'll need it soon. For more information about Swarm leaderboards, click here.

2. Submit Score to Leaderboard
To submit scores to the leaderboard, update superjumper/com.badlogicgames.superjumper/GameScreen.java as follows (near line 155).
if (world.state == World.WORLD_STATE_GAME_OVER) {
    state = GAME_OVER;
    // if (lastScore >= Settings.highscores[4])
    //  scoreString = "NEW HIGHSCORE: " + lastScore;
    // else
    //  scoreString = "SCORE: " + lastScore;
    // Settings.addScore(lastScore);
    // Settings.save();
    
    // Submit score to the leaderboard
    // Replace MY_LEADERBOARD_ID with your Leaderboard ID from the Swarm Admin Panel
    SwarmLeaderboard.submitScore(MY_LEADERBOARD_ID, (float) lastScore);
}
3. Show Leaderboard
Update the High Scores button on the game's home screen to show the global, online leaderboards instead of the local leaderboards. Modify superjumper/com.badlogicgames.superjumper/MainMenuScreen.java as follows (near line 66).
if (OverlapTester.pointInRectangle(highscoresBounds, touchPoint.x, touchPoint.y)) {
    Assets.playSound(Assets.clickSound);
    
    // game.setScreen(new HighScoresScreen(game));
    
    // Show the leaderboard when someone taps on High Scores
    Swarm.showLeaderboards();
    
    return;
}


Setup Achievements

1. Define Achievements in the Admin Panel
Set up three achievements via the Swarm Admin Panel. Title them "Score 50+ Points", "Score 100+ Points", and "Score 200+ Points". Feel free to write some fun descriptions too! Record all 3 of your Achievement ID numbers as you'll need them soon. For more information about Swarm achievements, click here.

2. Unlock Achievements
To setup the Achievement unlocking in code, go back and update superjumper/com.badlogicgames.superjumper/GameScreen.java as follows.
if (world.state == World.WORLD_STATE_GAME_OVER) {
    state = GAME_OVER;
    // if (lastScore >= Settings.highscores[4])
    //  scoreString = "NEW HIGHSCORE: " + lastScore;
    // else
    //  scoreString = "SCORE: " + lastScore;
    // Settings.addScore(lastScore);
    // Settings.save();
    
    // Submit score to the Leaderboard
    // Replace MY_LEADERBOARD_ID with your Leaderboard ID from the Swarm Admin Panel
    SwarmLeaderboard.submitScore(MY_LEADERBOARD_ID, (float) lastScore);
    
    
    // Unlock Achievements
 
    // Replace MY_50_PLUS_ACHIEVEMENT_ID with the 
    // Achievement ID for "Score 50+ Points" from the Swarm Admin Panel
    if (lastScore >= 50)  SwarmAchievement.unlock(MY_50_PLUS_ACHIEVEMENT_ID);
    
    // Replace MY_100_PLUS_ACHIEVEMENT_ID with the 
    // Achievement ID for "Score 100+ Points" from the Swarm Admin Panel    
    if (lastScore >= 100) SwarmAchievement.unlock(MY_100_PLUS_ACHIEVEMENT_ID);
    
    // Replace MY_200_PLUS_ACHIEVEMENT_ID with the 
    // Achievement ID for "Score 200+ Points" from the Swarm Admin Panel
    if (lastScore >= 200) SwarmAchievement.unlock(MY_200_PLUS_ACHIEVEMENT_ID);
}


Show Dashboard

The dashboard enables players to easily navigate through the game's social features. For the purposes of this example, just connect the dashboard to the Help button. Modify superjumper/com.badlogicgames.superjumper/MainMenuScreen.java as follows (near line 73).
if (OverlapTester.pointInRectangle(highscoresBounds, touchPoint.x, touchPoint.y)) {
    Assets.playSound(Assets.clickSound);
    
    // game.setScreen(new HelpScreen(game));
    
    // Show the dashboard when someone taps on Help
    Swarm.showDashboard();
    
    return;
}


Congratulations!

You're all done! You should be able to compile and run your new Super Jumper game, complete with Swarm-enabled social features! You can also download the finished complete source code (includes compiled apk). By downloading the finished complete source code you are agreeing to the Terms and Conditions. Note that you'll need to create a Swarm account and get an App Id, App Key, along with the Leaderboard and Achievement ID numbers to make the source code compile and run. However, no additional work is needed if you just want to run the apk and see what happens.

Thanks for following the Super Jumper - LibGDX Example! If you have any questions or comments, we'd love to hear your thoughts. Please send all feedback through the Swarm Support Center.

Looking for more clever ideas to make your game social? Check out the Pro Tips!