
I wrote little classes to generate loading animations. A simple circle spawns other circles that disappear after a few ms. I intended to use the animation in a few other classes handling external content loading.
But, once I got the stuff up and running, I noticed the CPU usage climbs pretty high (around 30%) and doesn’t decrease once the animations are removed.
I tried everything I could think of, removeChild, instance = null and so on. Do you guys have a magical solution to that problem?
Here are the two classes:
The Auto disappearing Circle
package net.calii.flash.anim { import flash.display.Sprite; import flash.events.Event; import caurina.transitions.Tweener; /** * … * @author nicolas bousquet dencuff - [email]nicolas.bousquet.dencuff@gmail.com[/email] */ public class DecayingCircle extends Sprite { public function DecayingCircle(color:uint=0×333333, x:int=0,y:int=0,rad:uint=5) { //trace ("popping new dc at x:" + x + " y:"+y); this.x = x; this.y = y; graphics.beginFill(color, .8); graphics.drawCircle(0, 0, rad); graphics.endFill(); Tweener.addTween(this, { alpha:0, time:2, transition:“easeOutQuad”, onComplete:deleteSelf } ); } private function deleteSelf():void { try { this.parent.removeChild(this); } catch (e:Error) { } graphics.clear(); delete this; } } }
And the Animation Class:
package net.calii.flash.anim { import flash.display.Sprite; import caurina.transitions.Tweener; import flash.events.Event; import flash.events.TimerEvent; import flash.geom.Point; import flash.utils.Timer; import net.calii.flash.utils.WeakRef; /** * … * @author nicolas bousquet dencuff - [email]nicolas.bousquet.dencuff@gmail.com[/email] */ public class LoadingIndicator extends Sprite { protected var _decArray:Array = new Array(); protected var circ:Sprite = new Sprite(); protected var w:uint; protected var color:uint; protected var rad:uint; protected var previousX:int = 0; protected var previousY:int = 0; protected var coef:int = 1; protected var spawnDist:int; protected var _nuked:Boolean = false; public function LoadingIndicator(color:uint=0×333333, w:uint = 50, rad:uint = 5, aspawnDist:int=10):void { this.w = w; this.color = color; this.rad = rad; this.spawnDist = aspawnDist; drawCirc(color); initMovement(); circ.addEventListener(Event.ENTER_FRAME, onCircEnterFrame); } protected function initMovement():void { trace (“init movement”); moveRight(); } private function moveRight():void { if (_nuked) return; Tweener.addTween(circ, { x:w, time:2,transition:“easeOutQuad”, onComplete:moveLeft } ); previousX = circ.x; previousY = circ.y; coef = 1; } private function moveLeft():void { if (_nuked) return; Tweener.addTween(circ, { x:-w, time:2, transition:“easeOutQuad”, onComplete:moveRight } ); previousX = circ.x; previousY = circ.y; coef = -1; } protected function drawCirc(color:uint):void { circ.graphics.beginFill(color, 1); circ.graphics.drawCircle(0, 0, rad); circ.graphics.endFill(); addChild(circ); } protected function onCircEnterFrame (e:Event):void { var dist:Number = Point.distance(new Point(circ.x, circ.y), new Point(previousX, previousY)); if (dist >= spawnDist) { _decArray.push(new DecayingCircle(color, circ.x, circ.y,rad)); addChild(_decArray[_decArray.length - 1]); previousX = circ.x; previousY = circ.y; } } public function nuke():void { trace (“nuke”); Tweener.removeAllTweens(); _nuked = true; circ.removeEventListener(Event.ENTER_FRAME, onCircEnterFrame); for (var i:uint = 0; i < _decArray.length; i++) { removeChild(_decArray[i]); _decArray[i] = null; } _decArray = null; circ = null; delete this; } } }
The methods DecayingCicle.deleteSelf() and LoadingIndicator.nuke() may seem a little messy, I left them as they were while I was trying everything I could think of the bring the CPU load back to 0.
Thanks !
If you would like to make a comment, please fill out the form below.
Recent Comments