Appodeal's adListener not wroking while using composer API

Hi there,

I am currently working on a android game and using the appodeal plugin for monitization.

I follow the the guide and docs to init and show the “interstitial” ad successfully in the game’s “gameScene”.

Also the adListener works fine so I get notice for each state such as event.phase == “loaded”,“displayed”…

But when I use the : composer.gotoScene( “rewardScene”, options ) to transit to the  “rewardScene”, and then

composer.gotoScene( “gameScene”, options ) to go back the “gameScene” for next stage, I found that the adListener’s callback is not working, I get no feedback for each state :event.phase==“loaded”,“displayed”,“clicked”.

Is anything I’ve missed? or  any way to fixed this problem?

Thanks

Hi @vincenthawk,

Almost certainly it’s how you have “scoped” the Appodeal listener function in the Lua module where you put it. Other Composer scenes need to know about it (have reference to it). There are several ways to accomplish this, so if you post some of your code, I’m sure we can help you out.

Take care,

Brent

Hi Brent,

thanks for your quick reply

here is the code related:
 

local composer = require( "composer" ) local scene = composer.newScene() display.setStatusBar(display.HiddenStatusBar) local appodeal = require( "plugin.appodeal" ) --show institual function showInterstitial() if appodeal.isLoaded("rewardedVideo") then appodeal.show("rewardedVideo") return true elseif appodeal.isLoaded("interstitial") then appodeal.show("interstitial") return true else return true end end --goto next stage local function gotoNextStage() --currentStage=currentStage+1 saveDataToJson() local options = { effect = "fade", time = 1, } composer.gotoScene( "interScene", options ) return true end -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) --print("scene:create( event )") sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen composer.recycleOnSceneChange = true local function adListener( event ) -- Exit function if user hasn't set up testing parameters if ( setupComplete == false ) then return end -- Successful initialization of the Appodeal plugin if ( event.phase == "init" ) then --print( "Appodeal event: initialization successful" ) -- An ad loaded successfully elseif ( event.phase == "loaded" ) then -- The ad was displayed/played elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then -- The ad was closed/hidden/completed elseif ( event.phase == "hidden" or event.phase == "closed" or event.phase == "playbackEnded" ) then -- The user clicked/tapped an ad elseif ( event.phase == "clicked" ) then -- The ad failed to load elseif ( event.phase == "failed" ) then --print( "Appodeal event: " .. tostring(event.type) .. " ad failed to load" ) end end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then appodeal.init( adListener, { appKey="" } ) elseif ( phase == "did" ) then if(currentStage%3==0) then if appodeal.isLoaded("interstitial") then appodeal.show("interstitial") end end end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

I am looking forward to hearing more advisements from you, thanks a lot !!

Best
Vincent

Hi Vincent,

Do you plan to show ads in multiple scenes, like within “gameScene” and also within “rewardScene”?

Brent

Hi Brent,

No, I plan to show ads only in “gameScene”.

The first time for entering the  “gameScene”, everything works fine.

The problem is when the scene transition like : gameScene->rewardScene->gameScene,

the second time back to  “gameScene”, the adListener call back not working.

Vincent

@vincenthawk

init() can only be called once in the lifetime of an app (all our monetization plugins behave the same way). Therefore you should call appodeal.init() from your main.lua only. If you call it from within a scene the plugin will lose its initialization when changing scenes which is causing the issues you are seeing.

As Ingemar suggests, a lot of the ad “handling” stuff can be moved out of the scene module and over to “main.lua”. So I would suggest doing these tasks in that file (“main.lua”):

[lua]

local appodeal = require( “plugin.appodeal” )

local function adListener( event )

    – your code from above (or similar)

end

appodeal.init( adListener, { appKey="" } )

[/lua]

Now back in the Composer scene, it’s usually best to show an add during the “did” phase of the “show:create()” function block. This is because you’ll almost always want the ad to display once the scene is entirely on screen.

Hope this gets you on the right track. Any other questions, please ask.

Brent

P.S. If you are still a bit new to Composer, I would suggest you follow through the “Star Explorer” sample game tutorial. That goes into more detail on exactly how Composer works, when things should be called, how to scope things properly with Composer, etc. It might seem a bit distracting to put your game aside for a tutorial, but trust me, it will be super valuable to learn this as you move forward with Corona. :slight_smile:

https://docs.coronalabs.com/guide/programming/02/index.html

Brent

Hi Brent & Ingemar ,

thanks a lot

I’ll try to fix my code, and Yes,

I’ll dig through “Star Explorer” tutorial, too.

Best

Vincent

Hi  Ingemar,

There is one more question.

If I put the : 

appodeal.init( adListener, { appKey="" } )

in main.lua, where shoud I announce the function adListener?

If I put the adListener() in main.lua, how can I get call back function called in the gameScene.lua?

Best

Vincent

Hi Vincent,

Can you please describe what you wish to happen in “gameScene” when a specific ad event occurs? There are various ways to link up function references between modules/scenes, and I can help guide you with more details on your goals.

Brent

Hi Brent ,

I think the problem is caused by the command :  composer.recycleOnSceneChange = true

thanks for your kindly help ^^

Vincent

Hi Vincent,

Can you please download a recent daily build of Corona? This function had a known bug where the scene would be trashed entirely regardless of passing “true” as its sole parameter, but we recently fixed it. If you grab a recent build, it should be fixed:

https://developer.coronalabs.com/downloads/daily-builds/

Best regards,

Brent

Hi Brent,

Is there any guide for updating new corona sdk?

Vincent

Hi Vincent,

You can just download a newer version and run it independently as a standalone application. :slight_smile:

Brent

Hi @vincenthawk,

Almost certainly it’s how you have “scoped” the Appodeal listener function in the Lua module where you put it. Other Composer scenes need to know about it (have reference to it). There are several ways to accomplish this, so if you post some of your code, I’m sure we can help you out.

Take care,

Brent

Hi Brent,

thanks for your quick reply

here is the code related:
 

local composer = require( "composer" ) local scene = composer.newScene() display.setStatusBar(display.HiddenStatusBar) local appodeal = require( "plugin.appodeal" ) --show institual function showInterstitial() if appodeal.isLoaded("rewardedVideo") then appodeal.show("rewardedVideo") return true elseif appodeal.isLoaded("interstitial") then appodeal.show("interstitial") return true else return true end end --goto next stage local function gotoNextStage() --currentStage=currentStage+1 saveDataToJson() local options = { effect = "fade", time = 1, } composer.gotoScene( "interScene", options ) return true end -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) --print("scene:create( event )") sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen composer.recycleOnSceneChange = true local function adListener( event ) -- Exit function if user hasn't set up testing parameters if ( setupComplete == false ) then return end -- Successful initialization of the Appodeal plugin if ( event.phase == "init" ) then --print( "Appodeal event: initialization successful" ) -- An ad loaded successfully elseif ( event.phase == "loaded" ) then -- The ad was displayed/played elseif ( event.phase == "displayed" or event.phase == "playbackBegan" ) then -- The ad was closed/hidden/completed elseif ( event.phase == "hidden" or event.phase == "closed" or event.phase == "playbackEnded" ) then -- The user clicked/tapped an ad elseif ( event.phase == "clicked" ) then -- The ad failed to load elseif ( event.phase == "failed" ) then --print( "Appodeal event: " .. tostring(event.type) .. " ad failed to load" ) end end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then appodeal.init( adListener, { appKey="" } ) elseif ( phase == "did" ) then if(currentStage%3==0) then if appodeal.isLoaded("interstitial") then appodeal.show("interstitial") end end end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

I am looking forward to hearing more advisements from you, thanks a lot !!

Best
Vincent

Hi Vincent,

Do you plan to show ads in multiple scenes, like within “gameScene” and also within “rewardScene”?

Brent

Hi Brent,

No, I plan to show ads only in “gameScene”.

The first time for entering the  “gameScene”, everything works fine.

The problem is when the scene transition like : gameScene->rewardScene->gameScene,

the second time back to  “gameScene”, the adListener call back not working.

Vincent

@vincenthawk

init() can only be called once in the lifetime of an app (all our monetization plugins behave the same way). Therefore you should call appodeal.init() from your main.lua only. If you call it from within a scene the plugin will lose its initialization when changing scenes which is causing the issues you are seeing.