Quickstart: three.js
Add the SDK to a three.js game, pause cleanly around ads, and add a revive button paid by a rewarded ad.
1. Add the script
<script src="https://developer.goof.gg/sdk/v1/goof.js"></script>
<script type="module" src="./main.js"></script>
2. Start the SDK
await GOOF.game.init({ gameId: 'dYOUR_APP_ID' });
// ... load your models and textures ...
GOOF.game.setLoadingProgress(1);
GOOF.game.gameReady();
3. Pause and resume in one place
GOOF.game.onHide fires when the player leaves the game and before every ad or payment sheet. GOOF.game.onShow fires when they come back. Stop the render loop and the sound there.
const listener = new THREE.AudioListener();
camera.add(listener);
function loop() { /* update and render */ renderer.render(scene, camera); }
renderer.setAnimationLoop(loop);
GOOF.game.onHide(() => {
renderer.setAnimationLoop(null); // stop drawing
listener.setMasterVolume(0); // mute
});
GOOF.game.onShow(() => {
listener.setMasterVolume(1);
clock.getDelta(); // drop the time spent away
renderer.setAnimationLoop(loop);
});
The host keeps your WebGL context and all your game state while an ad shows. You do not need to reload textures or rebuild the scene after an ad.
4. A revive button with a rewarded ad
Show the button only when the player loses, and say what they get.
const reviveAd = GOOF.game.createRewardedVideoAd({ adUnitId: 'au_0123456789ab' });
reviveAd.onClose(({ isEnded }) => {
if (isEnded) revivePlayer(); // reward only when the ad was watched to the end
else showGameOver();
});
reviveAd.onError(({ errMsg, errorCode }) => {
console.warn('ad error', errorCode, errMsg);
showGameOver();
});
reviveButton.textContent = 'Watch an ad to continue';
reviveButton.onclick = async () => {
reviveButton.disabled = true;
try {
await reviveAd.load();
await reviveAd.show();
} catch (e) {
// e.errorCode: 30002 no ad available, 40001 not supported here, ...
showGameOver();
}
};
The same ad object can be shown again after it closed. Call destroy() when you no longer need it.
5. Test
Serve the folder on localhost. The SDK runs in mock mode: the test ad has a 5 second countdown, and closing it early gives isEnded: false. Then zip the build folder and upload it (see Publishing).