Sprite creation and performance test


This is one of my first tests using AS3. What I wanted to test here was the new way to create visual elements on the stage and the rendering performance. By usin g the new event system I create 10000 randomly shapes and apply a Fade effect to them.

The result IMHO is awesome, the player behaves very well with such a lot of animated elements on the stage.

Note that I've used the new mx.effects package to apply the fade effect. The target of this classes must be a UIComponent or a subclass of it, so that Circle and Square classes inherit from UIComponent.

Also note that from now on if we want to use the old onEnterFrame event we must explicitly subscribe to it. This also happens with all other events like onPress, onRelease, etc.

package {
	import flash.display.Sprite;
	import flash.events.EventType;
	import flash.events.Event;
	import flash.geom.Point;
	import mx.effects.Fade;
	import mx.events.EffectEvent;
	import mx.core.UIComponent

	public class RandomSprites extends Sprite {
		private var j:int = 0;

		public function RandomSprites () {
			addEventListener(EventType.ENTER_FRAME,onEnterFrame);
		}

		private function onEnterFrame (evt:Event) {
			var i:int;
			var sp:UIComponent;
			var p:Point;
			var e:Fade;
			j++;

			switch (getRandom(1,2)) {
				case 1:
					sp = UIComponent (new Circle(color));
					break;
				case 2:
					sp = UIComponent (new Square(color));
					break;
			}
			p = point;
			sp.x = p.x;
			sp.y = p.y;

			addChild(sp);

			e = new Fade();
			e.target = sp;
			e.alphaFrom = 100;
			e.alphaTo = 0;
			e.duration=2000;
			e.repeat = 0;
			e.playEffect();
			e.addEventListener(EffectEvent.EFFECT_END,effectEnd);
			if (j > 10000) {
				removeEventListener(EventType.ENTER_FRAME,onEnterFrame,false);
			}
		}

		private function effectEnd (event:Event) {
			var e:Fade = Fade (event.target);
			var tmp:int;

			tmp = e.alphaTo;
			e.alphaTo = e.alphaFrom;
			e.alphaFrom = tmp;
		}

		private function get point ():Point {
			return new Point (getRandom (0,400),getRandom(0,400));
		}

		private function get color ():Number {
			return Math.round (Math.random() * 0xFFFFFF);
		}

		private function getRandom( minVal:Number, maxVal:Number):Number {
			return Math.round(minVal + Math.random() * (maxVal - minVal));
		}
	}

	private class Circle extends UIComponent {
		public function Circle(color:uint) {
			super();
            className="Circle";

			graphics.beginFill(color,100);
			graphics.lineStyle(1,color,100);
			graphics.drawCircle(20,20,30);
		}
	}

	private class Square extends UIComponent {
		public function Square(color:uint) {
			super();
            className="Square";

			graphics.beginFill(color);
			graphics.lineStyle(1,color,100);
			graphics.drawRect(0,0,20,20);
		}
	}
}

To test the code your self create a new Project on Flex Builder 2.0. Create a new ActionScript file. On the file explorer view right click your file and select Application Management -> set as default application. Then run it.

This is another new capability. The old _root now can be binded to a class.

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts
Cambiamos de idioma
Flex 2 - AS3

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!



Bad Behavior has blocked 359 access attempts in the last 7 days.