Activate function after ad displayed?

Hi guys,

I’m trying to activate a function after an ad displayed. The goal of this function -> get a new character, either by using in game credits or by watching an ad. The same function is used for both, but only working properly when I use it with in game credits. This is how I’ve set up the ad listener:

 function functions.applovinAdListener( event ) if ( event.phase == "init" ) then -- Successful initialization print( event.isError ) -- Load an AppLovin ad applovin.load() elseif ( event.phase == "loaded" ) then -- The ad was successfully loaded print( event.type ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.isError ) print( event.response ) elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then -- The ad was displayed/played print( event.type ) elseif ( event.phase == "hidden" or event.phase == "playbackEnded" ) then -- The ad was closed/hidden print( event.type ) functions.freeRandomBall() applovin.load() elseif ( event.phase == "clicked" ) then -- The ad was clicked/tapped print( event.type ) end end applovin.init(functions.applovinAdListener, { sdkKey="MyKey" })

The “MyKey” string is filled in correctly in my code.

This is the code for the function:

 function functions.freeRandomBall(event) getRandomBallValue = mRand(2,6) if getRandomBallValue == 2 then transitions.boughtBall = transition.to(boughtBall[2], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[2], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball2 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[2].alpha = 1 print("ball2 = ", settings.ball2) elseif getRandomBallValue == 3 then transitions.boughtBall = transition.to(boughtBall[3], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[3], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball3 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[3].alpha = 1 print("ball3 = ", settings.ball3) elseif getRandomBallValue == 4 then transitions.boughtBall = transition.to(boughtBall[4], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[4], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball4 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[4].alpha = 1 print("ball4 = ", settings.ball4) elseif getRandomBallValue == 5 then transitions.boughtBall = transition.to(boughtBall[5], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[5], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball5 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[5].alpha = 1 print("ball5 = ", settings.ball5) elseif getRandomBallValue == 6 then transitions.boughtBall = transition.to(boughtBall[6], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[6], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball6 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[6].alpha = 1 print("ball5 = ", settings.ball5) end end function functions.watchAdForCoins(event) applovin.show() end

I feel like it should work just fine, but it’s not for some reason, the misbehaviour is hard to explain because sometimes it executes the function twice, sometimes it doesn’t executes the function… I don’t see a patern in the misbehaviour. Could someone help?

Kind regards

Bram

To begin with, I would try adding a few prints so you can see what is happening. I would advise printing the phase and isError at the very beginning of the appLovinAdListener:

function functions.applovinAdListener( event ) print("phase = ", event.phase) print("isError = ", event.isError) if ( event.phase == "init" ) then -- Successful initialization etc etc end 

Then print the random ball value immediately after it is created:

getRandomBallValue = mRand(2,6) print("randomballvalue = ", getRandomBallValue)

And then add a print to the start of your if/elseif statements to see whether they are being triggered successfully:

if getRandomBallValue == 2 then print("value is 2") transitions.boughtBall = transition.to(boughtBall[2], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[2], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball2 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[2].alpha = 1 print("ball2 = ", settings.ball2) elseif getRandomBallValue == 3 then print("value is 3") etc etc end

When it happens twice, my guess would be that both the “hidden” and “playbackEnded” phases are being triggered:

“playbackEnded” when the video reaches the end

“hidden” when the user dismisses the ad at the end

When it doesn’t happen at all…I’m not sure.

Hi,
I’m pretty sure that that isn’t the case because the user simply can’t skip the ad and the back button is disabled. No ither suggestions?:confused:
Thanks for the reply,
Bram

Has anyone else found the time to have a look? I really can’t seem to find the problem :confused:

What was the result of adding the print statements as I suggested earlier?  

Did you follow Alan’s request to get more information from your log?

Also make sure to print out the value of event.response along with event.phase and event.isError. You can’t trouble shoot these errors without putting prints in your code and studying your console log.

It’s also best to let Corona SDK’s build process install the app on your device for you and view the messages in the console.log window that is started with the simulator. As long as you DO NOT dismiss the popup window that says tells you to leave it up until your done debugging the device.  If not use “adb logcat” or “monitor” from the command line to watch the device log for Android, or Xcode’s “Devices” screen for iOS.

Okay, so I debugged on an actual device and was able to fix some stuff, but now the weirdest thing is happening. The function is firing twice. This is what I did, I made some changes to the code and placed it above 

-- "scene:create()" function scene:create( event )

So it should be available everywhere.

I adjusted the code to look like this:

-- FREE BALL AFTER AD FUNCTION function functions.freeRandomBall() print("Function randomBall FREE applied") getRandomBallValue = mRand(2,6) if getRandomBallValue == 2 then transitions.boughtBall = transition.to(boughtBall[2], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[2], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball2 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[2].alpha = 1 print("ball2 = ", settings.ball2) elseif getRandomBallValue == 3 then transitions.boughtBall = transition.to(boughtBall[3], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[3], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball3 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[3].alpha = 1 print("ball3 = ", settings.ball3) elseif getRandomBallValue == 4 then transitions.boughtBall = transition.to(boughtBall[4], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[4], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball4 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[4].alpha = 1 print("ball4 = ", settings.ball4) elseif getRandomBallValue == 5 then transitions.boughtBall = transition.to(boughtBall[5], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[5], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball5 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[5].alpha = 1 print("ball5 = ", settings.ball5) elseif getRandomBallValue == 6 then transitions.boughtBall = transition.to(boughtBall[6], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[6], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball6 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[6].alpha = 1 print("ball5 = ", settings.ball5) end end

And this is my code for the adlistener:

-- Ads function functions.onValidationExceededQuotaPopUp( event ) if event.action == "clicked" then local i = event.index if i == 1 then return true end end end function functions.applovinShopAdListener( event ) if ( event.phase == "init" ) then -- Successful initialization print( event.isError ) -- Load an AppLovin ad local isAdLoaded = applovin.isLoaded() print( isAdLoaded ) applovin.load() elseif ( event.phase == "loaded" ) then -- The ad was successfully loaded print( event.type ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.isError ) print( event.response ) applovin.load() elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then -- The ad was displayed/played print( event.type ) applovin.load() elseif ( event.phase == "hidden" or event.phase == "playbackEnded" ) then -- The ad was closed/hidden print( event.type ) functions.freeRandomBall() elseif ( event.phase == "clicked" ) then -- The ad was clicked/tapped print( event.type ) functions.freeRandomBall() end end applovin.init(functions.applovinShopAdListener, { sdkKey="MyKey" }) -- -------------------------------------------------------------------------------

Like I said in my first post, the function still works fine with a normal button that does not show ads. But when I try to use it with the adlistener, it fires twice :frowning:

Any ideas why this happens?

Kind regards

Bram

The log does not show any error or anything… And I’m not sure what more I could do with the log information.

Are you possibly getting multiple event.phases: both “hidden” and “playbackEnded” or “clicked” and “playbackEnded”?

Hi Rob,

I added some more print functions and it’s firing the “( event.phase == “hidden” or event.phase == “playbackEnded” )” twice. The Clicked phase is never entered.

It looks to me like it’s doing exactly as I suggested yesterday. It’s not triggering the same phase twice, it’s triggering those 2 separate phases:

“playbackEnded” is self-explanatory, it gets called when you reach the end of the ad.

“hidden” is most likely called _either _when the user dismisses the ad or when the ad automatically closes itself once it ends.

So it’s very likely that when an ad plays it triggers the “playbackEnded” as soon as the ad reaches then end, and then “hidden” when the ad disappears and returns back to your app. If you print out the phase itself rather than just printing inside the if statement, you should be able to see both of these phases being called. I would suggest that you don’t check for both of these phases when deciding whether to give the user the free item, just use the “playbackEnded” phase. 

I suspect the “clicked” phase is only supposed to be triggered if the user clicks the “download” button or whatever the ad uses to redirect to the relevant app store.

Hi Alan,

That I overlooked that! I guess I misunderstood your first response, but a thousand thanks. This was the final touch that I’d to make to my game :slight_smile:

Have a nice day,

Bram

To begin with, I would try adding a few prints so you can see what is happening. I would advise printing the phase and isError at the very beginning of the appLovinAdListener:

function functions.applovinAdListener( event ) print("phase = ", event.phase) print("isError = ", event.isError) if ( event.phase == "init" ) then -- Successful initialization etc etc end 

Then print the random ball value immediately after it is created:

getRandomBallValue = mRand(2,6) print("randomballvalue = ", getRandomBallValue)

And then add a print to the start of your if/elseif statements to see whether they are being triggered successfully:

if getRandomBallValue == 2 then print("value is 2") transitions.boughtBall = transition.to(boughtBall[2], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[2], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball2 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[2].alpha = 1 print("ball2 = ", settings.ball2) elseif getRandomBallValue == 3 then print("value is 3") etc etc end

When it happens twice, my guess would be that both the “hidden” and “playbackEnded” phases are being triggered:

“playbackEnded” when the video reaches the end

“hidden” when the user dismisses the ad at the end

When it doesn’t happen at all…I’m not sure.

Hi,
I’m pretty sure that that isn’t the case because the user simply can’t skip the ad and the back button is disabled. No ither suggestions?:confused:
Thanks for the reply,
Bram

Has anyone else found the time to have a look? I really can’t seem to find the problem :confused:

What was the result of adding the print statements as I suggested earlier?  

Did you follow Alan’s request to get more information from your log?

Also make sure to print out the value of event.response along with event.phase and event.isError. You can’t trouble shoot these errors without putting prints in your code and studying your console log.

It’s also best to let Corona SDK’s build process install the app on your device for you and view the messages in the console.log window that is started with the simulator. As long as you DO NOT dismiss the popup window that says tells you to leave it up until your done debugging the device.  If not use “adb logcat” or “monitor” from the command line to watch the device log for Android, or Xcode’s “Devices” screen for iOS.

Okay, so I debugged on an actual device and was able to fix some stuff, but now the weirdest thing is happening. The function is firing twice. This is what I did, I made some changes to the code and placed it above 

-- "scene:create()" function scene:create( event )

So it should be available everywhere.

I adjusted the code to look like this:

-- FREE BALL AFTER AD FUNCTION function functions.freeRandomBall() print("Function randomBall FREE applied") getRandomBallValue = mRand(2,6) if getRandomBallValue == 2 then transitions.boughtBall = transition.to(boughtBall[2], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[2], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball2 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[2].alpha = 1 print("ball2 = ", settings.ball2) elseif getRandomBallValue == 3 then transitions.boughtBall = transition.to(boughtBall[3], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[3], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball3 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[3].alpha = 1 print("ball3 = ", settings.ball3) elseif getRandomBallValue == 4 then transitions.boughtBall = transition.to(boughtBall[4], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[4], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball4 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[4].alpha = 1 print("ball4 = ", settings.ball4) elseif getRandomBallValue == 5 then transitions.boughtBall = transition.to(boughtBall[5], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[5], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball5 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[5].alpha = 1 print("ball5 = ", settings.ball5) elseif getRandomBallValue == 6 then transitions.boughtBall = transition.to(boughtBall[6], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) transitions.boughtBallTxt = transition.to(boughtBallTxt[6], {time = 3500, alpha = 1, onComplete = functions.removeBoughtBallandTxt}) settings.ball6 = true loadsave.saveTable(settings, "circleGameSettings.json") availableBar[6].alpha = 1 print("ball5 = ", settings.ball5) end end

And this is my code for the adlistener:

-- Ads function functions.onValidationExceededQuotaPopUp( event ) if event.action == "clicked" then local i = event.index if i == 1 then return true end end end function functions.applovinShopAdListener( event ) if ( event.phase == "init" ) then -- Successful initialization print( event.isError ) -- Load an AppLovin ad local isAdLoaded = applovin.isLoaded() print( isAdLoaded ) applovin.load() elseif ( event.phase == "loaded" ) then -- The ad was successfully loaded print( event.type ) elseif ( event.phase == "failed" ) then -- The ad failed to load print( event.type ) print( event.isError ) print( event.response ) applovin.load() elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then -- The ad was displayed/played print( event.type ) applovin.load() elseif ( event.phase == "hidden" or event.phase == "playbackEnded" ) then -- The ad was closed/hidden print( event.type ) functions.freeRandomBall() elseif ( event.phase == "clicked" ) then -- The ad was clicked/tapped print( event.type ) functions.freeRandomBall() end end applovin.init(functions.applovinShopAdListener, { sdkKey="MyKey" }) -- -------------------------------------------------------------------------------

Like I said in my first post, the function still works fine with a normal button that does not show ads. But when I try to use it with the adlistener, it fires twice :frowning:

Any ideas why this happens?

Kind regards

Bram

The log does not show any error or anything… And I’m not sure what more I could do with the log information.

Are you possibly getting multiple event.phases: both “hidden” and “playbackEnded” or “clicked” and “playbackEnded”?