download / 3.3 mb zip
version : 1.1.9
updated : February 18, 2011
open all | close all
upcoming HYPE events
No events.
let's keep in touch.
Keep us informed what you're working on using HYPE by connecting with us, either via e-mail or through twitter.

hype@hypeframework.org
twitter.com/hypeframework
Branden Hall

automatastudios.com
twitter.com/waxpraxis
Joshua Davis

joshuadavis.com
twitter.com/joshuadavis
flickr.com/photos/joshuadavis

Alternative content

Get Adobe Flash player

ObjectPool / 01_objectPool
import hype.extended.behavior.VariableVibration;
import hype.extended.trigger.ExitShapeTrigger;
import hype.framework.core.ObjectPool;
import hype.framework.core.TimeType;
import hype.framework.display.BitmapCanvas;
import hype.framework.rhythm.SimpleRhythm;

var myWidth = stage.stageWidth;
var myHeight = stage.stageHeight;

var bmc:BitmapCanvas = new BitmapCanvas(myWidth, myHeight);
addChild(bmc)

var clipContainer:Sprite = new Sprite();

var pool:ObjectPool = new ObjectPool(MyCircle, 200);

function addNextClip(r:SimpleRhythm) {
	pool.request();
}

var rhythm:SimpleRhythm = new SimpleRhythm(addNextClip);
rhythm.start(TimeType.TIME, 1);

pool.onRequestObject = function(clip) {
	clip.x = myWidth / 2;
	clip.y = myHeight / 2;
	clip.scaleX = clip.scaleY = 0.05 + (Math.floor(Math.random() * 3) * 0.3);
	
	// target Object, property, spring, ease, vibrationRange
	
	var xVib:VariableVibration = new VariableVibration(clip, “x”, 0.99, 0.05, 20);
	var yVib:VariableVibration = new VariableVibration(clip, “y”, 0.99, 0.05, 20);
	xVib.start();
	yVib.start();
	
	// exit callback function, target Object, shape, shapeFlag
	
	var onExit:ExitShapeTrigger = new ExitShapeTrigger(onExitShape, clip, MyExitShape, true);
	onExit.start();
	
	clipContainer.addChild(clip);
}

function onExitShape(clip):void {
	pool.release(clip);
	clipContainer.removeChild(clip);
}

bmc.startCapture(clipContainer, false);


Leave a Comment
Franck said, on November 19th, 2009 at 5:48 am

Hello,

Thanks for this good FrameWork. I try to test this exemple but i’m not sure to anderstand what is the MyExitShape…

Could you explain it to me ?

Joshua Davis said, on November 19th, 2009 at 10:27 am

Franck,

We’re using an object pool, in this case 200 objects that we want to use in the behavior on screen. but the object has movement… and if we let the object move forever… they would animate off screen.

So we create a MovieClip called “MyExitShape” and when an object falls OFF “MyExitShape” it is told to fire the onExitShape function.

The onExitShape function “RECYCLES” the clip back into the pool and back into the center of the screen again so we can re-animate the object.

hope this helps,

Dmitry said, on December 7th, 2009 at 6:49 pm

Hello guys,

This framework is really fun.
But i don’t understand how to actually release an object from ObjectPool on my own.
for example i wanna one rhythm to show objects and other one to hide object.
Simply casting a release method for desired object is crashing application for me…

Thanks

Branden Hall said, on December 8th, 2009 at 9:06 am

Hey Dmitry – we’d have to see the code to understand what’s going wrong. In general though, you’ll need to do your own cleanup (removing the object from the display tree, etc) before you go and release it.

Dmitry said, on December 8th, 2009 at 11:50 am

Here is a simple example:
*btw: If there there are no vibrations assigned to clips, application keep running throwing error otherwise it crashes…

import hype.framework.core.ObjectPool;
import hype.framework.core.TimeType;
import hype.framework.rhythm.SimpleRhythm;

var clipContainer:Sprite = new Sprite();
addChild(clipContainer);
var pool:ObjectPool = new ObjectPool(MyCircle,10);

function addNextClip(myRhythm:SimpleRhythm) {
pool.request();
}
function removeRandomClip(MyRhythm:SimpleRhythm) {
if (clipContainer.numChildren > 0) {
var index:uint = Math.floor(Math.random() * clipContainer.numChildren);
var clip = clipContainer.getChildAt(index);
try {
pool.release(clip);
} catch (e:Error) {
trace(e); // TypeError: Error #1009: Cannot access a property or method of a null object reference.
}
clipContainer.removeChild(clip);
}
}

var rhythm1:SimpleRhythm = new SimpleRhythm(addNextClip);
var rhythm2:SimpleRhythm = new SimpleRhythm(removeRandomClip);
rhythm1.start(TimeType.TIME, 100);
rhythm2.start(TimeType.TIME, 1000);

pool.onRequestObject = function(clip) {
clip.x = Math.random() * stage.stageWidth;
clip.y = Math.random() * stage.stageHeight;
clipContainer.addChild(clip);
};

Dmitry said, on December 8th, 2009 at 11:58 am

Here is workaround, but i don’t like it

var trigger = new SimpleTrigger(success,clip,releaseFromPool);
trigger.start();

function releaseFromPool(clip) {
pool.release(clip);
clipContainer.removeChild(clip);
}
function success(clip):Boolean {
return (true);
}

[...] し参照を使い回すことでパフォーマンスの低下を防ごうとしています。 http://hype.joshuadavis.com/02_examples/objectpool/01_objectpool/ FITC Tokyo 2009 で gskinner [...]

awk2 - HYPEでFlashコンテンツを透過PNG保存する said, on January 28th, 2010 at 10:03 am

[...] サンプルコードをHYPEのサイトから適当にもってきて試します。 (自分は01_objectpoolを少し改造して試します。) [...]