The GamePix game SDK provides a series of utility methods needed for running your game in the GamePix ecosystem.
They enhance the user experience of your game and complete the integration with our ads network and revenues system.
Before you start integrating the SDK methods inside your Game, here are a few things to check:
window.alert or window.confirm.Import the SDK as first script in the <head> section.
<head> <script src="https://integration.gamepix.com/sdk/v3/gamepix.sdk.js"></script> ... </head>
Use GamePix.localStorage methods to save progress in your Game, through getItem, setItem and removeItem. Consider that your game will be played inside an iframe, so in some mobile browsers, when the user closes the application or a week has passed, the localStorage will be purged, due to a third party policy restriction. We are working hard on overcoming this restriction with a global storage mechanism that will save in cloud users' progress. When this mechanism will be available, it will be integrated in GamePix.localStorage methods.
// GamePix.localStorage.setItem(key, value);
// @param: {String} key
// @param: {String} value
// returns: {Object}
// example:
GamePix.localStorage.setItem(`mySuperProperty`, `1`);
// Messages:
// Error: KEY_OR_VALUE_FOR_LOCALSTORAGE_NOT_A_STRING
/*-------------------------------------------------------*/
// GamePix.localStorage.getItem(key);
// @param: {String} key
// returns: {String || null}
// example:
GamePix.localStorage.getItem(`mySuperProperty`);
// Messages:
// Error: KEY_OR_VALUE_FOR_LOCALSTORAGE_NOT_A_STRING
/*-------------------------------------------------------*/
// GamePix.localStorage.removeItem(key);
// @param: {String} key
// returns: {Object}
// example:
GamePix.localStorage.removeItem(`mySuperProperty`);
// Messages:
// Error: KEY_OR_VALUE_FOR_LOCALSTORAGE_NOT_A_STRINGNot every single interstitialAd() will trigger an ad. GamePix's system will determine when a user is ready for another ad, so feel free to signal as many commercial break opportunities as possible. Remember to pause your game before calling interstitialAd() and to resume it in the callback.
// GamePix.interstitialAd()
// @param: none
// returns: {Promise}
// example:
// Show request for Interstitial Ad
// IMPORTANT: *** PAUSE YOUR GAME ***
GamePix.interstitialAd().then(function (res) {
// IMPORTANT: *** RESUME YOUR GAME ***
if (res.success) {
// Log the success if you want
info();
} else {
// Log the error if you want
errorInfo();
}
});
// Messages:
// Error: GAMEPIX_LOADED_NOT_CALLED
// Error: INTERSTITIAL_AD_CALLED_TWICEThis function will ask the SDK to show a Reward Ad. This method waits for the reward ad to be displayed and then returns res.success = true if the user has watched it, or false if a reward ad is not available or the user has not watched it. GamePix rewarded ads are still in beta, the fill rate depends on countries and device settings. In the test environment, you will receive res.success = true 50% of the time.
// GamePix.rewardAd()
// @param: none
// returns: {Promise}
// example:
// Show request for Reward Ad
GamePix.rewardAd().then(function (res) {
if (res.success) {
assignReward();
} else {
denyReward();
}
});
// Messages:
// Error: GAMEPIX_LOADED_NOT_CALLED
// Error: REWARD_AD_CALLED_TWICECall this function every time there's a score update inside the Game. The function accepts numbers only.
// GamePix.updateScore(currentScore)
// @param: {Number} currentScore
// returns: {Number}
// example:
// Player scores 9000!!!!!
let currentScore = 9000;
GamePix.updateScore(currentScore);
// Messages:
// Error: GAMEPIX_LOADED_NOT_CALLED
// Error: UPDATE_SCORE_VALUE_IS_NOT_A_NUMBERCall this function every time the user gets to a new level. The function accepts numbers only.
// GamePix.updateLevel(currentLevel)
// @param: {Number} currentLevel
// returns: {Number}
// example:
// Player reaches level 10
let currentLevel = 10
GamePix.updateLevel(currentLevel);
// Messages:
// Error: GAMEPIX_LOADED_NOT_CALLED
// Error: UPDATE_LEVEL_VALUE_IS_NOT_A_NUMBERIf your Game has multiple languages (and you want to know it before the Game's loaded, in order to switch your Game language for the user), you can check the current player language using GamePix.lang(). Otherwise the Game should default to English.
// GamePix.lang()
// @param: none
// returns: String (enum: ['ar','zh','nl','en','fr','de','it','ja','ko','pl','pt','ru','es','tr'])
// example:
const lang = GamePix.lang();
console.log('lang:', lang) // => "lang: en"Send to the SDK if a particular "Happy Moment" just happened in your Game. It could be the unlocking of a special item, or triggering a cool event in the Game.
// GamePix.happyMoment() // @param: none // returns: null // example: // Player wins a tournament or unlock something GamePix.happyMoment()
By default, browsers allow the page to scroll when the user presses the spacer or arrow keys. This behavior should be disabled in your game. To prevent scrolling, you can include the following code in your game:
window.addEventListener("keydown", (event) => {
if (["ArrowUp", "ArrowDown", " "].includes(event.key)) {
event.preventDefault();
}
});
window.addEventListener("wheel", (event) => event.preventDefault(), {
passive: false,
});The following messages are the responses the SDK returns in case of problems: