Jump to content

Leaderboard no open


JairoGames
 Share

Recommended Posts

Good morning everyone, I would like your help . this my code :main.js    What are missing to activate Leaderboard

In developer facebook page active Leaderboard generated this code:

 

Name: Top
Sort Order: HIGHER_IS_BETTER
Score Format:
Type: NUMERIC
Options: {"decimal_offset":0}
Is Context Scoped: False

Thank you all!

game.module(
    'game.main'
)
.require(
    'plugin.essentials',
    'plugin.instantgames'
)
.body(function() {
    
game.addAsset('music.m4a');
game.addAsset('bg.png');
game.addAsset('fg.png');
game.addAsset('monster1.png');
game.addAsset('monster2.png');
game.addAsset('monster3.png');
game.addAsset('monster4.png');
game.addAsset('monster5.png');
game.addAsset('button.png');
game.addAsset('font.fnt');

// This function is called as soon as the Instant Games SDK is initialized
game.onFBInstantInited = function() {
    // Check if leaderboard is supported
    if (FBInstant.getSupportedAPIs().indexOf('getLeaderboardAsync') !== -1) {
        // Load leaderboard
        FBInstant.getLeaderboardAsync('highscore')
        .then(function(leaderboard) {
            // Save leaderboard instance so we can access it from anywhere
            game.leaderboard = leaderboard;
        })
        .catch(function(error) {
            console.log(error);
        });
    }
};

game.createScene('Main', {
    backgroundColor: '#b3cfd1',
    monsters: [],
    gamesPlayed: 0,
    score: 0,
    timeout: 20,

    init: function() {
        var music = new game.Music('music.m4a');
        music.play();
        
        // Add background sprites
        this.stage.addChild(new game.Sprite('bg.png', { y: game.height / 2 }));
        this.stage.addChild(new game.Sprite('bg.png', { x: game.width, y: game.height / 2, scale: new game.Vector(-1, -1) }));

        this.monsterContainer = new game.Container().addTo(this.stage);
        
        // Add foreground sprites
        var fg = new game.Sprite('fg.png');
        fg.y = game.height - 204;
        fg.interactive = true; // Prevent from tapping monsters under the sprite
        fg.addTo(this.stage);

        var fg = new game.Sprite('fg.png');
        fg.scale.set(1, -1); // Flip sprite
        fg.y = 204;
        fg.interactive = true;
        fg.addTo(this.stage);
        
        this.startContainer = new game.Container().addTo(this.stage);

        // Load interstitial ad
        this.ad = new game.InstantAd('421863008273455_433527023773720');
        this.ad.load();
        
        // Add start button
        var startButton = new game.TextButton('button.png', 'START', game.width / 2, game.height / 2, this.startGame.bind(this));
        startButton.text.y -= 10;
        startButton.addTo(this.startContainer);
        
        // Load player data
        FBInstant.player.getDataAsync(['gamesPlayed']).then(this.showPlayerData.bind(this));
        
        if (game.leaderboard) this.loadLeaderboard();
    },
    
    showPlayerData: function(data) {
        if (typeof data.gamesPlayed !== 'undefined') this.gamesPlayed = parseInt(data.gamesPlayed);
        
        // Show how many games played
        var text = new game.Text('Games played: ' + this.gamesPlayed);
        text.x = game.width / 2 - text.width / 2;
        text.y = game.height - 90;
        text.addTo(this.startContainer);
        
        // Show player's photo
        var photo = new game.PlayerPhoto('playerPhoto', 100, true);
        photo.x = game.width / 2 - 50;
        photo.y = game.height - 200;
        photo.addTo(this.startContainer);
    },
    
    loadLeaderboard: function() {
        // Load leaderboard entries (only first)
        game.leaderboard.getEntriesAsync(1, 0).then(this.showLeaderboard.bind(this));
    },
    
    showLeaderboard: function(entries) {
        if (entries.length === 0) return;
        
        // We only get first entry
        var entry = entries[0];
        
        // Get entry score
        var score = entry.getScore();
        
        // Show current highscore
        var text = new game.Text('Highscore: ' + score);
        text.x = game.width / 2 - text.width / 2;
        text.y = 10;
        text.addTo(this.startContainer);
        
        // Show photo of the current leader
        var player = entry.getPlayer();
        var photoUrl = player.getPhoto();
        var photo = new game.PlayerPhoto(photoUrl, 100, true);
        photo.x = game.width / 2 - 50;
        photo.y = 100;
        photo.addTo(this.startContainer);
        
   
    },
    
    gameMusic: function() {
        var music = new game.Music('music.m4a');
        music.play();

    
        
    },
    
    startGame: function() {
        this.startContainer.remove();
        
        this.gamesPlayed++;
        // Save new player data
        FBInstant.player.setDataAsync({
            gamesPlayed: this.gamesPlayed
        });
        
        this.monsters.push(new game.Monster(60, 4 * 51, 1));
        this.monsters.push(new game.Monster(60 + 200 * 1, 4 * 51, 1));
        this.monsters.push(new game.Monster(60 + 200 * 2, 4 * 51, 1));
        this.monsters.push(new game.Monster(60 + 200 * 3, 4 * 51, 1));
        
        this.monsters.push(new game.Monster(60, 4 * 51, -1));
        this.monsters.push(new game.Monster(60 + 200 * 1, 4 * 51, -1));
        this.monsters.push(new game.Monster(60 + 200 * 2, 4 * 51, -1));
        this.monsters.push(new game.Monster(60 + 200 * 3, 4 * 51, -1));
        
        this.scoreText = new game.Text();
        this.scoreText.y = 10;
        this.scoreText.addTo(this.stage);
        this.addScore(0);
        
        this.timeText = new game.Text('Time: ' + this.timeout);
        this.timeText.x = game.width / 2 - this.timeText.width / 2;
        this.timeText.y = game.height - 90;
        this.timeText.addTo(this.stage);
        
        // Start game timer that calls updateTime function every second
        this.gameTimer = game.Timer.add(1000, this.updateTime.bind(this), true);
    },
    
    updateTime: function() {
        this.timeout--;
        this.timeText.setText('Time: ' + this.timeout);
        this.timeText.x = game.width / 2 - this.timeText.width / 2;
        
        if (this.timeout === 0) this.gameOver();
    },
    
    gameOver: function() {
        this.isGameOver = true;
        this.gameTimer.pause();
        
        if (game.leaderboard) this.saveScore();
        
        var restartButton = new game.TextButton('button.png', 'RESTART', game.width / 2, game.height / 2, this.restartGame.bind(this));
        restartButton.text.y -= 10;
        restartButton.addTo(this.stage);
    },
    
    restartGame: function() {
        // Show ad and then restart game
        this.ad.show(function() {
            game.system.setScene('Main');
        });
    },
    
    saveScore: function() {
        // Save score to leaderboard
        game.leaderboard.setScoreAsync(this.score).then(function(entry) {
            // Get player's current rank
            var rank = entry.getRank();
            
            var text = new game.Text('Your rank is ' + rank);
            text.x = game.width / 2 - text.width / 2;
            text.y = game.height / 2 - 150;
            text.addTo(game.scene.stage);
        }).catch(function(error) {
            console.log(error);
        });
    },
    
    addScore: function(amount) {
        if (this.isGameOver) return;
        
        this.score += amount;
        this.scoreText.setText('Score: ' + this.score);
        this.scoreText.x = game.width / 2 - this.scoreText.width / 2;
    }
});

game.createClass('Monster', 'Sprite', {
    texture: 'monster1.png',
    bounceHeightMin: 4 * 10,
    bounceHeightMax: 268,
    bounceSpeedMin: 200,
    bounceSpeedMax: 1000,
    bounceDelayMin: 100,
    bounceDelayMax: 1000,

    init: function(x, y, dir) {
        this.id = game.scene.monsters.length + 1;
        this.dir = dir;
        
        if (dir === 1) y = game.height - y;
        else this.scale.y = -1;
        
        this.anchor.x = this.width / 2;
        this.position.set(x, y);
        this.origPos = y;

        this.timer = new game.Timer();
        this.bounce();

        this.addTo(game.scene.monsterContainer);
    },

    bounce: function(delay) {
        this.interactive = true;
        
        var bounceHeight = Math.random(this.bounceHeightMin, this.bounceHeightMax);
        var bounceTime = Math.random(this.bounceSpeedMin, this.bounceSpeedMax);
        var bounceDelay = Math.random(this.bounceDelayMin, this.bounceDelayMax);

        game.Tween.add(this.position, { y: this.position.y - bounceHeight * this.dir }, bounceTime, {
            delay: bounceDelay,
            onStart: this.reset.bind(this),
            onComplete: this.bounce.bind(this),
            easing: 'Quadratic.InOut',
            repeat: 1,
            yoyo: true
        }).start();
    },

    reset: function() {
        this.timer.reset();
        this.scale.x = Math.random() > 0.5 ? 1 : -1;
        var texture = 'monster' + Math.round(Math.random(1, 5)) + '.png';
        this.setTexture(texture);
    },

    mousedown: function() {
        this.interactive = false;
        
        game.Tween.stopTweensForObject(this.position);
        game.Tween.add(this.position, { y: this.origPos }, 100, {
            onComplete: this.bounce.bind(this)
        }).start();
        
        game.scene.addScore(1);
    }
});

});

 

Link to comment
Share on other sites

3 hours ago, enpu said:

I don't think you need to activate leaderboard in any way. Do you get any errors with the getLeaderboardAsync command?

I see that you named your leaderboard "Top". You should use that same name in getLeaderboardAsync command:


FBInstant.getLeaderboardAsync('Top')

 

I edited and it worked, thank you!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...