Intergrating CPMStar Ads


(Granit Bajraktari) #1

I tried implementing CPMStar Ads in my game , but the official CPMStar readme is only for flash CS. I searched around for hours yet I can’t find a solution.

This is my code for now:

`package { import flash.display.Sprite; import net.flashpunk.Engine; import net.flashpunk.FP; import net.flashpunk.graphics.Image; import net.flashpunk.World; import flash.display.MovieClip; import flash.display.DisplayObject;

public class Main extends Engine {
	
	public static var mainMenuWorld:MainMenuWorld;
	
	private var ad:AdLoader;
	
	public function Main() {
		
		super(400, 600);
		
		mainMenuWorld = new MainMenuWorld();
	}
	
	override public function init():void {
		
		var adBox: MovieClip = new MovieClip();
		var CPMStarContentSpotID:String = "xxxxxxxxxxxxxxx"; //spot id
		var ad: DisplayObject = new AdLoader(CPMStarContentSpotID);
		adBox.addChild(ad);
		
		FP.world = mainMenuWorld;
		
	}
	
	
}

}`

And the AdLoader:

`package { import flash.display.; import flash.events.; import flash.net.; import flash.system.;

public class AdLoader extends flash.display.Sprite {
	
	private var cpmstarLoader:Loader;
	private var contentspotid:String;
	public function AdLoader(contentspotid:String) {
		this.contentspotid = contentspotid;
		addEventListener((Event.ADDED_TO_STAGE), addedHandler);
	}
	private function addedHandler(event:Event):void {
		removeEventListener(Event.ADDED_TO_STAGE, addedHandler);			
		Security.allowDomain("server.cpmstar.com");
		var cpmstarViewSWFUrl:String = "http://server.cpmstar.com/adviewas3.swf";
		var container:DisplayObjectContainer = parent;
		cpmstarLoader = new Loader();
		cpmstarLoader.contentLoaderInfo.addEventListener(Event.INIT, dispatchHandler);
		cpmstarLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, dispatchHandler);
		cpmstarLoader.load(new URLRequest(cpmstarViewSWFUrl + "?contentspotid="+contentspotid));
		addChild(cpmstarLoader);
	}
	private function dispatchHandler(event:Event):void {
		dispatchEvent(event);
	}
}

} `


(Adam Edney) #2

Did you get anywhere with this? PS link to your games?


(Granit Bajraktari) #3

Actually I got nothing , and here’s a link to a game , but it’s meh.

I created a preloader, because supposedly that’s where they should be placed , but it still doesn’t work.

` package { import flash.display.DisplayObject; import flash.display.MovieClip; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.text.TextField; import flash.utils.getDefinitionByName; import CPMStar.AdLoader;

public class Preloader extends MovieClip {
	private var
	loadedPercent:TextField = new TextField(),
	loadedPercentText:TextField = new TextField();
	
	public function Preloader() {
		if (stage) {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
		}
		
		addEventListener(Event.ENTER_FRAME, checkFrame);
		loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
		loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
		
		// Show Loader
		loadedPercentText.selectable = false;
		loadedPercentText.text = " % loaded";
		loadedPercentText.autoSize = "center";
		loadedPercentText.x = (stage.stageWidth >> 1) + 20;
		loadedPercentText.y = (stage.stageHeight >> 1);
		addChild(loadedPercentText);
		
		loadedPercent.selectable = false;
		loadedPercent.autoSize = "center";
		loadedPercent.x = (stage.stageWidth >> 1);
		loadedPercent.y = (stage.stageHeight >> 1);
		addChild(loadedPercent);
	}
	
	private function ioError(e:IOErrorEvent):void {
		trace(e.text);
	}
	
	private function progress(e:ProgressEvent):void {
		// Update Loader
		loadedPercent.text = String(int(100 * e.bytesLoaded / e.bytesTotal));
	}
	
	private function checkFrame(e:Event):void {
		if (currentFrame == totalFrames) {
			stop();
			loadingFinished();
		}
	}
	
	private function loadingFinished():void {
		removeEventListener(Event.ENTER_FRAME, checkFrame);
		loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
		loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
		
		// Remove Loader
		//removeChild(loadedPercent);
		//removeChild(loadedPercentText);
		//loadedPercent = null;
		//loadedPercentText = null;
		
		var adBox: MovieClip = new MovieClip();
		var CPMStarContentSpotID:String = "14390Q0ACA480D"; //spot id
		var ad: DisplayObject = new AdLoader(CPMStarContentSpotID);
		
		addChild(adBox);
		adBox.addChild(ad);
		
		
		adBox.x = 200;
		adBox.y = 200;
		
		//startup();
	}
	
	private function startup():void {
		var mainClass:Class = getDefinitionByName("Main") as Class;
		addChild(new mainClass() as DisplayObject);
	}
}

}`

Pay attention only to the loadingFinished function.


(Adam Edney) #4

Hi granit_bajraktari (I have no idea how to say your name :slight_smile: )

I think the problem is with your URL: http://server.cpmstar.com/adviewas3.swf?contentspotid=14390Q0ACA480D doesn’t lead anywhere.

When I try using one of my .swf files from my website it works no problem.

	removeEventListener(Event.ADDED_TO_STAGE, addedHandler);			
Security.allowDomain("*");
var cpmstarViewSWFUrl:String = "http://topbananagames.co.uk/Tutorial files/tutorial4/example 1.swf";

var container:DisplayObjectContainer = parent;
cpmstarLoader = new Loader();
cpmstarLoader.contentLoaderInfo.addEventListener(Event.INIT, dispatchHandler);
cpmstarLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, dispatchHandler);
cpmstarLoader.load(new URLRequest(cpmstarViewSWFUrl));
addChild(cpmstarLoader);

Are you 100% sure that that URL is correct for your account?

I’m trying to make an account with them now, need to see if i’m accepted.

Thanks, Adam


(Granit Bajraktari) #5

At first I thought It wasn’t accessible directly from my browser for whatever reason. I don’t know if that URL is correct but that’s the one they gave to me.

Since I’m sure now that the error is in the URL I’ll contact CPMStar.

Also don’t worry if it takes long to get accepted. For me it took like 1 month.