Flash CS3 Tutorial – Arrays by Gordon French

November 28th, 2009

An array is basically a container for holding data, similar to the way a variable hold data. The difference is that an array can hold multiple pieces of data. The location of each piece of data is referred to as its index and will always fall in a specific sequence. These indexes are numbers sequentially and begin at zero. Since Flash numbers each piece of data within the array it is easy for the programmer to access a specific piece of data at any given time. In ActionScript you are not forced to contain one type of data in an array. This means that an array may contain a number in index 0, a name in index 1, and a movie clip in index 2. Creating an array is rather simple an only requires two steps. The first is to declare the array, second is to populate the array with data.

Flash Array

Animation with Arrays

To Create an array select the first key frame on the actions layer in the time line. Copy the sample code. This code is pretty straight forward. You are creating a variable called colorsArray, data typing it as an array and setting it equal to a new array and that is it for creating the array. To add data to the array copy and past the code from the next example. With this code your are simply calling the name of the array, then in between the braces you are telling flash what index of the array to use. Lastly you are telling flash to put something in the specified index.

var colorsArray:Array = new Array();
var colorsArray:Array = new Array();

A loop will be used to access the data contained within the array, but first create another array by coping the example code. This is the section that makes this an advanced Flash CS3 Tutorial. The first line is the Array that this Flash CS3 Tutorial is all about. Next, is the for loop that you should already understand. Inside the loop is where the fun begins. The for loop is actually creating circles with a color from the first array and adding them to the cirArray so they can be used later. First, a variable num is created and set to be a random number between 0 an 4, (see math tutorials) if you need a better understanding. Next, the code is creating a new Sprite (object) and the next three lines are using graphics properties to create a circle (see controlling graphics tutorial) inside the Sprite or cir variable.

var cirArray:Array = new Array();
for (var i:int = 0; i < 15; i++){
var num = Math.floor(Math.random() * 5)
var cir:Sprite = new Sprite();
cir.graphics.lineStyle(1);
cir.graphics.beginFill(colorsArray[num]);
cir.graphics.drawCircle(0,0,30);
cir.x = Math.random() * 500
cir.y = Math.random() * 300
cir.alpha =.5
addChildAt(cir, i)

cir.name=”cir”+i
cirArray[i] = this.getChildByName(”cir”+i)
};

If you look at the line cir.graphics.beginFill(colorsArray[num]) you will see a reference to the array you first created holding the color values. This line of ActionScript is simply using the random number (num) to decide which index of the array to pull the color information from. You should understand the two lines that are setting the cir.x and cir.y to a random location, then the cir’s alpha is set to .5 making it semi transparent, and finally the circle is added to the stage with the addChildAt. The last two lines of this code are particularly important to the array. cir.name=”cir”+i names the current instance of cir as cir + i and the last line is a method of the DisplayObjectContiner class that returns the object with the specified name, thus placing the newly created and named instance of the cir inside a separate index of the cirArray. Sounds a little complicated but basically it is the same as manually adding an object to each index of the array as you did in the first array.

start_btn.addEventListener(MouseEvent.CLICK, startMotion);
function startMotion(event:MouseEvent):void{
stage.addEventListener(Event.ENTER_FRAME, motion);
stop_btn.visible = true
start_btn.visible = false
};

stop_btn.addEventListener(MouseEvent.CLICK, stopMotion);
function stopMotion(event:MouseEvent):void{
stage.removeEventListener(Event.ENTER_FRAME, motion);
stop_btn.visible = false

start_btn.visible = true
};

At this point in the Flash CS3 Tutorial ActionScript has created two arrays and added data to each of them, now to interact with the data. Copy the example code to the actions panel below the arrays. In the source file you should have noticed two buttons, stop and start. This section of code is simple the event listeners to make the buttons work.

function motion(event:Event):void{
for (var i:int = 0; i < 15; i++){
if (cirArray[i].x > 520){
cirArray[i].x = -20
cirArray[i].y = Math.random()* 300
} else
cirArray[i].x +=15
}
};

Once again add the example code to the actions panel. The first line of code is a simple function that is being called by the start button. Next, is the for loop that moves through the array. You could change the number 15 to be the length of the array by replacing it with cirArray.lenght and it would automatically detect the length of the array. Next, is an if statement checks to see if the object contained with the specific index of the array is on the stage. If the object is on the stage it moves the object 15 pixels and if it is not on the stage is moves it to the other side of the stage and randomly changes the y position. This simple if statement is what makes the 15 simple circles seem to be an endless amount.

Press Control-Enter and test the movie, if all goes well you have a Flash CS3 Animation created with ActionScript Arrays

Download Flash CS3 Tutorial, Source Files

Flash CS3 Tutorials, Copyright FrenchSquared 2008

Adobe Flash CS4 Tutorials, The Information You Need is designed around the idea that knowledge helps everyone. If an individual so desires he/she should not need to spend $100k on a college education. The knowledge needs to be available to that said individual. Adobe Flash CS4 Tutorials, The Information You Need is an attempt to bring some knowledge to the masses.

Article Source: http://EzineArticles.com/?expert=Gordon_French

  • Share/Bookmark

Web Design, Social Networking, Blogging and Beyond!

November 27th, 2009

World Wide WebThe internet is constantly evolving. You must learn to evolve with it. If you want to make money online, it doesn’t always come easy. You have to earn it. There are many ways to promote your business using good web design, social networking, blogging and strong marketing principles. I recommend building your web presence slowly and steadily. Sometimes this can be boring. Especially at first when you’re looking at all the other sites and videos with hundreds of thousands of views and wondering why can’t that be me? Don’t be discouraged though. Everyone has to start somewhere. You may want to hire someone to design and market your products or services for you.

I can recommend many social networking sites to put profiles on such as twitter, MySpace, Facebook, friendster, tribe, bebo, and many others. You don’t want to appear as though you are just trying to sell something though. Usually, it’s a good idea to make it really personal and fun for people. Use a lot of pictures and tell an interesting story. Then you have links to your website and share your business proposition from there. It’s a good idea not to just come right out with a sales pitch. That can really turn people off these days, although you do want to lead people down a path to finding out more about you.

You are the best selling point for your business. You need to embrace who you are and what you are selling if you want your business to be successful. Websites share an experience with people that will either inspire them or not. I like to focus on conscious arts for example. People who are interested in conscious arts are interested in working with me therefore.

995748___network__I recommend making all your media and marketing geared towards attracting like minded people. If people like you then they will naturally want to help you out whenever possible with recommendations to their friends. Social networking is like going to a party. You don’t go in there and just start blabbing about what you have for sale. That is not very attractive to anyone. The best thing to do is go make some friends, and they will learn what you do after they get to know you. Inspire people by not trying to sell right off the bat. Wait until people show interest in what you do before you invite them back to your website. That is where you keep most the detailed information about what you’re offering.

This article was written by Jacob Louis.

For more information visit my Asheville Web Design portfolio. I also do work long distance if interested.

  • Share/Bookmark

Professional Web Design

November 27th, 2009

Asheville Web DesignDo you want to share your music, or perhaps create a professional art gallery. Maybe you want a personal photo gallery, or a website for your business. Maybe you already have a website that needs some updating or maintenance. What about getting some search engine optimization, so you can get to number 1 on google and get more traffic to your website. I also do graphic design, video editing, and music production. If you need a logo, youtube video, license musical instrumentals, or any other multimedia production work then check out my Asheville Web Design portfolio.

  • Share/Bookmark

Heart Based Web Design

November 25th, 2009

I would love to offer a more holistic approach to web design.  I have been building websites for years now, and find that people are naturally more attracted to creative designs than “professional” websites.  Just take a look through my Web Design portfolio, and you will see that most of the sites I’ve designed are for more holistic practices like yoga, art, raw food, and health.  I find that web design is such a powerful tool that I like to be careful what I promote with my skills.  I like to healing arts and conscious expansion at the core of my web design ideals, although I have done some flash design work for HP, and other big name companies.  I was subcontracted, and wasn’t being paid fairly for my services.  It didn’t take to long to lose my passion for that particular job.  If you want me to feel free to go on a creative rampage, and really go all out with your website then I encourage you to make that possible by offering adequate funding for my service.  I really have everyone’s success as my top priority.  Let me help you, and you will be happy with the results.

  • Share/Bookmark

Webhead Service on Facebook

  • Shopping Cart


  • Latest Products


  • Newsletter

    Subscribe now, and you will receive an instant download of the ebook... "63 Killer Marketing Strategies" by Dan S. Kennedy:
    name:
    email:
  • Pages

  • Recent Posts

  • Archives

  • Meta

  • Tags