Wortal SDK for Cocos Creator 2.x
Installation
- Install the extension from the Cocos Store.
Building
To use the Wortal SDK in your game, you must build with the web-mobile template. This is included in the extension and handles all the necessary setup.
Initialization
-
Call
wortal.initializeAsync()
as early as possible in your game's initialization code, thenwortal.startGameAsync()
when your game has finished loading and is ready for play. -
Report the loading progress of the game in your initialization code. The game will not start until the loading progress reaches 100%.
wortal.initializeAsync().then(() => {
// SDK is ready to use.
// Wait for game to finish loading.
wortal.setLoadingProgress(100);
wortal.startGameAsync();
});
How to Use
Ads
Interstitial ads can be shown at various points in the game such as a level end, restart or a timed interval in games with longer levels.
// Player reached the next level.
wortal.ads.showInterstitial('next', 'NextLevel', pauseGame, resumeGame);
// Player paused the game.
wortal.ads.showInterstitial('pause', 'PausedGame', pauseGame, resumeGame);
// Player opened the IAP shop.
wortal.ads.showInterstitial('browse', 'BrowseShop', pauseAudio, resumeAudio);
Rewarded ads can be shown too. These are longer, optional ads that the player can earn a reward for watching. The player must be notified of the ad and give permission to show before it can be shown.
// This example shows the game flow independent of the outcome of the ad.
// Ex: Player gets bonus coins for watching the ad, but the game continues regardless of the outcome.
wortal.ads.showRewarded('BonusCoins', pauseGame, resumeGame, skipBonus, addBonusCoins);
// This example shows the game flow depending on the outcome of the ad.
// Ex: Player dies and can revive by watching an ad, but if they skip the ad they lose the level.
wortal.ads.showRewarded('ReviveAndContinue', pauseAudio, resumeAudio, endGame, continueGame);
NOTE: Players should only be rewarded in the adViewed
callback.
Analytics
The Analytics API can be used to track game events that can help better understand how players are interacting with the game. This data will be available for viewing in the Wortal dashboard.
// Logs the start of the level.
wortal.analytics.logLevelStart('Level 3');
// Logs the end of the level. Will track the time spent playing the level if the name matches
// the name of the last logLevelStart() call.
wortal.analytics.logLevelEnd('Level 3', '100', true);
// Logs a choice the player made in the game. This can be useful for balancing the game
// and seeing what content your players interact with the most.
wortal.analytics.logGameChoice('Character', 'Blue');
Achievements
The Achievements API is used to track the player's progress in the game and reward them for completing tasks.
// Unlock an achievement.
wortal.achievements.unlockAchievementAsync('achievementID')
.then(() => console.log("Achievement unlocked!"));
// Get the player's achievements progress.
wortal.achievements.getAchievementsAsync()
.then(achievements => console.log(achievements));
Context
The Context API is used to connect players and allow them to interact in the game session, share their content and send messages to each other.
// Invite a friend to play the game. Does not switch the player's current context.
wortal.context.inviteAsync({
image: 'data:base64Image',
text: 'Invite text',
cta: 'Play',
data: { exampleData: 'yourData' },
}).then(() => console.log("Invite sent!"))
// Share your game activity with friends.
wortal.context.shareAsync({
image: 'data:base64image',
text: 'Share text',
cta: 'Play',
data: { exampleData: 'yourData' },
}).then(result => console.log(result));
In-App Purchases
The In-App Purchases (IAP) API is used to provide an interface for in-game transactions on the platforms. This process will differ based on what platform the game is being played on, but the API remains the same.
// Get the catalog of products the player can purchase.
wortal.iap.getCatalogAsync()
.then(products => console.log(products));
// Purchase a product.
wortal.iap.makePurchaseAsync({
productID: 'my_product_123',
}).then(purchase => console.log(purchase));
Leaderboards
The Leaderboard API gives the game access to the platform's leaderboard functionality. This is where you can track player's scores and compare them to other players.
// Get the top 10 entries on the global leaderboard.
wortal.leaderboard.getEntriesAsync('global', 10)
.then(entries => console.log(entries));
// Add the player's score to the leaderboard.
wortal.leaderboard.sendEntryAsync('global', 100);
Notifications
The Notifications API is used to send notifications to the player. These can be used to notify the player of an event in the game or to remind them to come back and play.
// Schedule a notification to send to the player.
wortal.notifications.scheduleAsync({
title: "Your energy is full!",
body: "Come back and play again.",
mediaURL: "https://example.com/image.png",
label: "resources-full",
scheduleInterval: 300 // 5 minutes
}).then((result) => {
console.log(result.id);
});
// Cancel a scheduled notification.
wortal.notifications.cancelAsync('notification-id-123')
.then((result) => {
console.log(result);
});
Player
You can find details about the current player via the Player API.
// Get the player's name.
wortal.player.getName();
// Get a list of the player's friends who have also played this game.
wortal.player.getConnectedPlayersAsync({
filter: 'ALL',
size: 20,
hoursSinceInvitation: 4,
}).then(players => console.log(players.length));
Session
Details about the current session can be accessed in the Session API.
// Get the entry point of where the game started from.
wortal.session.getEntryPointAsync()
.then(entryPoint => console.log(entryPoint));
// Get the entry point data from a social invite or share.
// This is useful for tracking where players are coming from or
// providing a reward for players who were invited to play.
const data = wortal.session.getEntryPointData();
console.log(data);
Stats
Track stats for players and record their high scores with the Stats API.
// Get the player's high score for the level.
wortal.stats.getStatsAsync("level1")
.then(stats => console.log(stats.value));
// Set the player's high score for the game.
wortal.stats.postStatsAsync("level1", 1000)
.then(() => console.log("High score set!"));
Tournament
The Tournament API is used to create and manage tournaments for your game.
// Create a tournament.
const payload = {
initialScore: 100,
config: {
title: "Level 1 Tournament",
},
data: {
level: 1,
},
};
wortal.tournament.createAsync(payload)
.then(tournament => console.log(tournament.payload["level"]));
// Post a score to a tournament.
wortal.tournament.postScoreAsync(200)
.then(() => console.log("Score posted!"));