create your own

Flash CS3 Tutorial: Loops, for if and while

68
rate or flag this page

By frenchsquared

For Loops in Flash CS3
For Loops in Flash CS3


Flash Cs3 Tutorial on Loops

The for loop is made up of three elements that have the capability to provide for a counter variable, a condition and an operation all in one line. The three elements are separated by a semicolon and contained within a set of parentheses, see example. The first element is the variable in the example the variable is i. The second element is the condition, in this example the for loop is checking to see if i is less then 10. The third and final element is the operation or counter. In the example i is increased by one every time the loop is started.

for (var i:int = 0; i< 10; i ++){

//do this

}

Flash CS3 Tutorial, In this Flash CS3 Tutorial you will find that a few simple objects have been created for you. In the Library you will see three items: container, faster_btn and slower_btn. The faster_btn and slower_btn are simply buttons that you are going to add event listeners to. The object that is slightly different is the container movie clip. This movie clip is empty at the moment but you are going to use ActionScript to put something inside of it. In this Flash CS3 Tutorial the container has already been named container1 in the linkage properties dialog box, but make sure that you remember this step.

To link an object right click on the objects name inside the library select linkage, then select export for ActionScript.

You should be familiar with the naming conventions so, open the actions panel by pressing F9, select the first key frame on the actions layer and look at the code already in the layers panel. The first line of code is creating a variable called speed and setting it equal to 6. You will use this variable later in this Flash CS3 Tutorial. The next line creates the variable container, data types it as a movie clip and sets it equal to a new instance of container1. Remember the linkage, container1 is the name given to the empty movie clip in the library. The third line simple adds an instance of the container to the stage. In more advanced Flash CS3 Tutorials you will learn more about the rest of the code already in the actions panel. This Flash Tutorial is really meant to focus on the for loop.

for (var i:int = 0; i< 361; i +=12){

var shape:Shape = new Shape();

shape.graphics.lineStyle(1.5, 0x45DC91); shape.graphics.drawEllipse(0,0,110,40);

container.addChild(shape); shape.rotation = i;

};

Copy and past the next block of example code to the actions panel, below the other code. Here is the actual for loop, this loop is defining i setting it equal to zero, it then check to see if i is less then 361, if i is less then 361 the loop will do whatever is inside the {}. Next, every time the loopis found to be true the loop adds 12 to the value of i. You can put any information inside the brackets{}.In this Flash CS3 Tutorial the for loop is being used to draw a design. Feel free to explore the shape properties if you want but, this Flash Tutorial isn't going to cover them. However, this Flash Continue SmileyCS3 Tutorial is going to make something interesting so Press control enter to see your for loop in action.

While Loop The while loop is very similar to a for loop in that both will accomplish the same thing. The difference is that you must define each of the elements within a while loop yourself.

Warning - you need to be careful to safe your file before testing a while loop, if you forget part of the code you can and will create and endless loop.

var j:int = 0

while (j<361){

var shape2:Shape = new Shape();

shape2.graphics.lineStyle(1, 0x2C17E8 ); shape2.graphics.drawEllipse(0,0,155,80);

container.addChild(shape2);

shape2.rotation = j;

j+=10

};

Copy the example code into the actions panel below the for loop. The first line of the example code creates and defines the variable j. next is the while statement that checks to see if j is lessthen 361. The new and important line is the last one j+=10 this is basically the counter that increases j. If you forget this line j will never, not be less the n361. Therefor causing the code to run forever. Do, While Loop So, you now have a bar that grows with the amount of data loaded in the main time line, but nothing else happens. You need to make the movie play when the function progressAction is finished. There are several ways to do make Flash CS3 do something when it has finished loading. First, I'm going to show you the simplest way. You can simply add an if statement that checks to see when yourProgress is equal to or greater then 1. If yourProgress is greater or equal to one then play.

var m:int = 0

do {

var shape1:Shape = new Shape();

shape1.graphics.lineStyle(1, 0xFF9900 ); shape1.graphics.drawEllipse(0,0,125,80);

container.addChild(shape1);

shape1.rotation = m; m+=10

} while (m<361);

If you Press Control-Enter you should see the spirals spinning. Each spiral is created with a different kind of loop. Continue Smiley

If you want to understand how the circles were drawn play with the code inside the loops, then checkout the tutorial under controlling graphics.

Download Flash CS3 Tutorial, Source Files


IT Admin, Everything Computer Related

  • CSS, How to understand inline and block level elements.

    Block level  elements are normally displayed as blocks with line breaks before and after. Examples of block level elements include paragraphs, headings, divs and block quotes. Inline elements are... - 3 months ago

  • CSS, How To tweak css for only Safari?

    If you have spent anytime dealing with CSS you have run into issues with a browser rendering code incorrectly. You may even have gone as far as to make several style sheets and use a browser check... - 3 months ago

  • CSS, How to Hide Styles from Older Browsers?

    Some older browsers, such as Netscape Navigator 4 and IE 4, have poor support for CSS. It is possible to hide styles from these browsers using specific media types and @import rules. All styles will... - 3 months ago

  • CSS, How to use @import styles

    @import Styles? Header and external style sheets also can import other style sheets using the @import rule. The @import rule must be placed before all other rules in the header or external style... - 3 months ago

  • CSS, How to use External Style Sheets!

    The third method of applying styles to  document involves linking to external style sheets. External style sheets are the most appropriate method for styling documents. If styles need to be... - 3 months ago

  • CSS, How to Use Header Styles

    Header styles also can be used to style the <p> element. The CSS rules can be placed in the head of the document using the style element. Like inline styles, header styles, header styles... - 3 months ago

  • CSS, How to Apply Inline Styles.

    CSS, Inline styles can be applied directly to elements in the HTML code using the style attribute. However, inline styles should be avoided wherever possible because the styles are added to the HTML... - 3 months ago

  • Twitter Weekly Updates for 2009-08-09

    New blog post: CSS, What is a <div> http://bit.ly/Sbfu # Flash, ActionScript tutorials on loops http://bit.ly/QMq8t # Great PHP Tutorial on using include and require. Include is a great way... - 3 months ago

CSS, How To

  • CSS, How to work with content areas.

    The content are of a box can be given width, height, and color. Width and height can be specified in points (equal to 1/72 of an inch), picas (equal to 12 points), pixels, ems, exes, millimeters, centimeters, inches, or percents. - 6 weeks ago

  • Twitter Weekly Updates for 2009-09-20

    My Farmville is broken, cant access, gifts, market, or ribbons # Farmville and zynga are a rip off. Watch out. There are still some glitches in the game and zynga told me to bad Im unimportant. # wouldnt it make more since for a developer to fix broken issues rather then add new features. Well not farmville, [...] - 2 months ago

  • Twitter Weekly Updates for 2009-09-06

    I need 5 more neighbors on FarmVille help me out. http://bit.ly/tLsni # looking for flash tutorials and how to guides check out – http://frenchsquared.com/ # Powered by Twitter Tools - 2 months ago

  • Twitter Weekly Updates for 2009-08-30

    New version of Wordpress theme adStyle is live at: http://wordpress.org/extend/themes/adstyle # Just finished installing OS X 10.6 on my new Solid State drive. Very impressed, seems noticeably faster. # I need more neighbors on Farmville for Facebook. Hit me up as a friend. http://bit.ly/tLsni # playin farmville on my macbook 10.6 and new solid sate sata II [...] - 2 months ago

  • CSS, How to properly use Border Styles.

    The border properties specify the width, color and style of the border of an element. Shorthand border properties include border-top, border-bottom, border-right, border-left and border. - 3 months ago

  • CSS, How to Padding Padding and more Padding

    Padding can be applied to the outside edges of the content area of any block level or inline element. Padding creates the space between the edge of the element and its content. - 3 months ago

  • Twitter Weekly Updates for 2009-08-23

    Wordpress liked my theme. They have asked that a make a few revisions and resubmit. Sounds like my theme may get accepted! # how long does it normally take for wordpress to get back to you after submitting a theme? # Just finished update my first wordpress theme http://wordpress.org/extend/themes/adstyle # New wordpress theme made for adsense check it out [...] - 3 months ago

  • CSS, How to deal with background color and images.

    The background-color property sets the background color of an element. The backgound-image property applies a background image to an element, which will appear on top of any background-color. - 3 months ago

Comments

RSS for comments on this Hub

frenchsquared profile image

frenchsquared  says:
18 months ago

Great Flash Tutorial on loops

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working