AS: Beginning AS 2
Okay welcome back to another installment of AS: Beginning. Last time we learnt how to declare variables and use arithmatic operations. We also learnt how to use the trace function. Today we are going to expand on our knowlage of the different classes and some of the things we can do with them.
Classes
A class is something which has a set of properties and methods. And object is an instance of a class. So if we drew a ball and converted it into a symbol called "ball" then we would have just created a new instance of the "MovieClip" class (a new movieClip object)
So what is a propety and method?
Properties
A property is basically a type of variable that belongs to a class. Simple as that.
So for the movieClip class some properties include _x, _y, _width, _alpha etc.
There are read-only properties and then there are properties which you can freely edit.
_currentframe for example is a read-only property belonging to the MovieClip class.
Methods
Methods is the fancy class word for function. Methods are very different to properties. A method usualy has an input a process and an output. The input and output are optional but a method (function) always has a process. So for example with the trace function. The input was whatever we wanted it to trace. It has no return type but as a process it displays a message in the output window.
so here is a method for the movieClip class you may be familiar with.
gotoAndStop (5)
This has as input (frame number) and the process is it goes to that frame. Simple.
An example of a method with an output is the movieClip method
getDepth ()
this returns the movieClips current depth level
Simple so far?
Using these methods and properties
We can combine these properties and methods to perform tasks. So for example we could do
gotoAndPlay (_currentFrame + 100)
This however is boring and pretty useless. What we really need is control the flow of our actionscript and perform methods if certain conditions are met.
Enter if
The if .. statement
We use if like this
if (condition) {
Staements
}
What this does is evaluates the statment and if its true then it performs an action. We can also add else to make it do something if the condition is false
if (condition) {
statments
} else {
statments
}
Easy peasy.
Chuck it all together
so lets make something with out new aquired knowlage. Lets make a variable with a random number and if that number = 7 then we display a win message otherwise we display a loose message.
So first of all lets make a variable containing a number. Refresh your memory on how to declare a variable...
var random_number:Number
we have just declared a new variable called random_number. Now lets assign it a value using the random funcion in the Math class. The function has an input and an out put. The input is how high the number can go-1. so for example
random (11)
will allows numbers 0,1,2,3,4,5,6,7,8,9 and 10
We dont want this. We want numbers 1-10 so lets use arithmatic like we did last time.
random (10)+1
so set the variable to this
var random_number:Number = random (10)+1
Okay now we will us an if to find out if it is equal to 7
var random_number:Number = random (10)+1
if (random_number == 7) {
}
pretty self explanitary right now
Okay so now lets wham in that else and those traces to give us a complete code.
var random_number:Number = random (10)+1
if (random_number == 7) {
trace ("You win")
} else {
trace ("You lose")
}
See easy as pie
Check it out. Play around with it. Try doing it without declaring a variable. Look up >, <, != etc. Have a mess around then look out for part 3 to learn some more. Next time it will get alot more exciting.
Also learn some methods and properties of some of the most useful classes (movieClip, String, Array etc)
Til next time goodbye : )