Hi Everyone,
Please forgive me for the crappy code I posted last week. I didn't have time to do much testing after I typed it in. I create the files in Flash, and then copy it into this webpage and make a lot of errors in the process. So, what was working in flash, once it gets transferred into HTML (and last week I did it in a rush), includes many typos.
Please use the steps below to correct my code from last week.
Carter-
- WEEK 10
- Classwork I:
- LINK Here are some files we will be using next week in class.
- Homework:
- Exercise 1: Please bring to me the beginning of your PORTFOLIO PROJECTS done in FLASH.
- Exercise 2: Carefully follow the steps below. The steps must be completed by the beginning of class this week.
- The first exercise has to do with drawing with code, creating vector graphics with what's known as Flash's Drawing API. We will play around with creating lines and shapes first, and then do a simple drawing. For the first exercise, we will have 3 files.
- The first file is the .as file that contains the class that has all the necessary code to create a drawing of a face; the second file is another .as file which actually creates a face object to put that drawing into, and then places that object on the stage; and the third file is the .fla file which is an instance of the class.
- So, INSIDE your project folder that you should've created last week or the week before, create a new folder named wk9. This will be sitting beside the wk8 folder that you created last week.
- Now, create a new ActionScript file named Face.as, save it into that folder, and type the following code to create our new class:
- package wk9 {
- import flash.display.Shape; /* allows access to all properties of the Shape class built-in to Flash */
- public class Face extends MovieClip { /* this tells us that the custom class that we are now creating will build upon, or extend the capabilities of the Shape class that is built-in to Flash */
- public function Face() {
- }
- }
- }
- Save the file above.
- Next, we will create the file that contains the actual document class. What that means is that it is the class that the instance in the .fla file connects to. You should open a new .as file and save it as FaceTest.as inside the same directory, wk9.
- Type the following code inside your new file and save it again once complete:
- package wk9 {
- import flash.display.Sprite; /* In case you have forgotten, the Sprite class is also built-in to Flash and it is a class of objects which has properties very similar to the MovieClip class; however, it does not have its own timeline in the way MovieClips do */
- public class FaceTest extends Sprite {
- }
- }
- As mentioned, the FaceTest class is going to actually create the face object, and it to the stage, and position it. Below, we add the constructor function for the FaceTest Class. It does the job of creating the class and its primary parts. In this case, adding an instance to the stage of the FACE CLASS and positioning it:
- package wk9 {
- import flash.display.Sprite;
- public class FaceTest extends Sprite {
- public function FaceTest () {
- // Create & center Face sprite on the stage
- var smileyFace:Face = new Face();
- addChild(smileyFace);
- // Divides stage width in half so as to center face horizontally
- smileyFace.x = (stage.stageWidth)/2
- // Divides stage height in half so as to center smileyFace vertically
- smileyFace.y = (stage.stageHeight)/2
- }
- }
- }
- Create a new .fla file and save it in the project directory with the name smileyFace.fla.
- Set the document class of this file to the path and name of the second class file, wk9.FaceTest.
- Save and test. What you'll get is an empty SWF file and, hopefully, no errors.
- Now that we've set up our class and file structure, let's begin with the actual Drawing API.
- There are two basic actions when drawing with code: one is drawing lines from point-A to point-B, and the other is moving from point-A to point-B. The first is akin to when you drag your pencil across the surface of your paper from its starting point to another point on the paper and then stop. The second is more like when you lift up your pencil and move it to a new location to begin drawing a new line that is separate from the first.
- To draw a line with the Drawing API, we use the lineTo() action, and to move to a new starting point, we use the moveTo() action.
- Go to the Face.as file and add the following lines of code to draw a straight line:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // creates a line that is 1px thick
- graphics.lineStyle(1);
- /* draws a line from the origin (right in the center at 0/0, the x/y coordinates) to the new coordinates indicated, 50px to the right, (50,0) */
- graphics.lineTo(50, 0);
- }
- }
- }
- Save and test your .fla file again. You will see a small black line that starts in the center of the stage at (0,0) and that goes to the right 50 pixels to (50,0).
- Even though we didn't indicate a starting point, the drawing starts automatically by default at the origin, at (0,0) in the x/y coordinates.
- In this case, the origin is right in the center of the stage because when we told Flash to position the face, we told it to place it right in the center of the stage, half of the height and half of the width. If we wanted to start in a new position, we'd have to state the moveTo() action first.
- Let us add a vertical line to the horizontal line we already have. We won't move the pen from where it stopped at (50,0), so it will start drawing again right from the end point of the first line.
- I will now tell it to draw a straight line upwards. Remember y is the vertical dimension, up and down, so if we draw up from here where we are at (50,0), we will be going into negative territory. We are currently at the 0 x-position and at the 50 y-position, and drawing up from the 0 point will mean, as mentioned, that we're drawing into negative territory. That means that we have to tell Flash to go in the negative direction with a negative number:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // creates a line that is 1px thick
- graphics.lineStyle(1);
- /* draws a line from the origin (right in the center at 0/0, the x/y coordinates) to the new coordinates indicated, 50px to the right, (50,0) */
- graphics.lineTo(50, 0);
- graphics.lineTo(50, -50);
- }
- }
- }
- Now, let's pick up the pen and move it to a new position and begin drawing again:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // creates a line that is 1px thick
- graphics.lineStyle(1);
- /* draws a line from the origin (right in the center at 0/0, the x/y coordinates) to the new coordinates indicated, 50px to the right, (50,0) */
- graphics.lineTo(50, 0);
- graphics.lineTo(50, -50);
- // Move pen 100px to left & 50px downward
- graphics.moveTo(-50, 0);
- // Draw a line upwards 100px
- graphics.lineTo(-50, -100);
- // Draw a line to right 100px
- graphics.lineTo(50, -100);
- }
- }
- }
- We can modify the thickness of the line if we want with a value anywhere between 0 and 255. It may also include decimals if necessary. As we have it, 1 creates a 1px line and 75 would create a 75px line.
- If you choose a thickness of 0 pixels, this will draw a hairline.
- Typically, if we later scale up a 1px line to 10 times its normal size, the line will grow to 10px in thickness; however, a hairline will NEVER get thicker, no matter how big you scale it.
- You can also change the color and alpha of a line. Below, I will increase the thickness of the line to 5px, give it a bright red color, and cut its opacity (alpha) to 50%. Remember, to set alpha, it needs to be a decimal from 0 to 1, with 0 being invisible, and 1 being completely opaque. If you want half (50%) alpha, then you need to set it at 0.5.
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // change thickness, color, and alpha
- graphics.lineStyle(5, 0xff0000, 0.5);
- graphics.lineTo(50, 0);
- graphics.lineTo(50, -50);
- graphics.moveTo(-50, 0);
- graphics.lineTo(-50, -100)
- graphics.lineTo(50, -100);
- }
- }
- }
- Curves are a bit more complicated. Instead of just 2 numbers, the x/y coordinates of the the ending point, there are 4, the x/y coordinates of the ending point, and the x/y coordinates of the control point.
- What the hell is a control point? Well, think of using the pen tool or the sub-selection tool (direct-selection in Illustrator), aka the white arrow tool. When you use this too on the anchor points of a curved line, little handles point outward. You can drag on those handles to alter the curve. Each one of those is a control point. If the control point is moved to a new spot, the height or depth of the curve will change.
- To draw a curve, we use the curveTo() action. Inside the parentheses you put 4 numbers: curveTo(control-point-X, control-point-Y, end-point-X, end-point-Y).
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- graphics.lineStyle(5, 0xff0000, 0.5);
- graphics.lineTo(50, 0);
- graphics.lineTo(50, -50);
- graphics.moveTo(-50, 0);
- graphics.lineTo(-50, -100);
- graphics.lineTo(50, -100);
- /* First two numbers are the control point x/y coordinates; and the second two numbers are the end point x/y coordinates—this is back to the starting point of the 2nd L-shape we've drawn */
- graphics.curveTo(100, 100, -50,0);
- }
- }
- }
- Okay, those are the very basics. Let's start our actual drawing by commenting out those test lines:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // graphics.lineStyle(5, 0xff0000, 0.5);
- // graphics.lineTo(50, 0);
- // graphics.lineTo(50, -50);
- // graphics.moveTo(-50, 0);
- // graphics.lineTo(-50, -100);
- // graphics.lineTo(50, -100);
- // graphics.curveTo(100, 100, -50,0);
- }
- }
- }
- We'll start the drawing with a circle. A circle takes three parameters, 3 numbers. The first two are the x/y coordinates of the center point of the circle, and the last number is the radius of the circle (remember, a radius is the distance from the center of a circle to its edge, one half of its full width, or diameter).
- We will draw one circle, a large circle for the head:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // graphics.lineStyle(5, 0xff0000, 0.5);
- // graphics.lineTo(50, 0);
- // graphics.lineTo(50, -50);
- // graphics.moveTo(-50, 0);
- // graphics.lineTo(-50, -100);
- // graphics.lineTo(50, -100);
- // graphics.curveTo(100, 100, -50,0);
- // draw head
- graphics.lineStyle(5);
- graphics.drawCircle(0, 0, 100);
- }
- }
- }
- Next, we'll draw 2 small ellipses for the eyes. They're similar to the circle, only they're oblong. Ellipses have 4 parameters, 4 numbers, instead of just 3 for circle. Like the circle, the first two numbers deal with the x/y coordinates of the center point. The 3rd number is the horizontal diameter of the ellipse (the width), and the 4th number is the vertical diameter of the ellipse (its height).
- We will draw one circle, a large circle for the head:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // graphics.lineStyle(5, 0xff0000, 0.5);
- // graphics.lineTo(50, 0);
- // graphics.lineTo(50, -50);
- // graphics.moveTo(-50, 0);
- // graphics.lineTo(-50, -100);
- // graphics.lineTo(50, -100);
- // graphics.curveTo(100, 100, -50,0);
- // draw head
- graphics.lineStyle(5);
- graphics.drawCircle(0, 0, 100);
- // draw eyes
- graphics.lineStyle(2);
- graphics.drawEllipse(-60, -15, 40, 15);
- graphics.drawEllipse(20, -15, 40, 15);
- }
- }
- }
- Now, we'll put two circles inside those ellipses:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // graphics.lineStyle(5, 0xff0000, 0.5);
- // graphics.lineTo(50, 0);
- // graphics.lineTo(50, -50);
- // graphics.moveTo(-50, 0);
- // graphics.lineTo(-50, -100);
- // graphics.lineTo(50, -100);
- // graphics.curveTo(100, 100, -50,0);
- // draw head
- graphics.lineStyle(5);
- graphics.drawCircle(0, 0, 100);
- // draw eyes
- graphics.lineStyle(2);
- graphics.drawEllipse(-60, -15, 40, 15);
- graphics.drawEllipse(20, -15, 40, 15);
- graphics.lineStyle(1);
- graphics.drawCircle(-40, -8, 7);
- graphics.drawCircle(40, -8, 7);
- }
- }
- }
- How about we add some glasses to the little face? We'll use a new shape which is basically a rectangle, but which has rounded corners. There are six parameters for this shape:
- x: the x coordinate for the top-left corner of the rectangle
- y: the y coordinate for the top-left corner of the rectangle
- width: the width of the rectangle
- height: the height of the rectangle
- ellipseWidth: the width of the ellipse to draw the rounded corners
- ellipseHeight: the height of the ellipse to draw the rounded corners
- Now, I didn't just know the right numbers to put in there, but did a little trial and error until I ended up with numbers that drew the rectangles the way they looked good to me:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // graphics.lineStyle(5, 0xff0000, 0.5);
- // graphics.lineTo(50, 0);
- // graphics.lineTo(50, -50);
- // graphics.moveTo(-50, 0);
- // graphics.lineTo(-50, -100);
- // graphics.lineTo(50, -100);
- // graphics.curveTo(100, 100, -50,0);
- // draw head
- graphics.lineStyle(5);
- graphics.drawCircle(0, 0, 100);
- // draw eyes
- graphics.lineStyle(2);
- graphics.drawEllipse(-60, -15, 40, 15);
- graphics.drawEllipse(20, -15, 40, 15);
- graphics.lineStyle(1);
- graphics.drawCircle(-40, -8, 7);
- graphics.drawCircle(40, -8, 7);
- // draw glasses
- graphics.lineStyle(3);
- graphics.drawRoundRect(-70, -25, 60, 30, 15, 15);
- graphics.drawRoundRect(10, -25, 60, 30, 15, 15);
- }
- }
- }
- For the ear-pieces and the bridge of the glasses above the nose, we'll just draw a couple of diagonal lines and a curve:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // graphics.lineStyle(5, 0xff0000, 0.5);
- // graphics.lineTo(50, 0);
- // graphics.lineTo(50, -50);
- // graphics.moveTo(-50, 0);
- // graphics.lineTo(-50, -100);
- // graphics.lineTo(50, -100);
- // graphics.curveTo(100, 100, -50,0);
- // draw head
- graphics.lineStyle(5);
- graphics.drawCircle(0, 0, 100);
- // draw eyes
- graphics.lineStyle(2);
- graphics.drawEllipse(-60, -15, 40, 15);
- graphics.drawEllipse(20, -15, 40, 15);
- graphics.lineStyle(1);
- graphics.drawCircle(-40, -8, 7);
- graphics.drawCircle(40, -8, 7);
- // draw glasses
- graphics.lineStyle(3);
- graphics.drawRoundRect(-70, -25, 60, 30, 15, 15);
- graphics.drawRoundRect(10, -25, 60, 30, 15, 15);
- graphics.moveTo(-70, -20);
- // left ear piece
- graphics.lineTo(-95, -25);
- graphics.moveTo(70, -20);
- // right ear piece
- graphics.lineTo(95, -25);
- graphics.moveTo(-10, -20);
- // bridge over nose
- graphics.curveTo(0, -25, 10, -20);
- }
- }
- }
- Drawing the mouth is simple, all we need are a couple of curves connected to each other, the bottom curve and the top curve:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // graphics.lineStyle(5, 0xff0000, 0.5);
- // graphics.lineTo(50, 0);
- // graphics.lineTo(50, -50);
- // graphics.moveTo(-50, 0);
- // graphics.lineTo(-50, -100);
- // graphics.lineTo(50, -100);
- // graphics.curveTo(100, 100, -50,0);
- // draw head
- graphics.lineStyle(5);
- graphics.drawCircle(0, 0, 100);
- // draw eyes
- graphics.lineStyle(2);
- graphics.drawEllipse(-60, -15, 40, 15);
- graphics.drawEllipse(20, -15, 40, 15);
- graphics.lineStyle(1);
- graphics.drawCircle(-40, -8, 7);
- graphics.drawCircle(40, -8, 7);
- // draw glasses
- graphics.lineStyle(3);
- graphics.drawRoundRect(-70, -25, 60, 30, 15, 15);
- graphics.drawRoundRect(10, -25, 60, 30, 15, 15);
- graphics.moveTo(-70, -20);
- // left ear piece
- graphics.lineTo(-95, -25);
- graphics.moveTo(70, -20);
- // right ear piece
- graphics.lineTo(95, -25);
- graphics.moveTo(-10, -20);
- // bridge over nose
- graphics.curveTo(0, -25, 10, -20);
- // draw mouth
- graphics.lineStyle(2);
- graphics.moveTo(-45, 30);
- graphics.curveTo(0, 50, 45, 30);
- graphics.curveTo(0, 30, -45, 30);
- }
- }
- }
- Since we've gone through line-drawing, it's now time to try working with solid color fills. It's quite simple and can commence with the beginFill() action. This action takes two arguments: the color with which to fil the shape and a number between 0 and 1 for alpha. If you leave the alpha argument off, the default will be 1, opaque. You simply start with the beginFill() either before or after you set the lineStyle(). When drawing is complete, you call the endFill() action. We'll start by giving the head a color, and then make the whites of the eyes white, and then give the irises of the eyes colors. Lastly, we'll make the glass of the glasses somewhat tinted like sunglasses, but only partly transparent at 50%, which would be 0.5:
- package wk9 {
- import flash.display.Shape;
- import flash.display.MovieClip;
- public class Face extends MovieClip
- {
- public function Face() {
- // Testing lines
- // graphics.lineStyle(5, 0xff0000, 0.5);
- // graphics.lineTo(50, 0);
- // graphics.lineTo(50, -50);
- // graphics.moveTo(-50, 0);
- // graphics.lineTo(-50, -100);
- // graphics.lineTo(50, -100);
- // graphics.curveTo(100, 100, -50,0);
- // draw head
- graphics.beginFill(0xffccaa);
- // draw head
- graphics.lineStyle(5);
- graphics.drawCircle(0, 0, 100);
- // draw eyes
- graphics.beginFill(0xffffff);
- graphics.lineStyle(2)
- graphics.drawEllipse(-60, -15, 40, 15);
- graphics.drawEllipse(20, -15, 40, 15);
- graphics.endFill();
- graphics.beginFill(0x009966);
- graphics.lineStyle(1);
- graphics.drawCircle(-40, -8, 7);
- graphics.drawCircle(40, -8, 7);
- graphics.endFill();
- // draw glasses
- graphics.beginFill(0x999966, 0.5);
- graphics.lineStyle(3);
- graphics.drawRoundRect(-70, -25, 60, 30, 15, 15);
- graphics.drawRoundRect(10, -25, 60, 30, 15, 15);
- graphics.moveTo(-70, -20);
- // left ear piece
- graphics.lineTo(-95, -25);
- graphics.moveTo(70, -20);
- // right ear piece
- graphics.lineTo(95, -25);
- graphics.moveTo(-10, -20);
- // bridge over nose
- graphics.curveTo(0, -25, 10, -20);
- graphics.endFill();
- // draw mouth
- graphics.beginFill(0xffffff);
- graphics.lineStyle(2);
- graphics.moveTo(-45, 30);
- graphics.curveTo(0, 50, 45, 30);
- graphics.curveTo(0, 30, -45, 30);
- graphics.endFill();
- }
- }
- }
- Now you try and add some features, a nose or ears perhaps, maybe a moustache, a beard or some hair. You could try adding a hat or a bow-tie. You could even change the lips to be full and luscious and add some rosey cheeks. Adding other body parts could also be nice. It's all up to you, but be creative and experiment and add several things.
- ;
No comments:
Post a Comment