Sunday, July 11, 2010

notes about computer code for art majors not into computer code

Hey guys,

I'd like to take a second to mention a few things here that might make the code part of Flash a little easier to understand. I know that for a lot of folks the coding can be very intimidating, but it's really not that hard. The main thing is to learn the format. Once you have the format down, then you can look at it, and if not understand immediately what each bit does, at least understand how it fits in with the other bits.

actionscript is pretty much a necessity if you want interactivity in Flash. so here are some basics:

there are 4 basic elements that go into code in actionscript(AS);
instance names
event listeners
variables
functions

when you create a button or movieclip you can create an instance of that item by giving it a name. when you click on an object you have placed on the stage, in the properties window you will see a field that says "instance name." this allows you to create a single button or movieclip and have like 5 or 6 of it on the stage and have each one work independently. take space invaders. you can create a single space invader, then place 50 instances of it on the stage, and that way when you blow one up, only that one disappears.

AS is considered an "event driven" language. this means that it is waiting for things to happen. events. keyboard events, mouse events, timed events, etc. therefore, one of the basic elements of the code is the creation of listeners that wait for these events to occur.

variables are basically storage containers for information. they are mostly important for tracking and looping. say you have 10 buttons, and you want a different thing to happen when each one is clicked. you COULD write 10 different functions, one for each button, or you could write ONE function using a variable to store which button has actually been clicked (check the two previous posts and notice the "e.target == btn1" type statements) they are also useful as counters for creating loops (when you want something to occur a certain number of times) judicious use of variables can cut down the amount of code you have to write by a TON

finally there are functions. these are the parts of the code that actually DO something. they have a name and all the code fits within curly brackets {}

so here is an example:

btn1 --there's an instance name for a button

btn1.addEventListener(MouseEvent.CLICK, playMovie);

you take the instance name, and you add an event listener. the first thing inside the parenthesis is the type of event. in this case, for someone to click on the mouse button. the second item in the parenthesis is the function you want to run when the mouse button is clicked

function playMovie(e.MouseEvent): void
{
gotoAndPlay(2);
}

you declare that this is a function, give it a name. inside the parenthesis you show the type of event(this needs to match the event being listened for. if you put in (e.Event) it would not work because you added the MouseEvent listener to btn1 above) void is a computer code thing that means it doesn't return a value (like a bank account program returning a dollar amount) we won't go into detail here, just put it in there. inside the curly brackets is the actual work. in this case, when you click the button, the Flash document will advance to the second frame and play from there.

two additional important notes:

capitalization is VERY important. stop(); will work. Stop(); will not. if your button is named btn1, and you put in an event listener for Btn1, it won't work.

most (but not all) lines of code require semicolons at the end. that's probably the #1 thing you'll mess up when beginning, leaving off semicolons.

when you code within flash (and in most programming development programs) the program will color code the words for you. this helps greatly for a couple of reasons. first, you'll know right away if you misspell certain words because they'll be the wrong color, and second, certain words are reserved and can't be used as functions or variables. for example you cannot create a function called stop because stop is already used.

No comments:

Post a Comment