sud.createInterstitialAd
Creates an interstitial ad object. Each call returns a new instance.
typescript
createInterstitialAd(params: {
adUnitId: string;
}): InterstitialAd;Parameters
Object object
| Property | Type | Required | Description |
|---|---|---|---|
| adUnitId | string | Yes | Ad unit ID configured in the SUD OpenPaaS console |
Return Value
InterstitialAd ad object.
Methods
| Method | Return value | Description |
|---|---|---|
load() | Promise<void> | Starts loading the ad |
show() | Promise<void> | Requests that the ad be shown |
destroy() | void | Destroys the ad; the object cannot be reused |
The Promises returned by load() and show() report only the method-call result. Use onLoad for successful loading, onError for loading or display failures, and onClose for closing.
load() is subject to request frequency limits. When a limit is reached, onError reports the error code (errCode) and message (errMsg).
Events
| Method | Description |
|---|---|
onLoad(listener) | Listens for a successful ad load |
offLoad(listener) | Removes the specified load listener |
onError(listener) | Listens for ad loading or display failures |
offError(listener) | Removes the specified error listener |
onClose(listener) | Listens for the ad to close |
offClose(listener) | Removes the specified close listener |
onError Callback Parameters
The onError listener receives the following object:
| Property | Type | Description |
|---|---|---|
| errCode | number | See ad error codes |
| errMsg | string | Error message |
Example
Create and load the ad on level entry. Call onLevelFinished() when the level ends; if the ad is not ready, continue the game without displaying it.
javascript
const interstitialAd = sud.createInterstitialAd({
adUnitId: 'adunit-xxxxxxxx',
});
let ready = false;
let disposed = false;
let showRequested = false;
function onLoad() {
if (disposed) return;
ready = true;
}
function onError(err) {
if (disposed) return;
console.error('Interstitial ad failed', err);
onInterstitialSceneExit();
}
function onClose() {
if (disposed) return;
onInterstitialSceneExit();
}
function onLevelFinished() {
if (disposed || !ready || showRequested) return;
showRequested = true;
ready = false;
interstitialAd.show().catch(onError);
}
function onInterstitialSceneExit() {
if (disposed) return;
disposed = true;
ready = false;
interstitialAd.offLoad(onLoad);
interstitialAd.offError(onError);
interstitialAd.offClose(onClose);
interstitialAd.destroy();
}
interstitialAd.onLoad(onLoad);
interstitialAd.onError(onError);
interstitialAd.onClose(onClose);
interstitialAd.load().catch(onError);Bind onInterstitialSceneExit() to scene exit. For the complete flow, see game developer examples.
