Learning how to learn Action script

this is going to be a hard to question to ask correctly since i havnt been doing AS for more than a few weeks and dont really know enough to know exactly what to ask for or how to avoid unneeded questions. me and many other have this problem when calling upon the gurus or doing a forum/google/dictionary search.

So what im i asking for? Im looking for enough info to start exploring AS on my own or at least know what to ask for here or know what to look up in the AS dictionary.

I, however, dont know how much or what info that is. So i shall have to guees. Im sure everything in AS has a category. knowing whats those are, what they do, and how they work together would be a good start. Gramatical rules would be usefull as one command? will not do much. an AS FAQ would be usefull. I guees im also asking for what i dont know i need to know.

hope this makes some form of sense. you guys rock FK heh

You know, I’d run you right over to my “tutorials for newbies” if my server wasnt’ down currently. It’s really just a beta run of the tutorials, but it serves the purpose you’re talking about. Beleive me, I know exactly how you feel. There is a steep learning curve in the beginning that makes initial emersion in the subject tough to say the leaste.

I do not have access to my tutorials, but I do have a word document that contains a lot of it here. I’ll try to make a couple of posts that contain the most vital beginner a/s information that I’ve writen so far.

ah… I’m wrong… I don’t have these documents here at work. Well bear with me and I’ll try to explain what I can. Tomarrow I should be able to post more, or perhaps even send you my docs directly.

The first thing that you should know about a/s is that it is based off of the same core language as both Javascript and php. The similarities are limited, but at least you can rest in the knowledge that as you learn a/s it is helping you to learn these other languages. In general, all programing contains similar concepts that are also learned just by doing.

Action script is an OOP. That is to say that everything that you do will either be, reading the properties of, setting the properties of, or otherwise manipulating in some way, an object. Objects are refered to by dot syntax in strings which show the path to that object starting from the “root” of the Flash movie. (I said bear with me… I’ll explain all that in a minute. :slight_smile:)
Objects can be many things, but the three main ones that you will encounter in the beginning are “Movie Clips”, “Buttons”, and “Graphics” graphics are a specific object in Flash, and are not to be confused with the normal word “graphic” which applies to many things on the web. I’m assuming at this point that you’ve create a movie clip before. You should know that it has it’s own timeline which is independent of the main or _root timeline.

The other thing you need to know right off the bat is the three locations where actionscript can be placed. You can place actionscript in a Frame, or you can attach actionscript to a button or Movieclip. When looking at the “actions panel” you will see at the top it says either “Frame actions” or “object actions”. This is a good indicator. Often we think that we have an object selected only to find that the code we just spent all that time working on is placed in a frame instead. Errors will be caused by attempting to add “frame actions” to an object, or “object actions” to a frame.

If we want to do anything with an object, we have to know where that object is located. Because you could have a movie clip that contains movie clips which in turn contain movie clips, the Flash player will not just instantly know where to look for stuff. You must address it properly in your OOP strings. The following is an example of an OOP string.

_root.david.torso.arm.hand.pinky

_root tells the player to begin the oop string at the lowest level of the player. That is, the movie timeline that contains everything else in the movie. Next there is a period. This is a separator, Flash uses periods(other programs may use other symbols as a separator, but most use the period). Next is “david”. We must assume, if this is all we saw of the code, that david is a movie clip that rests on the main timeline. Inside david, the player is directed to look for an object called torso, and in that, arm, and in that, hand, and finally the pinky object. Some may wonder why objects are within each other at all. As you progress you’ll understand more, but the basic reasoning is that any change that you effect to any object, will subsiquently be applied to every object it contains. That is to say, if we look at our example, that the “hand” object is aligned in it’s position based upon the angle of the arm itself. If we move the arm upwards, the hand follows. This adds to animation imensely as we can have any number of objects being carried around by a single object, but also moving independently of the original object.
note: when using object names in an oop, we are talking about an objects “instance” name. All the movie clips, buttons, and graphics you create will have a name in the library, but in order to manipulate an object on stage, that object must have an instance name. When an object is selected, you will see a spot open up on the properties panel, for an instance name.

Perhaps that’s an unnecessary explination, but I think it illustrates the oop string pretty well, so I’ll leave it as is for now. :slight_smile:

An object has properties. Just as my pinky is smaller than the rest of my fingers, and it is pink, rather than copper, or brown, or even tanned. pink therefore is a property of my finger. All objects in Flash have properties, though buttons and graphics have less than Movie clips, and less still that you have any power over. We can read, and in some cases change properties.

all properties in Flash are preceded by an underscore(that is not to say that some other things in a/s are not preceded by an underscore, just that all properties are). Some examples are: _alpha , _x , and _rotation.
each property relates to the Flash player in what way it should be rendered on the screen. In this case, it’s transparency, it’s location from left to right in pixels, and the radian of it’s angle, respectively.

If you haven’t looked here already, you should check out Macromedia’s actionscript dictionary
http://www.macromedia.com/support/flash/action_scripts/actionscript_dictionary/

Take a look at some of the properties listed there. There are quite a few, but they are the same for all Movie clips.

Now, taking the knowledge of my previous post in conjunction with this one, can you figure out what this line of code is saying?

_root.myMovieClip._x=100;

It says to the flash player, “look for an object called ‘myMovieClip’ on the main timeline. Change it’s location on the stage to 200 pixels from the left.”

here’s a tricky question because I haven’t told you about variables yet… but take a look at this string of code.

myVariable=_root.myMovieClip._x;

You should note that everything I had on the left hand side of the equals sign in the previous example, is now on the right hand side. This is called “reading a property”. Instead of telling the Flash player to change the current location, I’m asking the Flash player to find out what the current possition is, and feed that information into a variable (which sits on the left of the equals). There is the distinction between reading and writing a property. We say that a property equals something when we are changing it, we say that something is equal to a property when we are reading it. More about this later when we go into variables.

Properties will come with time. You will learn things about each, as you do tutorials. For now though I think that gives you a basic explination of what they look like and how they work. No single explination will reveal all that there is to know about properties.

Naming conventions

Names of objects, variables and functions are created by you. You decide what their names are, and it is up to you to use a method that works for you.

note: often in the actionscript dictionary, and other places you’ll find things named like “myMovieClip”, or “myVariable”. These are just generic filler names that macromedia is using to designate that you should put an oop in that reflects your situation.

The most often seen naming convention for objects follows the following rule. Take as few words as you can to describe what an object is, remove the spaces from inbetween each word, and capitalize each word after the first one. It’s an easy method of both reading, and remembering object names.
examples:
myMovieClip
slidingMenuHolder
oPinky

That last one oPinky is the method I use. I typicaly use a lower case letter to designate type of object, then the same convention as described above, for the rest of the name. typical lower case designation letters I use are
o ~ a movie clip, button or graphic
a ~ an array
v ~ a variable
f ~ a function

The actual convention you use is up to you. There is no right or wrong way of naming your stuff, except of course if you can’t figure out what anything is, quckly and efficiently. :slight_smile: naming your stuff something like “b354k15”, if it isn’t usless to you, is sure to be very frustrating to anyone else checking out your scripts.

Variables actionscript’s powerful holders of data

I’ve already said earlier that you could set a variable to equal a property value of an object. Variables are very useful because you can store data in them for formulas or some other later use. By placing in a string of code, anything, then an equals sign, then something else, a variable is likely being created. If that variable already had a value, it is overwritten by the new data.
Flash pretty much always knows when you want to do a string variable or a number variable. There is a difference between the two however.

A string variable being set:

myVariable=“Hello world”;

a number variable being set:

myVariable=23;

Flash knows all by itself if you are trying to create a number or a string variable and works accordingly. Same thing with reading the variables.

introduction to trace(); trace is a command that tells the player to display an output during testing of a movie. It will display anything inside the parenthesis

if we did this code

vInput1=23;
vInput2="Hello world";
trace(vInput1+vInput2);

we would get an error. I think “undefined”. Flash knows that it can’t add a number and a string together like that. If our code looked like. . .

vInput1=23;
vInput2=11;
trace(vInput1+vInput2);

The output window would show 34

more on variables later I suppose, as it comes up. Those are the basics though.

now youv teased me with your example hehe. can i use that “myvariable” on other stuff?

IE: _root.myMovieClip2._x=“MyVariable”+10;

would the path change if the code was in either a frame or object?

i know about nearly all the stuff in MX that is non-AS and i have read quite a few tutorials on stuff like paths and arrays and what not, but you just taught me quite a few very usefull things. thanks a bunch.

is there a “allways and nevers of AS” article anywhere?
IE: all properties in Flash are preceded by an underscore or always do such and such or never do such and such.

also, a tutorial refferencing guide would be sweet. Like: if your having this problem or you want to do this one thing then you probly need to learn about paths or arrays or properties or run on sentances(heh) or syntax or what ever. and then have a list of recomended tuts. suggested prerequisite tuts would be great two, AS.org does something kinda like that allready tho.

hehe we have posted at the same time

() {} ; ? !!!

I know it all looks so confusing. Why all the symbols inbetween everything? And why do the explinations for symbols in the actionscript dictionary look like greek? I’ll explain what I can.

() parenthesis are usually used to contain “arguements” to functions. I haven’t gotten into functions yet… they come next. “arguements” will make more sense once we see some of them.

{} curly brackets are used around code that is to be exicuted by a function.

; are at the end of each line of code. (not really, there are a couple exceptions to that rule, but in general, a ; at the end of every line of code wont hurt anything. When in doubt, put it in.)

Other symbols I’ll try to get into as we go along. Some are obvious, some not so obvious.

Functions ~ functions are small blocks of code which we can call by name. We create functions when we are going to do the same thing many times during a movie. You can name them just like anything else.
This is a typical way of “constructing” a function,

myFunction = function(arguement1,arguement2,. . .){
}

I’m going to write a function that adds two numbers together.

fMyFunction = function(vA,vB){
    vC=vA+vB;
    return(vC);
}

now if I were to place this in the first frame of my movie, I would have it available throughout the movie, just by referencing it’s name. Say on frame 30 I have code that says

fMyFunction(3,7);
trace(vC);

The function is called sending in 3 and 7 as the arguements. There they are added together. The return() method lets me send those variables back to the place where the function was called. trace() is then called to display that variable in the output window.

That’s a pretty usless function in the grand scheme of Flash. Functions are often complex, sometimes can call themselves from within themselves (don’t ask yet), sometimes refer to other functions, etc. The important thing is to know what they look like, and how to break down what they are doing.

gimme a sec… just saw your posts.

now youv teased me with your example hehe. can i use that “myvariable” on other stuff?

IE: _root.myMovieClip2._x=“MyVariable”+10;

not quite like that, but yes you can. As an example:
if we wrote

vWidth=_root.myMovieClip2._width;
vXLoc=_root.myMovieClip2._x;
_root.myMovieClip2._x=_root.myMovieClip2._x+vWidth+vXLoc;

Then we would be taking the movie clip, finding out it’s width and x location, then adding the values of those two variables to the x location of the clip.

There’s another shortcut I can show you. This line

_root.myMovieClip2._x=_root.myMovieClip2._x+vWidth+vXLoc;

could also be writen

_root.myMovieClip2._x+=vWidth+vXLoc;

+= means take what is on the left of this, and add what is on the right of this to it. It’s a convenient shortcut.

In the case of variable names, if you are referencing them you’ll need to reference where they are located. If you create a variable through a script that is attached to a movie clip, then that variable is located at that movie clip in the oop string.

would the path change if the code was in either a frame or object?

the path relates to the location of the object in relation to the code that you are using, so yes it would. In most cases we use “root” which solves this problem because you are starting your reference from the base of the movie. However, often we are not in a position to use _root. If we are calling for an object which exists inside the object that contains the code, then it’s easy. We just go down the line.

ie my example of “david.torso.arm.hand.pinky”
If I had code in torso which wanted to tell the hand to do something, I only need to reference it like so

arm.hand.gotoAndPlay(4);

Since I’m going down the string it’s easy to manage. Sometimes though we need to go up the string towards the beginning, but we still can’t use _root. For that Macromedia gave us “_parent”. In the above example, hand is the _parent of pinky, arm is the _parent of hand, torso is the _parent of arm, and so on. If I had code in the hand that told the torso to do something, it could be writen something like

_parent._parent.gotoAndStop(19);

yup, you’re allowed to stack _parent in your oop in order to back track up the chain by each object.

i know about nearly all the stuff in MX that is non-AS and i have read quite a few tutorials on stuff like paths and arrays and what not, but you just taught me quite a few very usefull things. thanks a bunch.
I live to serve. :slight_smile:

is there a “allways and nevers of AS” article anywhere?
IE: all properties in Flash are preceded by an underscore or always do such and such or never do such and such.
I wish… I never found one. I think it’s just the kind of thing that you have to learn as you go along. It’s really not all that complex. It’s not like english’s “i before e” rule.

also, a tutorial refferencing guide would be sweet. Like: if your having this problem or you want to do this one thing then you probly need to learn about paths or arrays or properties or run on sentances(heh) or syntax or what ever. and then have a list of recomended tuts. suggested prerequisite tuts would be great two, AS.org does something kinda like that allready tho.
That would be nice. The only problem is, tutorials are made by all sorts of people, and they are all over the place. You’d be constantly updating your list every week just to keep it up to date. I’d like one too. :slight_smile:

anything more, you’ll have to wait till tomarrow. I got work to do. :slight_smile:

That’s a great tutorial upuaut. I wish this tutorial was here when I started learning AS =) .

Just one mistake I found:

“The first thing that you should know about a/s is that it is based off of the same core language as both Javascript and php.”

That statement is true for JavaScript, but PHP is quite different. PHP uses functions for almost everything, with OOP tacked on as an extra.

That’s not too say knowing AS won’t help you learn PHP, as with AS you’ll get a firm grasp of programming basics so when you move on to PHP, you will only have to learn the names of functions and some syntax.

Thanks for the clarification njs. I think I just said that because I learned so much php through a/s. They are very similar, but I get what you’re saying, they are not based, as I said, off of the same core.

everyone should feel free to correct me if they think i’m off on something. I’m no guru, just the guy who opted to answer the question. :slight_smile:

note: I forgot to mention this. The action panel has two modes. One is the basic mode, and it does a lot of the work for you, but it is restrictive in what it allows you to enter in the screen. In the upper right corner of the action panel there is a tab which will allow you to switch between “advanced”, and “basic” modes. I suggest that you do any of the examples here in advanced mode.

Flash objects.

Flash has some cool native objects included with the program. One that you’ll quickly become aquainted with is the Math object. there are lots of properties and methods that you can use with the Math object.

Math.PI(); returns the value of PI
Math.abs(); returns the absolute value of anything inside it’s parenthesis.

there are a lot of Math object methods. Look through the dictionary and become familiar with each.

Date is another object. Using it’s methods you can return to the system various information about the clock date on the viewer’s machine.

XML is another object. This one allows flash to use and parse through xml trees. It is a very fast way of organizing data.

there are other Flash native objects as well. Some are more complex subjects than others.

David, would you like a mirror for your tutorials? I have tons of extra space on 4 different servers… Let me know :slight_smile:

Would you have some space for me too, Jubby? :beam: My free host is complete crap :frowning:

Just another thing to add… The Math object is different from the other objects you mentioned because in order to take advantage of it you don’t have to make an instance of it. You can just say Math.PI(), whereas with the other two objects the workflow would be more like this:


myMath = new Math();
pi = myMath.PI();

absolutely.

ya, upuaut i like your teaching style. lemme know if you ever get your begining tuts working. thanks again