Archive for February, 2009

wordpress flash API – pre production

Posted in AMFPHP, Actionscript 3.0, Wordpress on February 28th, 2009 by Omar Faleh – 2 Comments

I am in my 3rd week down the ugly road of redesigning my pathetic 5 year old, HTML only website now with broken databases)

as any other flash programmer would do, we try to use everything we know in remaking our website, which eventually leaves us stuck not doing anything cos we try to integrate everything at the same time

so I decided to go with the Backend CMS option (not that I have regularly updated content or anything) but more for the search engine optimisation reasons. so I decided to go with the natural solution of using wordpress as a backend, and connecto to its database using something…. and that “something” became a whole week work of research..

turns out that, as popular wordpress is, there is no out-of-the-box solution (open source of course) that you can use to connect to it.
the closest found was Tim Wilson’s Press Connect . which provides a very nice connection, only the only way I found to use it was through AS2 with the XML.sendAndLoad() interface.. and that of course does not work if you are planning to use AS3

so I decided to take the plunge and write my own connectors, basing my self on Tim Wilson’s code, and use AMFPHP to be the middle man. production started officially last thursday, and I am hoping that by the end of the weekend I will have most of my connectors ready for use.. and will probably share it on my blog for those who need some help doing what I am trying to do..

so, stay tuned…

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller

Posted in Actionscript 3.0, Code Snippets on February 13th, 2009 by Omar Faleh – 7 Comments

one of those little things that are not clearely documented or explained anywhere on the Adobe flash documentation..
I was calling a function to load an image and add it to stage, and everytime I called that image, I was getting this error:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller

after 2 hours of trials and errors and 1 hour of web search, I finally found the answer below.

within this code:

package{
	import flash.display.DisplayObject;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.net.URLRequest;
public class img extends Sprite
{
	private var loader:Loader = new Loader();
	...
	..
	public function loadBgImage():void
	{
		var randDate:Date = new Date();
		if (holder.getChildByName("bgImg"))
		{
			if (holder.contains(holder.getChildByName("bgImg")))
				holder.removeChild(holder.getChildByName("bgImg"))
		}
		var request:URLRequest = new URLRequest("images/bg.jpg?randPath="+randDate.getTime());
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
		loader.load(request);
	}
	public function completeHandler(e:Event):void
	{

		var bm:Bitmap = e.target.contentas Bitmap;
		bm.smoothing = true;
		var sp:Sprite = new Sprite();
		sp.addChild(bm);
		sp.name = "bgImg";
		holder.addChild(sp);
	}
}

it works fine the first time.. however, while in runtime, if you try to call loadBgImage() again, you will get this error:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller

and after lots of research, it turns out that this occurs when the Loader is declared as a global variable in the class (i.e: when we don’t say :var loader:Loader = new Loader() every time we call the function) and the load() function is called again after the first load

when the COMPLETE event is fired, if we add the loader to stage as a e.target.content, then we are actually adding the only instance of that loader (Loader is a DisplayObject after all)
so, if we try to remove it in the load function and remove it before calling the function again:

holder.removeChild(holder.getChildByName("bgImg"))

then we are actually removing the only instance of that loader, so when we try to call it again onloader.load(), the instance is not found..

Thanks to the fine gentelmen at Actionscript.or, this discussion solved my problem:
http://www.actionscript.org/forums/archive/index.php3/t-138634.html

so, the best way to do this is:
1- cast the content to a display Object, and unload the loader, and then add the newly created display Object to the stage : see below


public function completeHandler(e:Event):void
{
	var theImage:DisplayObject = e.target.content;
	loader.unload()
	var bm:Bitmap = theImage as Bitmap;
	bm.smoothing = true;
	var sp:Sprite = new Sprite();
	sp.addChild(bm);
	sp.name = "bgImg";
	holder.addChild(sp);
}

or, the second way to do this is:
2- get the bitmap data of that loaded content, make a clone, instantiate a new bitmap based on that bitmap data, and add that one to stage like this:


public function completeHandler(e:Event):void
{

	var bm:Bitmap = new Bitmap(((e.target.content as Bitmap).bitmapData as BitmapData).clone());
	bm.smoothing = true;
	var sp:Sprite = new Sprite();
	sp.addChild(bm);
	sp.name = "bgImg";
	holder.addChild(sp);
}