Tweensy process using object pooling

2 views
Skip to first unread message

isaac

unread,
Jul 22, 2009, 7:41:22 PM7/22/09
to Tweensy
I'm trying to figure out how to use object pooling correctly, but so
far, I'm just getting strange behavior (like tweens breaking mid-
animation).

If anyone is familiar with using object pooling, a quick example
showing how it would be used when tweening multiple display objects
would be very helpful.

Do you have to create separate tweensy instances for each obj tween to
using object pooling correctly?

Thanks much, Isaac

Piergiorgio Niero

unread,
Jul 23, 2009, 3:41:21 AM7/23/09
to twe...@googlegroups.com
Hi isaac,

here's an example

package
{
    import com.flashdynamix.motion.TweensyGroup;
    import com.flashdynamix.motion.TweensyTimeline;

    

    import fl.motion.easing.Cubic;

    

    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;

[SWF(frameRate="31",backgroundColor="0x000000")]
    public class TweensyGroups extends Sprite
    {
    private const AMOUNT:int = 10;

    

        private var _s:Shape;

        

        private var _shapes:Vector.<Shape>;

        

        private var _timeline:TweensyTimeline;
        private var _group:TweensyGroup;

        

        private var _isPaused:Boolean = false;

        public function TweensyGroups()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;

            

            stage.addEventListener(Event.RESIZE,init);
        }


        private function init(e:Event):void
        {
        stage.removeEventListener(Event.RESIZE,init);

            _group = new TweensyGroup(false,true);

        

        _shapes = new Vector.<Shape>();

        

        var s:Shape;
        var t:TweensyTimeline;
        for(var i:int=0;i<AMOUNT;++i){
        s = new Shape();
        s.graphics.beginFill(Math.random() * Math.random() * 0xFFFFFF);
            s.graphics.drawRoundRectComplex(-20, -20, 40, 40, 8, 8, 8, 8);
            s.x = Math.random()*stage.stageWidth;
            s.y = Math.random()*stage.stageHeight;
            s.graphics.endFill();
            _shapes[_shapes.length] = s;
            addChild(s);

            

            t = _group.to(s,{x:Math.random()*stage.stageWidth, y:Math.random()*stage.stageHeight},3.0);
            t.repeatType = TweensyTimeline.YOYO;
            t.delayStart = Math.random()*1.0;
            t.ease = fl.motion.easing.Cubic.easeInOut;
        }
        }

                

    }
}

With object pooling enabled I set lazymode to false so you ought to handle your timelines' lives.
As you can see i have only one tweensygroup and many timelines each with its own properties.
A good way to handle timelines could be put each timeline in a dictionary using the tweened object as a key (something like mydict[s] = t in the previous example). In this way you can check the dictionary before instantiate a new timeline for an already tweening object (which can generate an unpredictable error, remember you're in lazymode=false) and choose whether to use an updateTo command or a simple to\fromTo.

i hope it helps.
bye

Piergiorgio Niero
TheFlashMind AUG - Italy

andré felipe

unread,
Jul 24, 2009, 12:04:20 AM7/24/09
to Tweensy
Hi Piergiorgio, I can't stop wondering.. Why 31 fps??? and not 30 ?
unrelated question, I know, but I couldn't resist

best!

Piergiorgio Niero

unread,
Jul 24, 2009, 2:58:05 AM7/24/09
to twe...@googlegroups.com
It's an old trick still in my habits :)
Long time ago (2001) in flash 5\6 there was a bug in fps: if you were targeting macs cpus were frying unless you set fps to "plateau values". those values were the only able to make to sync old ppc's clock with flash player's clock and make the movie not too cpu intensive.
however this trick is no longer required and you can use whatever fps you want but you know... habits are hard to unlearn :)


Piergiorgio Niero 
TheFlashMind AUG - Italy 

andre

unread,
Jul 24, 2009, 2:16:58 PM7/24/09
to twe...@googlegroups.com
: )  That's very Cool Piergiorgio.
I used to have a hard time with flash on macs up to flash8.. it was hard to listen from your coworkers "hey my pc is much faster than your mac!".  So maybe this was related.
Gladly this is history and Macs still the best!

Thanks for the reply!

andré








Reply all
Reply to author
Forward
0 new messages