Created
May 12, 2009 02:43
-
-
Save jubishop/110282 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" | |
applicationComplete="applicationComplete(event)" > | |
<mx:Script><![CDATA[ | |
import mx.events.FlexEvent; | |
import mx.controls.Image; | |
import flash.events.Event; | |
private var imgURL:String = "http://xerto.free.fr/toad1.jpg"; | |
// Depot, put and get. | |
private static var pool:Dictionary = new Dictionary(); | |
public static function get(type:Class):* { | |
if (!pool[type]) pool[type] = new Array(); | |
return (pool[type].length > 0) ? pool[type].pop() : new type(); | |
} | |
public static function put(item:*):void { | |
pool[item.constructor].push(item); | |
} | |
private function applicationComplete(event:FlexEvent):void { | |
// Add 100 images | |
for (var i:int = 0; i < 10; i++) | |
createNewImage(); | |
// Now we'll add one image, and remove one, every frame | |
addEventListener(Event.ENTER_FRAME, swapImages); | |
} | |
// Removes an image, and adds a new one. | |
private function swapImages(event:Event):void { | |
removeImage(); | |
createNewImage(); | |
} | |
// Removes a random image from the display tree | |
private function removeImage():void { | |
put(removeChild(getChildAt(Math.floor(Math.random() * numChildren)))); | |
} | |
// Adds and randomly places an image in the display tree | |
private function createNewImage():void { | |
var image:Image = get(Image); | |
image.source = imgURL; | |
image.x = Math.random() * stage.stageWidth; | |
image.y = Math.random() * stage.stageHeight; | |
addChild(image); | |
} | |
]]></mx:Script> | |
</mx:Application> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment