Difference between revisions of "Ads.new"

From GiderosMobile
Line 33: Line 33:
 
'''adframework''': (string) name of the ad framework<br/>
 
'''adframework''': (string) name of the ad framework<br/>
  
=== Example ===
+
=== Examples ===
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
admob = Ads.new("admob")
 
admob = Ads.new("admob")
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
'''Some tweening ads'''
 +
<syntaxhighlight lang="lua">
 +
require "ads" --create real object for on device
 +
require "easing"
 +
 +
ads = Ads.new("admob")
 +
ADMOB_UNIT_ID = "ca-app-pub-3940256099942544/6300978111" -- google banner test id
 +
ads:setKey(ADMOB_UNIT_ID)
 +
ads:enableTesting()
 +
 +
local banners = {
 +
"smart_banner",
 +
"banner",
 +
"iab_banner",
 +
"iab_mrect"
 +
}
 +
local cur = 1
 +
ads:showAd(banners[cur])
 +
 +
local pos = {"center", "bottom"}
 +
ads:setAlignment(unpack(pos))
 +
 +
ads:showAd("auto")
 +
 +
local shape = Shape.new()
 +
stage:addChild(shape)
 +
 +
local text = TextField.new(nil, "Ad goes here")
 +
text:setScale(3)
 +
text:setTextColor(0xffffff)
 +
text:setPosition(50, 35)
 +
shape:addChild(text)
 +
 +
ads:addEventListener(Event.AD_RECEIVED, function()
 +
print("ads AD_RECEIVED")
 +
local width = ads:getWidth()
 +
local height = ads:getHeight()
 +
local x = math.floor((application:getContentWidth() - width)/2)
 +
local y = math.floor((application:getContentHeight() - height)/2)
 +
shape:clear()
 +
shape:setFillStyle(Shape.SOLID, 0xff0000)
 +
shape:beginPath()
 +
shape:moveTo(0,0)
 +
shape:lineTo(0,height)
 +
shape:lineTo(width,height)
 +
shape:lineTo(width,0)
 +
shape:closePath()
 +
shape:endPath()
 +
shape:setPosition(x, y)
 +
Timer.delayedCall(2000, function()
 +
local tween = GTween.new(ads, 1, {x = x, y = y}, {ease = easing.outBack, dispatchEvents = true})
 +
tween:addEventListener("complete", function()
 +
cur += 1
 +
if cur > #banners then cur = 1 end
 +
ads:showAd(banners[cur])
 +
ads:setAlignment(unpack(pos))
 +
-- ads:setPosition(0,50)
 +
end)
 +
end)
 +
end)
 +
 +
ads:addEventListener(Event.AD_FAILED, function(e)
 +
print("ads AD_FAILED", e.error)
 +
end)
 +
 +
ads:addEventListener(Event.AD_ACTION_BEGIN, function()
 +
print("ads AD_ACTION_BEGIN")
 +
end)
 +
 +
ads:addEventListener(Event.AD_ACTION_END, function()
 +
print("ads AD_ACTION_END")
 +
end)
 +
 +
ads:addEventListener(Event.AD_DISMISSED, function()
 +
print("ads AD_DISMISSED")
 +
end)
 +
 +
ads:addEventListener(Event.AD_ERROR, function(e)
 +
print("ads AD_ERROR", e.error)
 +
end)
 +
</syntaxhighlight>
 +
 +
'''[[Media:gtween.lua|Gtween.lua]]''' '''tip: right click and "Save Link As"'''
  
 
{{Ads}}
 
{{Ads}}

Revision as of 03:12, 16 February 2025

Available since: Gideros 2014.01
Class: Ads

Description

Initializes a new ad framework.

Ads.new(adframework)

Possible values for ad frameworks:

  • "adcolony" Stable
  • "admob" Stable
  • "amazon" Beta
  • "applovin" Alpha
  • "chartboost" Stable
  • "heyzap" Beta
  • "iad" Stable
  • "inmobi" Stable
  • "millenial" Stable
  • "mopub" Beta
  • "revmob" Beta
  • "samsung" Beta
  • "tapfortap" Beta
  • "tapjoy" Stable
  • "vungle" Stable
Wiki only tested with Google Admob
Some ad frameworks may not even exist anymore!

Parameters

adframework: (string) name of the ad framework

Examples

admob = Ads.new("admob")

Some tweening ads

require "ads" --create real object for on device
require "easing"

ads = Ads.new("admob")
ADMOB_UNIT_ID = "ca-app-pub-3940256099942544/6300978111" -- google banner test id
ads:setKey(ADMOB_UNIT_ID)
ads:enableTesting()

local banners = {
	"smart_banner",
	"banner",
	"iab_banner",
	"iab_mrect"
}
local cur = 1
ads:showAd(banners[cur])

local pos = {"center", "bottom"}
ads:setAlignment(unpack(pos))

ads:showAd("auto")

local shape = Shape.new()
stage:addChild(shape)

local text = TextField.new(nil, "Ad goes here")
text:setScale(3)
text:setTextColor(0xffffff)
text:setPosition(50, 35)
shape:addChild(text)

ads:addEventListener(Event.AD_RECEIVED, function()
	print("ads AD_RECEIVED")
	local width = ads:getWidth()
	local height = ads:getHeight()
	local x = math.floor((application:getContentWidth() - width)/2)
	local y = math.floor((application:getContentHeight() - height)/2)
	shape:clear()
	shape:setFillStyle(Shape.SOLID, 0xff0000)
	shape:beginPath()
	shape:moveTo(0,0)
	shape:lineTo(0,height)
	shape:lineTo(width,height)
	shape:lineTo(width,0)
	shape:closePath()
	shape:endPath()
	shape:setPosition(x, y)
	Timer.delayedCall(2000, function()
		local tween = GTween.new(ads, 1, {x = x, y = y}, {ease = easing.outBack, dispatchEvents = true})
		tween:addEventListener("complete", function()
			cur += 1
			if cur > #banners then cur = 1 end
			ads:showAd(banners[cur])
			ads:setAlignment(unpack(pos))
--			ads:setPosition(0,50)
		end)
	end)
end)

ads:addEventListener(Event.AD_FAILED, function(e)
	print("ads AD_FAILED", e.error)
end)

ads:addEventListener(Event.AD_ACTION_BEGIN, function()
	print("ads AD_ACTION_BEGIN")
end)

ads:addEventListener(Event.AD_ACTION_END, function()
	print("ads AD_ACTION_END")
end)

ads:addEventListener(Event.AD_DISMISSED, function()
	print("ads AD_DISMISSED")
end)

ads:addEventListener(Event.AD_ERROR, function(e)
	print("ads AD_ERROR", e.error)
end)

Gtween.lua tip: right click and "Save Link As"