如何销毁 ADMOB 广告



当我在main.lua文件中使用下面的代码时,它会按照我想要的方式显示广告。但是当我在"退出场景"场景部分添加"ads.hide()"(广告停留在每个场景中)时,我在终端中收到此错误"尝试索引全局'广告'(零值)",我理解为广告不会显示在模拟器中,但是当我打开应用程序操作我的手机(Galaxy S4)时,没有按钮响应, 它只是停留在主文件/场景中.lua

local provider = "admob"
local appID = "**********"
local ads = require "ads"
local screenGroup = self.view
local statusText = display.newText( "", 0, 0, native.systemFontBold, 22 )
statusText:setTextColor( 255 )
statusText:setReferencePoint( display.CenterReferencePoint )
statusText.x, statusText.y = display.contentWidth * 0.5, 160
local showAd
local function adListener( event )
    local msg = event.response
    print("Message received from the ads library: ", msg)
    if event.isError then
        statusText:setTextColor( 255, 0, 0 )
        statusText.text = "Error Loading Ad"
        statusText.x = display.contentWidth * 0.5
        local screenGroup = self.view
        showAd( "banner" )
    else
    end
end
if appID then
    ads.init( provider, appID, adListener )
end
local sysModel = system.getInfo("model")
local sysEnv = system.getInfo("environment")
    showAd = function( adType )
    local screenGroup = self.view
    local adX, adY = display.screenOriginX, 400
    statusText.text = ""
    ads.show( adType, { x=adX, y=adY } )
end
if sysEnv == "simulator" then
else
local screenGroup = self.view
    showAd( "banner" )
end

嗨,我如何在屏幕更改中关闭或销毁"admob"广告?

您需要

在创建的每个场景中都要求广告

将此行添加到使用广告插件的每个Lua文件中

local ads = require("ads")
ads.hide( )
ads:removeSelf()
ads=nil

您可以在上面插入到任何事件侦听器或其他内容中。

您需要在每个屏幕中调用此函数。

如果广告那么

ads.hide()

结束

注意:当您生成动态广告时,广告之间有一段时间。因此,如果广告在屏幕上,它将隐藏,否则不会。但是,当您不检查 if 条件并移动到下一个场景时,广告将再次加载。

最新更新