Skip to content

Instantly share code, notes, and snippets.

@droidalex
Created September 19, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save droidalex/b517b681e688c3734fe79c75e9a00f24 to your computer and use it in GitHub Desktop.
Save droidalex/b517b681e688c3734fe79c75e9a00f24 to your computer and use it in GitHub Desktop.
My Simple Banner
package;
import flixel.FlxGame;
import openfl.Lib;
import openfl.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.Event;
import openfl.display.Bitmap;
import flash.events.MouseEvent;
/*
* This is not haxeflixel code. It does have "Sprite","Bitmap" and "AddChild" among others.
* The banner is shown in a upper layer. I've tried so hard to put together a loaded Bitmap and FlxSprite.
* It can be done with the help of the "Assets" and "OneOfThree" classes, but in local folder.
*
*/
class Junk extends Sprite
{
public var myLoader:Loader;
public var banner:Bitmap;
public var site:String;
public var banner_width:Int;
public var banner_height:Int;
public static var show:Bool=false;
public function new()
{
super();
// Loading someone's mobile banner that I've found in google images.
myLoader = new Loader();
myLoader.load(new URLRequest("http://1.bp.blogspot.com/_p0ZTd1mmzMU/TIQXL67HhZI/AAAAAAAAAHY/6Q9jQ12Ifhk/s320/your_ad_here.png"));
//myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
addEventListener(MouseEvent.CLICK, onMouseClick);
}
public static function showBanner(cmd:Bool=true)
{
show=cmd;
}
public function onLoaded(e:Event):Void {
//----Settings----
site="http://www.haxeflixel.com";
banner_width=320;
banner_height=50;
//----Settings----
// Getting the screen's dimensions.
var screen_width=Lib.current.stage.stageWidth;
var screen_height=Lib.current.stage.stageHeight;
//Getting the banner image data.
banner = e.target.content;
//Positioning the banner.
banner.x=((screen_width/2)-(banner_width/2));
// In my Smartphone Sony the banner is not seen. So I change to: screen_height - banner size x 2.
banner.y=(screen_height - (banner_height*2));
//This is necessary to display in the right place.
banner.width=banner_width;
banner.height=banner_height;
// Using Bool is not the best way to do this. I just don't know how to trigger the banner from a FlxState.
if (show==true)
{
this.addChild(banner); // This should work. Used to work.
}
else
{
}
//addChild(banner); //<--- This works like a charm as you can see. But it should be show where I want to.
}
/*
public function onComplete(e:Event):Void
{
I can't "addChild(banner)" from here. This is an issue.
}
*/
private function onMouseClick(e:MouseEvent):Void
{
//Opening the user's browser in the address site.
Lib.getURL (new URLRequest (site));
}
}
package;
import flixel.FlxGame;
import openfl.Lib;
import openfl.display.Sprite;
class Main extends Sprite
{
public function new()
{
super();
//--------- Some modifications to enable the banner.
var game = new FlxGame(0, 0, MenuState,true);
addChild(game);
game.addChild(new Junk());
//--------- Some modifications to enable the banner.
}
}
package;
import flixel.FlxG;
import flixel.util.FlxColor;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.math.FlxMath;
// Class just for demonstate. This would be a splashscreen with no banner in it.
class MenuState extends FlxState
{
override public function create():Void
{
super.create();
FlxG.camera.bgColor=FlxColor.BLACK;
var button:FlxButton = new FlxButton(10, 10, "Playstate", OnClickButton);
add(button);
}
function OnClickButton():Void
{
FlxG.switchState(new PlayState());
}
override public function update(elapsed:Float):Void
{
super.update(elapsed);
}
}
package;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.util.FlxColor;
import flixel.math.FlxMath;
// This is the in game state. The banner should be seen.
class PlayState extends FlxState
{
override public function create():Void
{
super.create();
FlxG.camera.bgColor=FlxColor.GREEN;
}
override public function update(elapsed:Float):Void
{
super.update(elapsed);
//This worked sometimes here in update. Tried to show/hide banner and it doesn't work anymore. It's a mistery.
Junk.showBanner(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment