A tester animation bondissant : https://happycoding.io/tutorials/libgdx/hello-world
Dimensionner le jeux pour les écrans android :
1°) Définir les coordonnées de l’écran virtuel :
Dans la classe principale (exemple MyGdxGame) définir la largeur et hauteur
public class MyGdxGame extends Game { public final static int WIDTH = 800; public final static int HEIGHT = 480;
2°) Dans les écrans du jeu :
private Stage stage;
Dans gameScreen
stage = new Stage();
Infinite Background déplacement :
Déclarer
//-- Infinite move final int BACKGROUND_MOVE_SPEED = 100; float xMax, xCoordBg1, xCoordBg2;
//-- Fond d'écran Background = new Texture(Gdx.files.internal("underwater.png")); Background2 = new Texture(Gdx.files.internal("underwater.png")); xMax = 800; xCoordBg1 = xMax*(-1); xCoordBg2 = 0;
Dans Render
xCoordBg1 += BACKGROUND_MOVE_SPEED * Gdx.graphics.getDeltaTime(); xCoordBg2 = xCoordBg1 + xMax; if (xCoordBg1 >= 0) { xCoordBg1 = xMax*(-1); xCoordBg2 = 0; }
et
game.batch.draw(Background, xCoordBg1, 0); game.batch.draw(Background2, xCoordBg2,0);
Ne pas oublier
Background.dispose(); Background2.dispose();
Avec des ressources packagées avec TextureAtlas :
changer
TextureAtlas.AtlasRegion Background, Background2;
Et ajouter :
public static TextureAtlas atlas; public static TextureRegion road;
atlas = new TextureAtlas(Gdx.files.internal("images.atlas")); road = atlas.findRegion("road"); Background = atlas.findRegion("road"); Background2 = atlas.findRegion("road");
Références :
A) Préférences :
Créez une classe « Prefs » afin d’obtenir les valeurs et de mettre à jour les préférences
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Preferences; public class Prefs { private Preferences pref; private boolean hasSound; private int NbDrops; private int completedLevel; public Prefs() { pref = Gdx.app.getPreferences("Preferences"); hasSound = pref.getBoolean("hasSound", true); NbDrops = pref.getInteger("NbDrops", 0); completedLevel = pref.getInteger("level", 0); } public void setSound(boolean hasSound) { this.hasSound = hasSound; pref.putBoolean("hasSound", hasSound); pref.flush(); } public void setDrops(int NbD) { this.NbDrops = NbD; pref.putInteger("NbDrops", NbD); pref.flush(); } public int getDrops() { return (NbDrops); } public boolean hasSound() { return hasSound; } //should be called once when we need to increase my level public void increaseLevel() { completedLevel++; pref.putInteger("level", completedLevel); pref.flush(); } public int getLevel() { return completedLevel; } }
Dans chaque classe du jeux pour récupérer ou mettre à jour les valeurs :
public class GameScreen implements Screen { final MyGdxGame game; public Prefs prefs;
Ecriture :
prefs.setDrops(dropsGathered);
Lecture :
dropsGathered=prefs.getDrops();
https://stackoverflow.com/questions/42986129/libgdx-android-preferences-how-to-save-data-to-app
B) Fond d’écran :
Texture Background;
Background = new Texture(Gdx.files.internal("fondgrotte.jpg"));
game.batch.draw(Background,0,0);
C) Déplacer les objets plus rapidement
https://stackoverflow.com/questions/40682264/moving-faster-in-libgdx
E) Internationalization :
- Créez un répertoire i18n dans le dossier assets
- dans le répertoire créer des fichiers déclinés comme suit :
- MyBundle.properties (défaut)
- MyBundle_fr.properties (français)
- MyBundle_en.properties (english)
- le contenu des fichiers est le suivant :
game=My Super Cool Game newMission={0}, you have a new mission. Reach level {1}. coveredPath=You covered {0,number}% of the path highScoreTime=High score achieved on {0,date} at {0,time}
- la déclaration (dans la classe …)
-
FileHandle internal = Gdx.files.internal("i18n/MyBundle"); I18NBundle local = I18NBundle.createBundle(internal, Locale.getDefault());
- la récupération des valeurs
String value = myBundle.get(key);
- Ou avec des paramètres
String value = myBundle.format(key, arg1, arg2, ...);
Par exemple :
String game = myBundle.format("game"); String mission = myBundle.format("newMission", player.getName(), nextLevel.getName()); String coveredPath = myBundle.format("coveredPath", path.getPerc()); String highScoreTime = myBundle.format("highScoreTime", highScore.getDate());
Whe
https://github.com/libgdx/libgdx/wiki/Internationalization-and-Localization
https://stackoverflow.com/questions/28160566/libgdx-internationalization
F) Adapter l’écran pour Android :
Où l’écran « virtuel » a une dimension de 800×480
camera = new OrthographicCamera(); camera.setToOrtho(false, 800, 480); screenViewport = new ScreenViewport(camera); screenViewport.setUnitsPerPixel(Math.min(800.0f / Gdx.app.getGraphics().getWidth(), 480.0f / Gdx.app.getGraphics().getHeight()));
Et ajoutez :
public void resize(int width, int height) { screenViewport.update(width, height); }
G) AdMob, ajout de publicité :
Principe : ajouter un layout pour Android comportant la publicité
1°) éditez le gradle.properties pour convertir le projet en AndroidX, ajoutez
android.useAndroidX=true android.enableJetifier=true
2°) Ajoutez dans build.gradle du module android, à la fin du {} Android :
dependencies { implementation 'com.google.android.gms:play-services-ads:18.1.1' }
3°) Dans le fichier AndroidManifest.xml, avant la balise </application> ajoutez :
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 --> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713"/>
3°) editez dans core/java/com.votrejeux une interface (click droit / new / Java Class -> Interface) AdsController :
public interface AdsController { public void loadInterstitialAd(); public void showInterstitialAd(); }
4°) editez la classe AndroidLauncher et changez le type de classe en « implements AdsController » :
public class AndroidLauncher extends AndroidApplication implements AdsController {
5°) Editez la classe pour placer des publicités
private final AdsController adsController;
public MyGdxGame (AdsController adsController){ this.adsController = adsController; }
6°) Publicité en bandeau
Dans la classe AndroidLauncher.java :
private static final String BANNER_AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111"; AdView bannerAd;
Pour pouvoir aficher un bandeau avec une publicité, nous avons besoin de gérer 2 views. 1 view qui affichera le jeux et 1 view qui sera au dessus de la vue qui affiche le jeux et qui affichera le bandeau de publicité.
Remplacez
initialize(new MyGdxGame(this), config);
Par
View gameView = initializeForView(new MyGdxGame(this), config);
Et créer une méthode pour afficher la bannière
public void setupAds() { bannerAd = new AdView(this); bannerAd.setVisibility(View.INVISIBLE); bannerAd.setBackgroundColor(0xff000000); // black bannerAd.setAdUnitId(BANNER_AD_UNIT_ID); bannerAd.setAdSize(AdSize.SMART_BANNER); }
Insérez l’appel setupAds(); juste après View gameview… et créez le layout pour la banière
RelativeLayout layout = new RelativeLayout(this); layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); layout.addView(bannerAd, params); setContentView(layout);
Dans l’interface AdsController ajoutez
public void showBannerAd(); public void hideBannerAd();
Et ajoutez dans la classe AndroidLauncher
@Override public void showBannerAd() { runOnUiThread(new Runnable() { @Override public void run() { bannerAd.setVisibility(View.VISIBLE); AdRequest.Builder builder = new AdRequest.Builder(); AdRequest ad = builder.build(); bannerAd.loadAd(ad); } }); } @Override public void hideBannerAd() { runOnUiThread(new Runnable() { @Override public void run() { bannerAd.setVisibility(View.INVISIBLE); } }); }
Pour montrer ou cacher la bannière utilisez :
adsController.hideBannerAd(); ou showBannerAd();
Dans les écrans du jeux après avoir ajouté :
private final AdsController adsController;
Et
public GameScreen(final MyGdxGame game, AdsController adsController) { this.adsController = adsController; ....
Références :
http://www.norakomi.com/tutorial_admob_introduction.php#scroll_introduction
https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx
Pour adapter le code des videos ci-dessous, éditez le gradle.properties et ajoutez
android.useAndroidX=true android.enableJetifier=true