Here you go:
[lua]---------------------------------------------------------------------------------------
– Inneractive wrapper class
– File name: cInneractive.lua
– Usage:
– inneractive = require(“cInneractive”)
– APP_ID = “MY_Inneractive_App_ID” – use your app id here
– inneractive.Init( APP_ID )
– inneractive.Show() – no parameters required, will call Hide in 5 seconds
– or:
– inneractive.Show{
– cnt_text=“You can skip in”,
– skip_time=10,
– skip_text=“Tap to skip Advertisments”
– }
– If you have called Show(), but need to explicitly close the ad, you can do this to
– clean everything up:
– inneractive.Hide()
module(…, package.seeall)
local ads = require “ads”
local isSimulator = system.getInfo( “environment” ) == “simulator”
local fontScaleX = display.contentWidth/1024
local fontScaleY = display.contentHeight/769
– Objects needed here for cleanup
local adInfo = {}
– Forward references
local adSkipListener, adSkipGobbler
– *******************************************************************************************
– Init()
– Initialize the Ad system
– Parameters:
– appId Your Inneractive app ID
– onAdDisplayed Event callback called when
– ad is displayed
– *******************************************************************************************
function Init( appId, onAdDisplayed )
if ( isSimulator ) then
adInfo.theAdShowCallback = onAdDisplayed
print( “ads.init() cannot be called in the simulator” )
else
adInfo.theAdShowCallback = nil – Use their callback via the ads system
ads.init( “inneractive”, appId, onAdDisplayed )
end
end
– *******************************************************************************************
– Hide()
– Removes the ad and all of the display objects we created for the ad.
– *******************************************************************************************
function Hide()
if ( isSimulator ) then
print( “ads.hide() cannot be called in the simulator” )
else
– Hide the ad
ads.hide()
end
if ( adInfo.timer ) then
timer.cancel( adInfo.timer )
adInfo.timer = nil
end
– Now cleanup our objects
if ( adInfo.text ) then
adInfo.text:removeEventListener( “tap”, adSkipListener )
adInfo.text:removeEventListener( “touch”, adSkipListener )
– We don’t really need to do this here since the group remove will handle it, but
– since we are already cleaning up the event listeners we might as well remove it.
adInfo.text:removeSelf()
adInfo.text = nil
end
if ( adInfo.backImage ) then
adInfo.backImage:removeEventListener( “tap”, adSkipGobbler )
adInfo.backImage:removeEventListener( “touch”, adSkipGobbler )
– We don’t really need to do this here since the group remove will handle it, but
– since we are already cleaning up the event listeners we might as well remove it.
adInfo.backImage:removeSelf()
adInfo.backImage = nil
end
if ( adInfo.group ) then
adInfo.group:removeSelf()
adInfo.group = nil
end
end
– local function to gobble all tap/touch events for the background
function adSkipGobbler( event )
return true
end
– local function to invoke hiding the ad
function adSkipListener( event )
if ( adInfo.skip_time == 0 ) then
timer.performWithDelay( 50, Hide, 1 )
end
return true
end
– *******************************************************************************************
– Show( params )
– Parameters:
– params.x [Optional] background x position (Default is 0)
– params.y [Optional] background y position (Default is 0)
– params.w [Optional] background width (Default is display.contentWidth)
– params.h [Optional] background height (Default is display.contentHeight)
– params.color [Optional] background color (Default is { 255,255,255,255 } )
– params.interval [Optional] interval to refresh ads (Default is 60)
– params.skip_text [Optional] Text to show user they can skip the ad (Default is “Tap to Skip”)
– params.skip_font [Optional] Font used for skip_text (Default is native.systemFont)
– params.skip_time [Optional] Time (in seconds) before the Skip is active (Default is 5)
– params.cnt_text [Optional] Text to show user during the countdown (Default is “Skip in”)
– NOTES:
– If params.skip_time is 0 then the skip_text will be used as is. However, if
– skip_time is > 0 then the seconds remaining will be appending to the cnt_text
– *******************************************************************************************
function Show( params )
– we have no required parameters so we need to handle passing in nothing
if ( params == nil ) then
params = {}
end
local back_x = params.x or 0
local back_y = params.y or 0
local back_w = params.w or display.contentWidth
local back_h = params.h or display.contentHeight
local ad_interval = params.interval or 60
local cnt_text = params.cnt_text or “Skip in”
local skip_text = params.skip_text or “Tap to Skip”
local skip_font = params.skip_font or native.systemFont
local skip_time = params.skip_time or 5
local back_color = { 255, 255, 255, 255 }
local banner_width, banner_height
if ( params.color ) then
for i=1,#params.color do
back_color[i] = params.color[i]
end
end
– our group to draw to
adInfo.group = display.newGroup()
– save off values we will need later
adInfo.cnt_text = cnt_text
adInfo.skip_text = skip_text
adInfo.skip_time = skip_time
adInfo.timer = nil
– These banner sizes are from Inneractive for the iPhone/iPad. If
– you have different devices you can add a check based on sysModel
if ( math.round( display.contentWidth/display.contentScaleX ) > 480 ) then
banner_width = math.round( 766*display.contentScaleX )
banner_height = math.round( 66*display.contentScaleY )
else
banner_width = 320
banner_height = 53
end
– Center the ad in the background rect
local ad_x = back_x + math.round( 0.5*( back_w - banner_width ) )
local ad_y = back_y + math.round( 0.5*( back_h - banner_height ) )
adInfo.backImage = display.newRect( back_x, back_y, back_w, back_h )
adInfo.backImage:setFillColor( back_color[1], back_color[2], back_color[3], back_color[4] )
adInfo.group:insert( adInfo.backImage )
adInfo.backImage:addEventListener( “tap”, adSkipGobbler )
adInfo.backImage:addEventListener( “touch”, adSkipGobbler )
if ( isSimulator ) then
– These will get cleaned up when the adInfo.group is cleaned up
local dummy_ad = display.newRoundedRect( 0, 0, banner_width, banner_height, 8 )
dummy_ad:setFillColor( 0, 0, 0 )
dummy_ad:setReferencePoint( display.TopLeftReferencePoint )
dummy_ad.x = ad_x
dummy_ad.y = ad_y
adInfo.group:insert( dummy_ad )
local dummy_text = display.newText( “Dummy Ad”, 0, 0, native.systemFont, math.round( 32*fontScaleY ) )
dummy_text:setTextColor( 255, 255, 255 )
dummy_text:setReferencePoint( display.CenterReferencePoint )
dummy_text.x = display.contentCenterX
dummy_text.y = display.contentCenterY
adInfo.group:insert( dummy_text )
end
local textToUse
if ( skip_time > 0 ) then
textToUse = string.format( “%s %d”, cnt_text, skip_time )
else
textToUse = skip_text
end
adInfo.text = display.newText( textToUse, 0, 0, skip_font, math.round( 32*fontScaleY ) )
adInfo.text:setTextColor( 0, 0, 0 )
adInfo.text:setReferencePoint( display.BottomCenterReferencePoint )
adInfo.text.x = display.contentCenterX
adInfo.text.y = display.contentHeight - 4
adInfo.text:addEventListener( “tap”, adSkipListener )
adInfo.text:addEventListener( “touch”, adSkipListener )
adInfo.group:insert( adInfo.text )
if ( isSimulator ) then
print( “ads.show() not supported in the simulator” )
if ( theAdShowCallback ) then
– Since we aren’t using the actual ad system,
– so fake it with a callback
theAdShowCallback()
end
else
ads.show( “banner”, { x=ad_x, y=ad_y, interval=ad_interval } )
end
local function updateText( event )
adInfo.skip_time = adInfo.skip_time - 1
if ( adInfo.skip_time > 0 ) then
adInfo.text.text = string.format( “%s %d”, adInfo.cnt_text, adInfo.skip_time )
else
adInfo.text.text = adInfo.skip_text
end
end
if ( skip_time > 0 ) then
– Since we are using skip_time in seconds, that means we can use
– skip_time for the number of times to perform this timer event
adInfo.timer = timer.performWithDelay( 1000, updateText, skip_time )
end
end[/lua] [import]uid: 16734 topic_id: 19981 reply_id: 79092[/import]