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).
[kml_flashembed movie=”http://www.horiacondrea.com/wp-content/uploads/article/Use%20a%20matrix%20to%20multiply%20an%20object/Other.swf” height=”480″ width=”620″ fversion=”9″ useexpressinstall=”true” /]

You can follow and like us:

Author: Horațiu Condrea

My name is Horațiu Condrea, and I work as a Software Developer Manager at Siemens PLM Software. I hope that my experiments will prove to be useful for many of you guys/girls out there. Don’t forget to leave a comment whenever you run over a bug or something that is not working as it should be. For any kind of information contact me.

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

  1. If you want to do something is 3D, you will probably want to use one of the following : papervision3d, alternativa3d etc.. Because when you work in 3D space, you will have to face lots of problems. Like: view, X rotation, Y rotation and more.

    But, if you want to do some experiments, you can easily add another loop, and you will have something like this:

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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.