00:00
00:00
Newgrounds Background Image Theme

domvip just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As: Beginning As 2

1,610 Views | 7 Replies
New Topic Respond to this Topic

As: Beginning As 2 2005-11-01 11:19:47


AS:Main

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 : )


- Matt, Rustyarcade.com

Response to As: Beginning As 2 2005-11-01 11:24:21


Yey! I tried to read it but it was too hard for me, *Sigh*... I guess I'll have a look at it again in three months.


BBS Signature

Response to As: Beginning As 2 2005-11-01 11:30:45


AS:Beginning AS 1 -- also by Ninja-Chicken

read this first

just for the sake of organisation :D

Response to As: Beginning As 2 2005-11-06 15:04:57


This thread has gotten very little publicity.

Response to As: Beginning As 2 2005-11-06 15:08:38


At 11/6/05 03:04 PM, kipling wrote: This thread has gotten very little publicity.

Lol, not less than Inglor's maths AS:. Before I bumped it it had 0 replies, posted one month before =)


BBS Signature

Response to As: Beginning As 2 2005-11-06 15:10:14


heck yea kipling. Anyway, good tutorial NC. Even though I already knew all that.

Response to As: Beginning As 2 2005-11-17 12:39:06


At 11/6/05 03:10 PM, Darkfire_Blaze wrote: heck yea kipling. Anyway, good tutorial NC. Even though I already knew all that.

Yeah its clearly for people who are knew hence the beginning part in the title.
I think the 2 beginning tutorials I wrote are the first things n00bs should go to because all the others give them clouded visions of copy and pasted code that doesnt explain anything


- Matt, Rustyarcade.com

Response to As: Beginning As 2 2008-05-07 19:55:49


Just read the first part of this, and then this part. It's great! I'm a complete and utter newbie to AS and I really want to get involved with it. For months I've had little or no motivation because I've never found a clearly explained intro to AS. This tutorial changed that :)

Thanks!