Best viewed in Firefox

Awesty Productions

Drawing and Making Movieclips through ActionScript

February 10th, 2007 by awesty

In this tutorial you will learn how to make MC’s (MovieClips) through actionscript and how to draw with actionscript so that those MC’s aren’t blank.

First of all, open a new flash document and hit F9 (Window>Actions) to open the actions panel. Insert this code:

root.createEmptyMovieClip("my_mc",getNextHighestDepth());

The createEmptyMovieClip function creates a new blank MC. It has 2 parameters. The first one, a string, is the name for the MC (Its instance name I suppose), so you can refer back to it. The second parameter, a number, is the depth for the MC. I put getNextHighestDepth, which gets the next highest depth so you don’t have to worry about it. Two MC’s can’t have the same depth.

Now, insert this code into the actions panel. This will draw a 25×25 square:

my_mc.lineStyle(1,0×000000);
my_mc.lineTo(25,0);
my_mc.lineTo(25,25);
my_mc.lineTo(0,25);
my_mc.lineTo(0,0);

Now if you hit ctrl+Enter you should see a square up in the top left corner of the stage. The first line chooses the style of the line. It has 8 parameters, but we will only worry about the first 3 for now. The first one is the line thickness. If you put 3, the line will be 3 pixels thick. The second parameter is the color of the line. 0xRRBBGG. Black is 0×000000, white is 0xFFFFFF. If you don’t know what a color is, open the color mixer and it will show you something like #CC6633. Just replace the # with 0x and flash will produce that color. The third parameter is the alpha of the line. If this is 0 the line will be completely transparent. If it is 100 it will be solid.
The lineTo function draws a line to the points provided (The first two parameters). The first parameters is the _x coordinate you want it to draw to, the second is the _y coordinate. The line will start at 0,0.

Your shape looks pretty boring at the moment, so why don’t you mess around with the lineStyle and add this the line after you create the MC?

my_mc.beginFill(0x006633,100);

If you test your movie (Ctrl+Enter), your MC should be a greeny color. But drawing squares is pretty boring after a while, so how about a star?

_root.createEmptyMovieClip("my_mc",getNextHighestDepth());
my_mc.beginFill(0×0099CC,100);
my_mc.lineStyle(3,0×0066CC);
my_mc.moveTo(0,50);
my_mc.lineTo(25,0);
my_mc.lineTo(50,50);
my_mc.lineTo(0,15);
my_mc.lineTo(50,15);
my_mc.lineTo(0,50);

If you test your movie (Ctrl+Enter) you should now have a star. I you look at the code, you should see a function I havent used yet, moveTo. You can use it so that the line doesnt start at 0,0. For this I used it so the line would start in the bottom left corner of the MC. It has the same parameters as the lineTo function.

You can apply code to dynamically created MC’s just like any other MC.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

21 Comments »

Comment by KIM
2007-02-10 22:25:16

hey, nice tute, just what i was looking for at this moment

 
Comment by KIM
2007-02-10 23:13:36

1.i want to know how to make a circle
2.i figured out an easier way to do it
A:i used the ruler or grid
3.thanks for the cool tutorial:)

 
Comment by Bob
2007-02-11 00:30:33

Yah good tute.

-

Emanuele Feronato (where I get all my other tutes from) made a great tutorial similar to this.

http://www.emanueleferonato.co.....rs-part-1/

 
Comment by awesty
2007-02-11 12:25:46

@KIM: Okay, I will look into making circles, but what do you mean you used the ruler and grid?

 
Comment by And Mar
2007-02-11 15:16:19

I copied and pasted the star code but it gave an error.

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: ‘)’ or ‘,’ expected
my_mc.beginFill(0×0099CC,100);

Total ActionScript Errors: 1 Reported Errors: 1

 
Comment by awesty
2007-02-11 20:49:21

What version of flash are you using?

 
Comment by Bob
2007-02-12 12:01:10

New fighting game! (I’s is posting in this topic because it’s the newest) Anywho. It takes what you’ve taught me and formed an awesome fighting AI.

http://www.deviantart.com/deviation/48469918/

Tell me what you think

 
Comment by awesty
2007-02-12 16:01:22

Yea, I saw that. Its pretty cool but you need to work on the animation a bit.

 
Comment by Bob
2007-02-12 21:53:06

Yah, The animation was at best…sloppy. I just wanted to get a workiong fighting engine for a game a friend of mine will animate.

 
Comment by big al
2007-02-14 01:49:03

nice tut. can u do tweens with actionscript

 
Comment by awesty
2007-02-14 16:41:51

Yes.

 
Comment by djodin
2007-02-18 00:08:16

nice tutorial! =D
luv it!

 
Comment by Bob
2007-02-20 07:46:01

Hey, I have some stuff I need help with live. So I was wondering if you had AIM or MSN messengers. If you want, but dont want to disclose personal information to the public through a comment, just send it you gamejocky@hotmail.com.

It would help a bunch.

 
Comment by awesty
2007-02-20 18:24:31

sniper_rifle_048[@]hotmail[.]com

 
Comment by KIM
2007-03-04 21:18:14

… i’m about to get bored…

can you make a tute on how to make a circle and/or square through action scripts?

 
Comment by awesty
2007-03-05 16:40:28

Did you read the tutorial?

 
Comment by Spleen
2007-03-05 17:37:31

@KIM: Here is a tutorial on how to draw a circle with actionscript: http://www.actionscript.org/fo.....adid=30328
All you had to do was google for it, so stop bugging awesty.

 
Comment by Eblup
2007-03-28 11:40:07

hey iam back i have been out of luck. my hard drive died so i had to get another computer and get flash. i need help agin i dont have any of my old codeing word documents so i need help recreating my jumping code can you tell me what is wroung with it.
onClipEvent (load) {
j = 0;
c = 1;
}

onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
if (c > 0) {
j = -15;
_y -= 6;
c = 0;
}
}
}

onClipEvent (enterFrame) {
if (j > 6) {
j = 6;
}
}

onClipEvent (enterFrame) {
if(_root.ground.hitTest(_x (_width/2),_y,true)){
this._x -= 8;
}
if(_root.ground.hitTest(_x-(_width/2),_y,true)){
this._x = 8;
}
if(_root.ground.hitTest(_x,_y (_height/2),true)){
j = 0;
c = 1;
}else{
j += 1;
this._y += j;
}
if(_root.ground.hitTest(_x,_y-(_height/2),true)){
j = 3;
}
}

onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
_x += 8;
_xscale = 100;
_root.guy.gotoAndStop(2);
}else if (Key.isDown(Key.LEFT)) {
_x -= 8;
_xscale = -100;
_root.guy.gotoAndStop(2);
}else{
_root.guy.gotoAndStop(1);
}
}

 
Comment by akskater100
2007-03-29 07:59:39

whenever i paste the code in it says error. how do i fix it?

 
Comment by awesty
2007-04-05 13:16:12

@Eblup: What is going wrong with the code?

@akskater100: What are the errors you are getting?

 
Comment by BK Subscribed to comments via email
2008-06-02 05:57:04

thanks a bunches ^^!

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.