Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Drawing with actionscript 3.0

This is a discussion on Drawing with actionscript 3.0 within the Tutorials forums, part of the Flash English category; Using Actionscript ( in this case version 3.0 ) you can create drawings at runtime. Applying some Trigonometry to Actionscript, you ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  16 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 03-10-07, 19:34
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Drawing with actionscript 3.0

Using Actionscript ( in this case version 3.0 ) you can create drawings at runtime.
Applying some Trigonometry to Actionscript, you can really do some interesting things, often useful to add some nice little details to our Flash application.
Obviously, the more Physics and Trigonometry you know, the more you get to know Actionscript, and more interesting and captivating will your animations be. In my example, I start from a basic level to give some input to people interested to learn this technique ( called Drawing API ) and you'll see later some more advanced articles on the subject.

Let's see the first sample script...



I create an FLA and I save it as ' disegno.fla ' .
I create the Document Class, an AS file saved as ' Disegno.as ' .
PS if it's not clear what a Document Class is and how to create it, it can bee rehearsed in this article: the best way to initiate the Main Class.
Let's se now how the Disegno class is implemented:
Code:
package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.SimpleButton;
	
	public class Disegno extends Sprite
	{
		private var clips_array:Array;
		
		private var centerX:Number;
		private const centerY:Number=50;
		private var radius:Number=0;
		private var angle:Number=0;
		private var counter:Number;
		
		public function Disegno()
		{
			init();
		}
		
		private function init():void
		{
			stage.frameRate=31;
			centerX=stage.stageWidth/2;
			initDrawingListener();
			initButtonListener();
		}
		
		private function initDrawingListener():void
		{
			clips_array=new Array();
			angle=0;
			radius=0;
			replay_btn.visible=false;
			addEventListener(Event.ENTER_FRAME,drawing);
		}
		
		private function initButtonListener():void
		{
			replay_btn.addEventListener(MouseEvent.MOUSE_DOWN,cliccato);
		}
		
		private function drawing(e:Event):void
		{
			var sprite:Sprite=new Sprite();
			addChild(sprite);
			clips_array.push(sprite);
			
			sprite.graphics.moveTo(centerX,centerY);
			sprite.graphics.lineStyle(.25,0x666666,.2);
			
			var xx:Number=centerX+Math.sin(angle)*radius;
			var yy:Number=centerX+Math.cos(angle)*radius;
			sprite.graphics.lineTo(xx,yy);
			
			angle+=.05;
			radius++;
			if(angle>Math.PI*2)
			{
				removeEventListener(Event.ENTER_FRAME,drawing);
				replay_btn.visible=true;
			}
		}
		
		private function cliccato(m:MouseEvent):void
		{
			callReplay();
		}
		
		private function callReplay():void
		{
			counter=clips_array.length-1;
			replay_btn.visible=false;
			addEventListener(Event.ENTER_FRAME,replay);
		}
		
		private function replay(e:Event):void
		{
			removeChild(clips_array[counter]);
			counter--;
			if(counter<0)
			{
				removeEventListener(Event.ENTER_FRAME,replay);
				initDrawingListener();
			}
		}
	}
}
Here's the result:







Let's analyze the code:

As you can see , in this script a section for the REPLY button is also implemented, created to give the user a chance to see the animation again.
I'd like to draw the attention to the method ' drawing ' ( functions within Actionscript classes are called methods ).
What exactly does it do:
An interval ( ENTER_FRAME ) holds the drawing phase
addEventListener(Event.ENTER_FRAME,drawing);
I create a sprite
var sprite:Sprite=new Sprite();
I add it to DisplayObject ( otherwise it wouldn't be visible )
addChild(sprite);
I insert it into an Array in order to recall it when I need it ( in this example I'll recall every sprite in the array to reply it )
clips_array.push(sprite);
I initiate the graphics
sprite.graphics.moveTo(centerX,centerY);
sprite.graphics.lineStyle(.25,0x666666,.2);
I create the Trigonometric variables, and as we know from Math?s, a sin and a cosin vary from -1 to 1, but multiplied by a given number ( radius ) ...
var xx:Number=centerX+Math.sin(angle)*radius;
var yy:Number=centerX+Math.cos(angle)*radius;
I tell the sprite to draw
sprite.graphics.lineTo(xx,yy);
then I increase angle and radius
angle+=.05;
radius++;
and finally I stop the interval at a given point
if(angle>Math.PI*2)
{
removeEventListener(Event.ENTER_FRAME,drawing);
}

Enjoy !
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Drawing polygons with Actionscript 3.0 Flep Tutorials 1 28-09-08 00:10
Drawing with Actionscript 3.0 - script 2 Flep Tutorials 0 27-09-07 21:00
Drawing with Actionscript 3.0 - script 3 Flep Tutorials 0 27-09-07 09:41


All times are GMT. The time now is 12:19.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap