[Actionscript] English to AS2 - A guide to basic understanding of Syntax.

Discussion in 'Computers' started by Nemo, Apr 6, 2009.

  1. Nemo

    Nemo Registered Member

    The basics of Action Script 2 can be pretty daunting at first, it is like a new language after all.
    But it is pretty easy to learn, all you have to do is take the regular old english language, change the grammar a bit, and change the punctuation.
    The differences between AS and english is called Syntax.
    I'm going to show you a little sample of Syntax, to explain how it works.

    Say you have a button, it has its up state and over state, that's basic and simple and anyone can do it, but how exactly do you make the button do stuff?
    My example will be a play button, placed at the beginning of a movie.
    Assuming you already have a movie (you don't have to, it can just be a couple frames of random scribbles), select the first frame of the timeline by clicking on it.
    Then open up the actions panel, usually you can do this by pressing F9.
    You should see a big text field with library component on the left, usually the whole things at the top above the timeline and stage (it might open in a new window).
    Now, for the first bit of code.

    If you were to play your movie right now, it would immediately start playing until it reaches the end, and then it will loop (start over).
    With the first frame selected and the actions panel open, we want to place a stopping code.
    This will make it so when you preview the movie, it opens up on the first frame not playing.
    Basically, the code is:

    Code:
    stop.
    But that will not work, because it's in english.
    All we have to do to translate it is change the syntax, basically the punctuation.
    Let's convert it to this:

    Code:
    stop;
    That still won't work, but I'm explaining it step by step.
    The ; is pretty much a period, it signals the end of a line, which could be considered a sentence.
    Now we have to add one little detail to get a working code.

    Code:
    stop();
    The parentheses () have to be there so flash knows what exactly you want to stop.
    If you wanted a certain movie clip to stop, and not the frame itself, you would enter that clips name in the parentheses.
    You don't have to understand that really, and since we only want to stop the frame, we leave the contents of the parentheses blank.
    Leaving it blank mean to apply that code to whatever the code is on, in our case the first frame.

    Now test your movie, it should stay on the first frame when you watch it.
    Try right clicking and pressing play, see what happens?
    When you press play, it overrides the stopping code.
    But of course, it would be much better to have a button on your movie so that people don't have to right click and press play.
    They may not know how to do that!
    So then, let's add a button.
    First you gotta draw it out, it doesn't matter what it looks like, as long as it's easy to click.
    After you finish drawing it on the first frame, select it and convert it to a Symbol (usually you can press F8).
    A window should pop up with some options.
    You can choose between Movie Clip, Button, or Graphic.
    We want a button, so pick that and press ok (you don't need to name it).
    There we go, we have our button, now on to the last part.

    Let's add the code to the button, I'll do some step by step translations again.

    Code:
    on release, play the movie.
    That should make sense in english right?
    In case you don't know what on release means, it basically means when the user lets go of clicking, or when the mouse comes up from clicking on the button.
    So when they click, it will make the movie play.
    Now to change it a bit into AS.

    Code:
    on release, gotoAndPlay;
    "And what's up with the gotoAndPlay bit??"
    Well, it means exactly that.
    When something happens (in our case it's the on release part), it will go to something and make it play.
    "Well, what is that something?"
    We haven't told it what yet.

    Code:
    on (release){
    gotoAndPlay();
    }
    "Woah, what happened?"
    Well, on (release) is a function.
    Functions always work like this:

    Code:
     on (function){
    what happens goes here.
    }
    The on (function) says what triggers the code to go off, like a click.
    The stuff inside the {}s is what will be triggered, in our case it will trigger the playing of the movie.
    "Hey, there's the parentheses again!"
    Yup, now that we have the gotoAndPlay part, we have to tell it what exactly to play.
    Since gotoAndPlay is a timeline control, we have to specify a Frame Number.
    Since frame one won't play because of its code, let's make it skip to frame 2 and start playing.

    Code:
    on (release){
    gotoAndPlay(2);
    }
    There, I hope it makes sense, we've finished the play button.
    BUT, there's still more.
    What about the end of your movie?
    Let's try going to the last frame, select that frame and bring up the AS area.
    Put in:

    Code:
    stop();
    Now make a button on that frame, the same way as last time, and make it say "replay" or something.
    After you've converted it to a button, put the same code on it as the first button.

    Code:
    on (release){
    gotoAndPlay(2);
    }
    There you go!
    Try your movie out, it should be pretty snazzy by now, and feel free to copypasta this other places but please include my name!

    ~Chabitsku Ryoma
    [Nemo]
     

  2. Mirage

    Mirage Administrator Staff Member V.I.P.

    Isn't Action Script 2.0 pretty old by now? Is it still commonly used? I've only used AS2 myself but when making new stuff I've tried to do it using AS3. I get annoyed when it asks if I want to make it compatible with older formats though.
     
  3. Nemo

    Nemo Registered Member

    Old, but not obsolete.
    AS3 isn't better, it's entirely different.
     
  4. c0cktailFuri0s

    c0cktailFuri0s Registered Member

    I'm actually a FLEX developer and found that there is a huge difference between AS2 and AS3. First, AS2 lets you get a way with being a bit sloppy in your coding. The move to AS3 gives you a little more stability and forces tight coding practices. Second, while running profiling tests, my company found that there is actually a speed increase running code inside of AS3.

    Just my two cents worth.
     

Share This Page