Quick Tip:Use a matrix to multiply an object in AS 3.0

This is a method frequently used among flash developers, when they want to multiply an object. Is not hard to understand I’m sure that this will be a piece of cake for you guys.

First, create a new class in your project and name it Object, this will be the class that will draw the object that we will multiply. Let’s take a look in our file(Object.as):

package hc
{
	import flash.display.Sprite;

	public class Object extends Sprite
	{
		public function Object():void
		{
			with(this.graphics)
			{
				beginFill(0x333333,1);
				drawRect(10,10,10,10);
				endFill();
			}
			
		}
	}
}

As you can see in the Object class I only draw an object. Now let’s see how our main class looks like:

package 
{
	import flash.display.Sprite;
	
	import hc.Object;
	
	public class main extends Sprite
	{
		private static const DISTANCE_X:Number = 15;
		private static const DISTANCE_Y:Number = 15;
		
		private var container:Sprite = new Sprite();
		private var numberX:Number = 10;
		private var numberY:Number = 10;
		
		public function main()
		{
			for(var i:int = 0; i < numberX; i++)
			{
				for(var j:int = 0; j < numberY; j++)
				{
					var obj:hc.Object = new hc.Object(); 
					obj.x = DISTANCE_X * i;
					obj.y = DISTANCE_Y * j;
					container.addChild(obj);
					addChild(container);
					
				}
				
			}
			
		}
	}
}

Make sure that your Object class is inside a folder(package).

Comments(3)
  1. Yolanda April 9, 2012
  2. Greg June 14, 2012
  3. Horia Condrea June 14, 2012

Leave a Reply

Your email address will not be published. Required fields are marked *


*

WordPress SEO