Coder Profile - Show off your skills, get a coder profile.
 
 
 
Actionscript API
Programming
What Is API?

API is one very useful technique in Flash. It is basically using code to draw objects using vector animation. For example, you could make a box, circle, or whatever!


What Is The Point Of API?

You can make whole games out of it, make the screen fade in and out, or general tasks that are either extremely difficult, large and/or time consuming. For example, say you want to make a draggable peice of string http://www.coderprofile.com/pinned-code/163/as-string in Flash. If you weren't using API, you would have to make in exess of 50 movieclips and program them all to follow one another in succession. If you made it in API, you could make a simple array and a for() loop to create every movieclip dynamically and attach each using the API lineTo() function (more will be explained on that later).


How Do I Use API?

Very good question! Well, there are a variety of functions that you can use to make API, but we will cover the basics here, nothing fancy-pantsy. Here's a list of functions:

createEmptyMovieClip()
moveTo()
lineTo()
lineStyle()
beginFill()
endFill()
clear()


How Do I Use These API Funtions?

Woah! Slow down there skippy! First things first, you almost always must have a movieclip to create API. If you aren't using a pre-made movieclip, you can make one dynamically using the createEmptyMovieClip() function.

The parameters for the createEmptyMovieClip() function are as follows:
Code Copy / Restore
  1. [heirarchy].createEmptyMovieClip(instanceName, depth);
The heirarchy is where you want to create the movieclip. you can use _root, this, _parent or _level0 for where you want to place the movieclip. The most used is _root. Next, is your movieclip's instance name. An instance name is a unique name given to a movieclip or button so you can call on it when neccessary. Just like your first name! The instance name (only when declaring it) is a string. So that means it must be within quotations [""]. Next is the depth, this is like the level on a building, but there can only be one item on a floor or else it gets removed (like a one room per level apartment building).


The next function is the moveTo() and lineTo() functions. These functions are like drawing lines on a graph. The moveTo() function is telling Flash to "take your pen off of the chart and move it here". The lineTo() function tells Flash to "draw a line from where your pen is now to here". Here is the syntax for using these:
Code Copy / Restore
  1. [movieclip].moveTo(xPosition, yPostion);
  2. [movieclip].lineTo(xPosition, yPosition);
The movieclip is the specified movieclip that you are drawing on (so if you remove the movieclip that you are drawing on so does the API art). Next the xPosition is the x-axis point on the stage the it will move/draw to. yPostion is the same as xPostion but on the y-axis of the stage.
A quick note, if you do not declare where you start drawing using moveTo(), Flash will automatically set your starting drawing coordinates to (0, 0).


Next is the lineStyle() function. This will declare how thick your line will be, the colour and the alpha (or opacity).
Code Copy / Restore
  1. [movieclip].lineStyle(thickness, colour, alpha);
Again, the movieclip is the movieclip you will be drawing with. Next is the colour in hexidecimal format (EG. red would be 0xFF0000). After is your alpha, ranging from 0 (invisible) to 100 (opaque).


After that you have the beginFill() and endFill() functions. The beginFill() function will fill in the API drawings with the fill of your choice and the specified alpha, and endFill() will stop the beginFill() function (so if you wanted to fill in your movieclip with a different colour you could stop it and recolour it with another beginFill() function).
Code Copy / Restore
  1. [movieclip].beginFill(colour, alpha);
  2. [movieclip].endFill()
The code is pretty self-explanitory from here, colour is the hexidecimal colour and alpha is the opacity of the movieclip.


Finally, the clear() function will remove all drawings in the specified movieclip or heirarchy level. If you keep updating the location of an API drawing (like a line rotating) it will redraw it, but will also leave the previous drawing there to, so after a while you have a bunch of different drawings on the screen. If you use the clear() function, it will delete the previous drawing of the movieclip and then redraw the updated location of it, so it looks like a smooth animation/transition.
Code Copy / Restore
  1. [movieclip].clear()
No need to explain here, again pretty self-explanitory.


Can You Post Some Sample API Code?

Why certainly! Here are some API drawings:

Square:
Code Copy / Restore
  1. _root.createEmptyMovieClip("square", 100);
  2. square.lineStyle(3, 0xFF0000, 75);
  3. square.beginFill(0xFF0000, 50);
  4. square.moveTo(-40, -40);
  5. square.lineTo(40, -40);
  6. square.lineTo(40, 40);
  7. square.lineTo(-40, 40);
  8. square.lineTo(-40, -40);
  9. square.endFill();
  10. square._x = (Stage.width / 2);
  11. square._y = (Stage.height / 2);
This will create a red transparent square in the middle of the stage.


Circle:
Code Copy / Restore
  1. var array:Array = [];
  2. var radius:Number = 50;
  3. _root.createEmptyMovieClip("circle", 100);
  4. for (var i = 0; i < 360; i += 10) {
  5.           X = (Math.sin(Math.PI / 180 * i) * 50);
  6.           Y = (Math.cos(Math.PI / 180 * i) * 50);
  7.           array.push([X, Y]);
  8. }
  9. circle.moveTo(array[0][0], array[0][1]);
  10. circle.lineStyle(3, 0x000000, 100);
  11. circle.beginFill(0xFFCC00, 100);
  12. for (var j = 0; j < array.length; j++) {
  13.           circle.lineTo(array[j][0], array[j][1]);
  14. }
  15. circle.endFill();
  16. circle._x = (Stage.width / 2);
  17. circle._y = (Stage.height / 2);
Creates a circle with a black stroke and an orange fill in the middle of the stage.


Hope all of this helps and if you have any questions don't hesitate to P.M. or email me! :D


Posted By mikeMarek
Please login to rate coding articles.

Click here to register a free account with us.
Comments
Please login to post comments.
Page 1 of 1
More Articles By This Author
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
if/else, switch/case and ?/: Statements In Actionscript 2.0
Recently Posted "Programming" Articles
N-Queens-Series Part I (Only the fittest survive)
Currying in JavaScript: Fun for the Whole Family!
[PHP] Create A Unique Page Hits Counter
Actionscript Events
Actionscript API
Quick Threading Tutorial - C++
C++ And Me (A Love Story)
First look at C
Introduction to Pseudo Code
Integrating your website with PHP
Recently Rated "Programming" Articles
[C++] Templates
Quick Threading Tutorial - C++
Intorduction to memoization.
N-Queens-Series Part I (Only the fittest survive)
[PHP] Create A Unique Page Hits Counter
Currying in JavaScript: Fun for the Whole Family!
Actionscript Events
First look at C
if/else, switch/case and ?/: Statements In Actionscript 2.0
Actionscript API
source codes Categories articles
Browse All
Business & E-Commerce (1)
Databases (1)
Design & Creativity (1)
Internet & Web Sites (1)
Life In General (2)
Operating Systems (3)
Other (2)
Programming (48)
Security (10)
Software Development (5)
Web Development (15)
search Search Inside
Programming
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved