+ Reply to Thread
Results 1 to 4 of 4

Thread: Graphic Primitives with Flash

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,042

    Graphic Primitives with Flash

    Drawing Graphic Primitives with Flash

    ActionScript contains a lot of methods for drawing all sorts of graphic primitives. With these graphic primitives, we can draw anything complicated that we want. We could make a drawing application very similar to MS paint. It wouldn't be hard to make something even better than paint.

    I'm just going to use the basic drawing commands in this tutorial. There are some graphic commands that are more advanced and more flexible.

    We are going to focus on drawing lines, drawing circles, and drawing rectangles.

    You can use code on different layers and frames but I am going to keep all of my code on layer1 and in frame1.


    Getting Started

    In Flash, you want to create a new Flash File using Action Script 3.0. It doesn't matter what you call it. Call it whatever you want.


    On the time line, open up the first frame and open the actions window (F9). All the drawing methods are located in this.graphics. Notice, that I include this. This is optional, you do not need to. You can draw to other sprites (more on that later). I always indicate what I am drawing to, and what I am adding stuff to.

    Drawing Lines

    The two methods we need are moveTo and lineTo. The moveTo method moves a cursor to the start point of the line and then we use lineTo to draw a line to the end point.

    Before doing any drawing, we must specify a line color or we will not see anything. Colors in Flash are specified in hexadecimal much like HTML. This makes sense because Flash is often used in an HTML environment. In fact, you can use CSS in ActionScript and HTML in ActionScript.

    Now let us set a lineColor. I am going to color the line a bright green color. The line will also have a thickness of 2. We want to draw a line from (50,50) to (300,200). So we use the moveTo method to move the cursor or pencil to (50,50);

    Code:
    this.graphics.lineStyle(2,0x33FF00); 
    this.graphics.moveTo(50,50);
    this.graphics.lineTo(300,200);
    The result will be a line like this:



    Drawing Circles

    Drawing a circle is as simple as providing the points of the center point and the radius of the circle.

    Example:

    Code:
    this.graphics.drawCircle(50,50,40);
    This circle has a center at (50,50) and a radius of 40.

    This will look like:



    A little bit of math allows us to understand why this even works.

    The distance from the center to any point on the outside of the circle is the radius. Thus this all we really need to draw a circle.

    The reality is drawing graphics has a lot to do with geometry.

    Rectangles

    The drawRect method takes 4 parameters. The x and y coordinates of the top left and the width and the height. The width and the height give us enough information to stretch the rectangle to give us the rectangle. Really though the width and height allow us to have four corners and we really are just drawing points between these two lines.

    The method prototype is:

    Code:
    this.graphics.drawRect(x,y,width,height);
    This gives us four major coordinates

    (x,y)
    (x,width)
    (height,y)
    (width,height)

    Then we just draw a line between the four points.

    Example:
    Code:
    this.graphics.drawRect(5,5,50,30);
    This draws a rectangle with a width of 50 and a height of 30. The upper left is (5,5). This looks like:



    Fills

    What if you want to fill the inside of the shape? This is simple before you use the draw commands issue a beginFill and pass it a color in hexadecimal notation.

    Example:

    Code:
    this.graphics.beginFill(0x336666);
    this.graphics.drawCircle(5,5,300);
    This will look like:




    These are the basic primitives that you can use to do a lot of really cool things. Try this: Moving animation. Use the keyboard to move the circle around and it will change colors.

    Challenges

    1. Using 4 text fields and a button, have the user input the start coordinates and the end coordinates of the line. If the line is negative, you will color the line blue. Otherwise, you will color it red.
    Display the slope in the output window.

    2. Draw a smiley similar to my avatar.

    3. Have the user input the coordinates of the center point and the coordinates of a point on the outside of the circle. Then you are to draw that circle. You will have to calculate the radius.

    4. The user will input the radius and the coordinates of a point on the outside of the circle. Draw the circle.

    5. Draw the above rectangle without using drawRect.
    Bijgevoegde miniaturen Graphic Primitives with Flash-fill.jpg   Graphic Primitives with Flash-line.jpg   Graphic Primitives with Flash-rect.jpg  
    Bijgevoegde afbeelding(e)

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,750
    Blog Entries
    97

    Re: Graphic Primitives with Flash

    Wow, you are a tutorial factory. +rep

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    13,155
    Blog Entries
    59

    Re: Graphic Primitives with Flash

    So, what prompted you to learn Flash? +rep
    CodeCall Blog | CodeCall Wiki
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #4
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,042

    Re: Graphic Primitives with Flash

    I was looking for something that I could make some cool things with really easily. Plus we are starting a few courses in ActionScript in the new year, and I wanted to see what it was like.

    And your right, Sprites is next.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Hacking Flash Games (PART 1)
    By TcM in forum Security Tutorials
    Replies: 287
    Last Post: Today, 11:14 PM
  2. Hacking Flash Games (PART 2)
    By TcM in forum Security Tutorials
    Replies: 115
    Last Post: 08-27-2010, 01:27 AM
  3. Creating a Basic GUI With Adobe Flash
    By chili5 in forum Tutorials
    Replies: 6
    Last Post: 08-26-2009, 11:57 AM
  4. New Google Algorithim for Flash!
    By Brandon W in forum News
    Replies: 9
    Last Post: 02-09-2009, 11:58 AM
  5. Adobe Releases Flash Player 9 for Linux
    By Onur in forum Linux/Unix General
    Replies: 1
    Last Post: 08-28-2007, 04:03 PM