Wednesday, March 3, 2010

Variables in AS2

Welcome to a tutorial on variables and how they are used in flash

This tutorial requires basic knowlage of flash and as2 actionscript

A variable is a space on you computer reserved for a set of number or letters or both

Let's look at an example:

x = y + z

Now what we have to do is change it to something flash can read

_root.x = _root.y + _root.z

All I do is put _root. in front of every variable it make it easier and less confusing

This is sometimes shown in math books and stuff what it means is "The variable x will be the sum of the variable y and x"

Now all we have to do is set the amounts for y and z we do this by putting

_root.y = 5
_root.z = 18

Or any numbers you wish in an actions tab on one of the frames on your main timeline known as the root timeline

And that is all you have created simple addition now how would we find out if it worked or not by adding this code to our root timeline

trace (_root.x)

The code trace() will make a message in our output box while testing a flash (Ctrl+Enter)

You will see the output box probably on the bottom-right corner of your screen and it will say 23 (or the sum of your numbers)

That is how you use number variables you may use variables for many things like

_root.myVar = 17;
gotoAndPlay(_root.myVar);

The code above will move the timeline to frame 17 the play(); the timeline

There are also rules for variable names

1. Two variables can not exist with the same name (they will just become one variable)
2. A variable can not start with an underscore (but they may have them in the middle or end of them)
3. A variable can not start with a number (but they may have them in the middle or end of them)
4. Varables are case sensitive so MYVAR, myvar, myVar, and MyVaR are all different variables
5. A variable can not have the same name as a command like "stop" for example

Varables may also be defind I dont't really know the reson for this

var name_of_varible:Type = value

A decaration must always began with "var" followed by the name of the variable then a colon ":" then the type (case sencenitive) like Number, String etc...

We have been talking about Number variables so far so let's talk about strings now

A string is a group of letters

_root.myName = "jeru"
trace (_root.myName)

This will output jeru this will also work

_root.frameLabel = "here"
gotoAndPlay(_root.frameLabel);

That will move the timeline to a frame labeled "here"

Now we will discuss one more type of variable called a Boolean

A Boolean can only be one of to things true or false

_root.myBool = true
_root.myBool = false

That is all it can be nothing else if you try to change it then you will get an error

var myBool:Boolean = true
_root.myBool = "jeru"

THAT CODE WILL NOT WORK because we tried to make a Boolean say something that is not true or false

Now I will show you operations in variable (don't worry we're almost done)

We have already saw how to do addition in varables theres how we will do the rest of math

_root.x = _root.y + _root.z
_root.x = _root.y - _root.z
_root.x = _root.y * _root.z
_root.x = _root.y / _root.z
_root.x = sin(_root.y)
_root.x = cos(_root.y)
_root.x = tan(_root.y)

That is how you do adding, subtracting, mutipling, and division in flash (sin, con, and tan are for triginomotry equation)

There are some differences with oporations in numbers for example I want to add 1 to a varable

_root.myVar + 1

Will not work

_root.myVar = _root.myVar + 1
_root.myVar += 1
_root.myVar++

All three of those codes will work when you put two pluses after a variable for some reason it goes up by one

There are also operations for strings

_root.firstName = "jeru"
_root.lastName = "none"
_root.fullName = _root.firstName + " " + _root.lastName
trace (fullName)

What I am saying here is fullName is my first name plus a space then my last name so it will output "jeru none"

That's all for now i hope you enjoyed and understand my tutorial if you have a questions then e-mail them to me at dante22_22@msn.com

--Sasuke2910

Sound in AS2

This is a short tutorial on how to use sound with as2

First you need to download a song

Then you need to import it into flash 8

File>Import>To Library

Then select your file

On your root time line put the following code

mySound = new Sound(this);
mySound.attachSound("sound1");
mySound.start();

In your library right click on your sound then click linkage...

In the pop up menu make sure the first check box is checked that says "Export for actionscript"

If that is check the first box where you can type in will be available

Type sound1 in that box (or whatever you put in the mySound.attachSound(); function)

The mySound.start(); part of the code can be ommitted and put some where else like

on (release) {
  mySound.start();
}

you can also control the volume of the sound using the setVolume() function like this

on (release) {
  mySound.setVolume(50)
  mySound.start();
}

That will make the sound play at 50% of it's normal volume

Now i will explain panning

Panning is witch speaker the sound will come out of if the panning is -100 then the sound will come out of the left speaker if the sound is 100 it will come out of the right speaker if it is set to 0 it will come out of both speakers

on (release) {
  mySound.setVolume(50)
  mySound.setPan(50)
  mySound.start();
}

That code means that the sound will play at 50% volume and will be herd more out of the left speaker

That is my tutorial if you don't understand the PM me of e-mail me

--Sasuke2910

AS2 to AS3 conversion

Ok so your good at as2 and ya wanna go to as3 huh? Well I'll teach ya what ya need to know here.

So first thing first. stop(); and play(); still work the same as they do.


And timeline control is a also exacly the same so all that gotoAnd and stuff work, same with next/prevFrame();


All variables have to be defined.


Now for the all so difficult frame events. So first thing you need to know is that all code goes on the timeline, so no code on MCs. Also all event's have to be in functions.


Let's just make a simple enterframe event that increases a variable


//Define the variable
var myNum:Number = 0


You may or may not be familier with listeners from as2 so I'll explain. When you make a listener you create a small section of script that waits for an event to happen like clicking or a press of a key. These listeners don't have to go in an on() or onClipEvent() block.

So first you wanna make the function to run ever frame first


var myNum:Number = 0
//ignore the evt parameter for now. It will be further expained later
//Fuctions are the same as as2.
function myFunction(evt:Event):void {
//If blocks are the same as as2
if (myNum<10) {
myNum++;
}
trace(myNum);
}

So there's a simple function to increase a variable up to 1o. But it is not being ran at the momment, to run it you have to add an event listener like this:


//Event.ENTER_FRAME is a method that make the function run all the time. Type 'Event.' to bring a list of possible events. The ones in capital letters are the most commonly used.

addEventListener(Event.ENTER_FRAME,myFunction);

function myFunction(evt:Event):void {
if (myNum<10) {
myNum++;
}
trace(myNum);
}

That is a completion of the code we were attepting to make.

Now let's make a button event. First let's make an MC or button on the stage. And name it say... myButton_btn


//So first lets make a event listener on the button.
//MouseEvent.CLICK is pretty self expanety.
myButton_btn.addEventListener(MouseEvent.CLICK,buttonFunction);

//Then the function. Again ignore the parameter.
function buttonFunction (evt:MouseEvent){
trace("clicked")
}


So pretty much the same.

So I guess I'll give some tips.
1. The esc hints don't work any more. 3: So no more Esc+i+f to make your if blocks.
2. By the way the parameters will take to long to expain I'll do it later but you can have any variable there I just chose to use "evt" then the class or the event your checking.
3. If ya need any more help then PM/e-mail me or, Well post it here.

--Sasuke2910

Introduction

I do program in flash quite a lot over the past couple of years (6 to be exact) and the marketing for flash game design has sky-rocketed. It is a very large industry and is not effected by any current economic downfalls, making it very successful profession.

I've recently decided to incorporate ads in to my flashes and submit them to popular site. I have make and submitted flashes to websites in the past but only as a past time and I didn't try very hard. But I will try harder in the future. I will also be posting tutorials on this blog for general use by others.

I've never really got much into the animation business when in come to flash. I have done very much programming. So I will only be posting tutorials on what I know.

I will also make an attempt in the future to try actionscript 3.0 instead of what people are calling the slow and old actionscript 2.0

--Macha