Variables, Functions and a lot more

Jul 13, 2009 2 Comments by

Hi everyone, I know it has been a while since i added anything here. In this post I will talk about the things that comprise the bulk of any code. I won’t delve too deep into this topic, you can find all about this from books and online tutorials. But before I can start creating functional and interesting Flash files it is a good idea to familiarize ourselves with building blocks of actionscript.

I really hate it when books and some video tutorials tell us that they will teach us action scripting by using the phrase “knowing the nuts and bolts of code”. A person thinks that he will actually learn how to code at the end of the book or tutorial. On the contrary, we end up with plain basic nuts and screwed bolts that adds nothing to our knowledge. It’s all the same information everywhere. What we really want from tutorials and books is very simple. We want to learn how some of the things we see online were created. We want to be able to create an art gallery, a preloader (not the boring ones), a news ticker, a cool site (no one has covered this topic on it’s entirty without compromization), a cool mp3 player…etc. you get the point.

That’s what I want to learn, just variables, functions and simple information that can be found in Help file that comes with Flash. That’s why I won’t spend too much time on boring stuff, instead I want to jump to project that are useful and yet look cool if I can. It is also why my learning won’t be linear, I will be putting things here that I want to learn based on need and the question “how did they do that” and hoping to get a “wow” at the end.

Variables:

Why use variables? Simple, to remember what we are working and and to simplify our code. For example, lets say that we have this code of a movie clip that we want to use: _root.automobile.car.carBody.carTire it would be easier if we refer it to a variable like this:

1
var tire:MovieClip = _root.automobile.car.carBody.carTire

Once the tire is declared and given a value, we can just use it as follows

1
tire.gotoAndPlay(2);

Important thing to remember is that variables can have any names. Some examples of variables are shown below:

1
2
3
var mcAlpha:Number = .5;
var myName:String = "j000";
var myURL:String = "<a href="http://www.flashjourney.com/">http://www.flashjourney.com/";</a>

Functions:

A block of code performing an action that can be called as many times as needed. Built in functions examples are gotoAndPlay(), play(); (my favorite) trace(), getURL() …etc.

The most interesting functions are the user created ones. For example, using a built in function trace(), we can instruct and button to output something whenever pressed.

In Flash authoring environment, create a rectangle, then select it and convert it to a symbol by clicking F8. Give it a name and follow the image below for options.

convert-to-symbol

once done delete the shape or symbol from the stage and click F9 to write some action script

1
2
3
4
5
6
7
var button:Button = new Button();
addChild(button);
button.addEventListener(MouseEvent.CLICK, output);

function output(e:MouseEvent):void{
trace("Button clicked");
}

Test the movie, by clicking (Ctrl + Enter) and watch what happens when you click the rectangle, an outcome like this appears

testing1Next we cover Events and Event Listeners

I hope this was good for you as it was good for me, I can’t wait till I show you how to create a full functional mp3 player

ActionScript-tuts, Tutorials

About the author

A designer wanna be who wants to quit day job to become a full time blogger and web designer. I also like to watch TV and movies a lot.

2 Responses to “Variables, Functions and a lot more”

  1. Mahfoodh says:

    Are you using ActionScript 3 or ActionScript 2. Just curious.

Leave a Reply