00:00
00:00
Newgrounds Background Image Theme

Arnahan 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: Basic Oop

7,749 Views | 33 Replies
New Topic Respond to this Topic

As: Basic Oop 2005-06-30 08:26:36


AS: Main

Hello, today we will cover the basics of Object Oriented Programming, I'm basing this off a java lecture, but it's fully converted to flash

Here we will cover:

Objects (instances)
Classes
Object References
Methods (also covered in AS: Functions )
Method Signatures (will only be available effectivly in flash 8)
An example

inheritence, interfaces, abstracts and so on in the next tutorial

Objects are everything, we use objects and we create out own types of objects (classes)
Running the program creates objects from the classes (for example _root.a=new Sound() creates a new sound object).

Every object has properties a constructor (sometimes auto-triggered) and methods.

for example, let's look at MovieClips, MovieClip is a class eventhough we probebly never thought about it, it's an object type, it has methods like

_x
_y
_alpha
_depth //which we can't access becuase macromedia designed it that way
and so on

it has methods like
setProperty (which we don't use anymore :P)
duplicateMovieClip
startDrag
getDepth
swapDepths

and so on

and it has a constructor which we don't use alot

now another example is Arrays, they have attributes like length and their content and they have methods like push() pop() and 3 constructors Array(); Array(object1,object2...objectN) and Array(length), whenever you use 'new' you trigger the constructor.

the world consists of objects:
some are tangible – A MovieClip, a button, a component.
some are intangible – A string, an array, a sound, (and most of your own classes)

a Flash program, when running, populates the "world" with objects (like in your game player, inventory, map, and so on), and specifies how they will interact.

this (partial) correspondence between real-world objects and software objects is the first step towards reducing the conceptual gap in software development. OOP allows us to make very readable code.

EXAMPLE:

a new MyCar Object, it needs the engineSize numDoors and colour attributes, and the drive() reverse() and lock() methods, the drive method uses engineSize, the lock method uses numDoors, within the object every function has access to the object's attributes.

In the real world, an object is a thing which has attributes and which behaves in particular ways. In Flash, an object is a collection of data and methods. The object encapsulates the data and methods. In a software model, the object provides an abstraction of the real-world entity

What is a class?

class is simply a specification of an object. This allows us to create multiple objects all of the same type.

For example, we might say there is a class "Car", describing cars in general. It has attributes (engine size, number of doors, colour, etc.) and behaviours (drive, reverse, open door, etc.).

We can then talk about particular objects in this class - for example, "Ken's car", "John's car" - they all have the same attributes and behaviour, but are different objects.

for example, we can have

Kens_car=new MyCar();
Inglor_car=new Mycar();
and inglor's car and ken's car get all the objects and properties of "MyCar"

to refer to an object, we use a name or referring phrase
a reference is a phrase used to refer to an object
"CS2200"
"my car"
"Ken Brown"
"CS2200 lecturer"
most objects we create in flash will have at least one reference, and many of them will have multiple references

To create an object,
we must specify what class it will be an instance of
we should give it a reference, so that we can refer to it
Example: to create an object from the MovieClip class, type:

var mc:MovieClip = new MovieClip();

The methods of an object are the pieces of behaviour it can be asked to carry out (or decides to carry out itself):
examples:
the tasks a person will perform
-delivering a lecture, completing an assignment, pouring a pint, ...
the functions of a machine
-accelerate, wash clothes on cycle 5, play track 3, ...
the facilities of an abstract object
-display your bank balance, increase your balance when you deposit money,

Provoking behaviour

What makes an object carry out one of its methods?

requested by another object (if it has access to it)
I ask my car to start
salesman asks finance officer to do a credit check

requested by the object itself
I decide to brush my teeth in the morning
car switches on fan when overheating

triggered by an event
door slams: I flinch

by passing a message

To pass a message, or provoke behaviour, you need to know
the name or location of the object
the command or action that provokes the behaviour

You can also directly invoke a method as following:

E.g. Barry says
"Moira, please do this credit check for me"

code example:
_root.object.object2.doMethod();

we usually specify some information when calling a method, passed (ususally) in params when calling an object not in the class we call it from, for example

_root.object.object2.traceFrom("Hello World");

methods may also have return values (explained in the functions tutorial)

Method signatures

if object A is to invoke one of object B's methods, it must know the method signature:
method name
the parameters, in the correct order and with the correct data type
the data type of the result

in flash it's not as critical, but since ver 8, it's a biggie.

Finally Syntax

classes only work in external .as files, with mx 2004 pro create a new .as file, in mx 2004 create a new .as file in notepad.

declare the class

class ClassName{

to declare a class you use the saved word "class" followd by the class name, remmember the file (the .as file) must have the EXACT SAME NAME;

accesstype var varname:VarType;

the accesstype in flash for now is either private (meaning only the class can access this var, or public (meaning anyone can)

following this will be the construction

function ClassName(params){//remmember, a constructor has NO RETURN TYPE
this.varname=params;
}

following the constructor are all the methods

function methodOne():Void{
this.varname++;
}

remmember, if you want your class to be able to trace, I'll post some examples soon

Response to As: Basic Oop 2005-06-30 08:27:50


examples, class Rain

lass descrip:

Constructor:
None and none needed
Vars:
None Visible
Functions:
static function analyze(amt:Number):Void
syntax: Rain.analyze(<density of the rain>);

creates a rain effect

static function stop():Void
syntax: Rain.stop();

stops the rain

class Rain {
private static var speed:Number;
private static var amount:Number;
static function analyze(amt:Number):Void {
amount = amt;
_root.createEmptyMovieClip("rain", 50000);
_root.rain.createEmptyMovieClip("dropOrigin", 100);
_root.rain.dropOrigin.lineStyle(1, 0x1EB4C4, 100);
_root.rain.dropOrigin.lineTo(10, 40);
for (var i = 0; i<amt; i++) {
var jran = random(101)+50;
//depth game
_root.rain.dropOrigin.duplicateMovieClip("drop"+i, jran);
_root.rain["drop"+i]._xscale = jran;
_root.rain["drop"+i]._yscale = jran;
_root.rain["drop"+i]._x = random(550);
_root.rain["drop"+i].starty = _y;
_root.rain["drop"+i].onEnterFrame = function() {
this._y += this._xscale/2;
if (this._y>400) {
this._x = random(550);
this._y = this.starty;
this._xscale += random(21)-10;
_root.rain["drop"+i]._x = random(Stage.width);
}
};
}
removeMovieClip(_root.rain.dropOrigin);
}
static function stop():Void {
removeMovieClip(_root.rain);
}
}

btw, static means that the attribute is class based and not object based, more on that later

Response to As: Basic Oop 2005-06-30 08:29:12


post here your classes and questions!

Response to As: Basic Oop 2005-06-30 08:29:49


Wow!Nice post,Inglor!


BBS Signature

Response to As: Basic Oop 2005-06-30 10:20:26


At 6/30/05 08:29 AM, Dark_Toaster wrote: Wow!Nice post,Inglor!

Agreed.
I've had this in my notepad links for a while, so I'm going to dump it here.
Associated reading: Introduction to OOP


- - Flash - Music - Images - -

BBS Signature

Response to As: Basic Oop 2005-06-30 10:42:04


wow denvish, very useful link ;) thanks

Response to As: Basic Oop 2005-07-25 18:25:52


Free OOP chapter from a book, now at Macromedia's site.

VERY useful primer on OOP, great for nubs like me! :D The only downside is that it's a PDF file, so you can't copy and paste code from it. Oh well, you'll learn the proper syntax quicker from typing it in by hand! :P

Response to As: Basic Oop 2005-10-08 01:07:44


it's a PDF file, so you can't copy and paste code from it.

wtf are you talking about, just need to click the select thing, instead of drag.

Response to As: Basic Oop 2005-10-08 07:10:46


Heres something I made, I based it on startDrag (the way you can specify where it can go), so it's similar to that:

Create a new .as file and call it Box, then paste these actions:

class Box {
private var th:Number = 2;
private var rgb:Number = 0x000000;
private var al:Number = 100;
private var curD:Number = 1;
private var perD:Number = 1;
public function boxStyle(t, c, a) {
th = t;
rgb = c;
al = a;
}
public function create(l, t, r, b, d) {
curD = d;
perD = d;
_root.createEmptyMovieClip("box"+d, d);
_root["box"+d].lineStyle(th, rgb, al);
_root["box"+d].moveTo(l, t);
_root["box"+d].lineTo(r, t);
_root["box"+d].lineTo(r, b);
_root["box"+d].lineTo(l, b);
_root["box"+d].lineTo(l, t);
}
public function depth() {
trace("box depth: "+curD);
}
public function setDepth(d) {
curD = d;
_root["box"+perD].swapDepths(curD);
}
}

Usage

boxName = new Box() //declare a new box

boxName.boxStyle(thickness, colour, alpha) //just like lineStyle.

boxName.create(left, top, right, bottom, depth) //draw the box, specify the co-ords of the box boundarys and specify the depth

boxName.depth() //get the depth of the box.. I included this just for debugging purposes

boxName.setDepth(depth) //same as swapDepths

Example

import Box;
b1 = new Box();
b1.boxStyle(5, 0xFFCCFF, 100);
b1.create(10, 10, 540, 390, 2);

Notes

The default box style is 2 thickness, black and full alpha. To be able to access the class from all location, place "Box.as" in Flashs Class folder, located at:

C:\Documents and Settings\USER NAME\Local Settings\Application Data\Macromedia\Flash 8\en\Configuration\Classes\FP8


Sup, bitches :)

BBS Signature

Response to As: Basic Oop 2005-10-08 10:42:38


At 10/8/05 07:10 AM, -liam- wrote: _root.createEmptyMovieClip("box"+d, d);
_root["box"+d].lineStyle(th, rgb, al);
_root["box"+d].moveTo(l, t);
_root["box"+d].lineTo(r, t);
_root["box"+d].lineTo(r, b);
_root["box"+d].lineTo(l, b);
_root["box"+d].lineTo(l, t);

ARGH! Why the fuck do you guys still do that???
That's so horrible. Do it like this, it's simpler.

var mc:MovieClip = _root.createM.....
mc.lineStyle......


wtfbbqhax

Response to As: Basic Oop 2005-10-08 10:45:10


Everyone has a different style mate but thanks I might start doing that


- Matt, Rustyarcade.com

Response to As: Basic Oop 2005-10-08 10:47:15


yep :) but I never do that (what fwe does) I never found trouble in long names unless extremely long , it just returns a pointer anyway, you might as well do

_root.attachMovie("clip","clip+i,100);
var mc:MovieClip = _root["clip"+i];

it's a pointer anyway

Response to As: Basic Oop 2005-10-08 10:49:03


hey inglor whats 3*5?

I am going to write a little OOP tutorial in a bit for people who want to dive right in and make a class so please dont come and critise everything I write :P


- Matt, Rustyarcade.com

Response to As: Basic Oop 2005-10-08 11:14:11


At 10/8/05 10:47 AM, Inglor wrote: yep :) but I never do that (what fwe does) I never found trouble in long names unless extremely long , it just returns a pointer anyway, you might as well do

_root.attachMovie("clip","clip+i,100);
var mc:MovieClip = _root["clip"+i];

it's a pointer anyway

Isn't it one less line to just do this?

var mc:MovieClip = _root.attachMovie("clip","clip+i,100);


wtfbbqhax

Response to As: Basic Oop 2005-10-08 11:27:30


At 10/8/05 10:49 AM, Ninja-Chicken wrote: hey inglor whats 3*5?

12+3

I am going to write a little OOP tutorial in a bit for people who want to dive right in and make a class so please dont come and critise everything I write :P

yeah yeah, for dummies :P

Response to As: Basic Oop 2005-10-08 12:19:18


At 10/8/05 10:42 AM, fwe wrote: ARGH! Why the fuck do you guys still do that???
That's so horrible.

I prefer my way, I don't mind typing a lot.


Sup, bitches :)

BBS Signature

Response to As: Basic Oop 2005-10-08 14:49:14


i still dont understand why you even need to use classes O_o


snyggys

Response to As: Basic Oop 2005-10-08 14:52:42


At 10/8/05 02:49 PM, 2k_rammerizkool wrote: i still dont understand why you even need to use classes O_o

That's what I was asking, then Ninja chicken made that tutorial for me :P


BBS Signature

Response to As: Basic Oop 2005-10-08 14:56:56


At 10/8/05 02:52 PM, -Toast- wrote:
At 10/8/05 02:49 PM, 2k_rammerizkool wrote: i still dont understand why you even need to use classes O_o
That's what I was asking, then Ninja chicken made that tutorial for me :P

just finished reading it. it explains some things better than this does, but it still doesnt really tell me how this is important...even with Ninja-Chicken's little example he posted.


snyggys

Response to As: Basic Oop 2005-10-08 15:04:23


Advantages of OOP

> Faster and easier on the CPU
> Reusable between projects
> Reusable within a project (ie using the same code for 100 deifferent kinds of enemies
> You dont need to know the code to be able to use it (for example do you know how the movieClip classes method "hitTest" works? no. But it is still very valuable
> Its what big boys use ; )

There you go there is a few


- Matt, Rustyarcade.com

Response to As: Basic Oop 2005-10-08 15:07:46


At 10/8/05 03:04 PM, Ninja-Chicken wrote: Advantages of OOP

> Faster and easier on the CPU
> Reusable between projects
> Reusable within a project (ie using the same code for 100 deifferent kinds of enemies
> You dont need to know the code to be able to use it (for example do you know how the movieClip classes method "hitTest" works? no. But it is still very valuable
> Its what big boys use ; )

There you go there is a few

the first and second are quite nifty...but what i need is an example of how to use it (i know, 100 enemies, whatever)

my site is currently made with flash. is there anything useful i could do with it if i use classes? or, say, a virtual pet game. what could i do with classes there?


snyggys

Response to As: Basic Oop 2005-10-08 15:10:17


Iglor you sux0rs!!L()#U)(

Response to As: Basic Oop 2005-10-08 15:15:58


ahhh definatly the virtual pet thing

http://www.moock.org/lectures/introToOOP/

So for example with your pet you could have a pet class and when somone chooses there new pet
(say a cat) you could just do

cat.fave_food = "milk"
cat.sex = "male"

and attach a movieClip of a cat with the linkage class as pet blah blah

http://www.moock.org/lectures/introToOOP/

Thats the best place to learn and conincidentally the example is a virtual pet


- Matt, Rustyarcade.com

Response to As: Basic Oop 2005-10-08 15:24:04


At 10/8/05 03:15 PM, Ninja-Chicken wrote: stuff

actually, the virtual pet is just a blob. you cant really choose anything else :P


snyggys

Response to As: Basic Oop 2006-05-15 15:10:58


At 10/8/05 03:04 PM, Ninja-chicken wrote: Advantages of OOP
> Faster and easier on the CPU
> Reusable between projects

I usually prefer (and so do artists) to make new engines for new games, for every good game should be special and unique, new and improved.

> Reusable within a project (ie using the same code for 100 deifferent kinds of enemies

You could just have an EnemyFunction = function() and then when you make a new enemy have like _root['enemy'+i].onEnterFrame = EnemyFunction;

> You dont need to know the code to be able to use it (for example do you know how the movieClip classes method "hitTest" works? no. But it is still very valuable

It's not the same thing. Using a hitTest method is not the same as copying an entire code. I'm against that point.

> Its what big boys use ; )

Yeh, like you and inglor? :P


BBS Signature

Response to As: Basic Oop 2006-05-15 15:29:09


question

Why do we use Object Oriented Programming with actionscript, a scripting language?


- Matt, Rustyarcade.com

Response to As: Basic Oop 2006-05-15 15:34:32


At 5/15/06 03:29 PM, Ninja-chicken wrote: question

Why do we use Object Oriented Programming with actionscript, a scripting language?

Exactly. We shouldn't learn it. :P


BBS Signature

Response to As: Basic Oop 2006-05-15 15:43:12


At 5/15/06 03:34 PM, -Toast- wrote:
At 5/15/06 03:29 PM, Ninja-chicken wrote: question

Why do we use Object Oriented Programming with actionscript, a scripting language?
Exactly. We shouldn't learn it. :P

no silly we use OOP in programming too so it's still useful to learn :P


- Matt, Rustyarcade.com

Response to As: Basic Oop 2006-12-15 07:18:15


These stuff makes me wanna cry...tried to learn it but still am not sure which is what.

My main problem with this...OOP thingy is I don't know when it IS considered OOP.
It's not as if something says "YOU HAVE JUST USED OOP IN FLASH,
CONGRATULATIONS!! YOU'RE NOW ONE OF THE BIG BOYS" (score += 999999999;)

Is it the act of using dot syntax or using classes or prototypes or just something else?
What exactly is it?!?!

And when the hell should I know when to create classes for my game?
Should anything I add in my game have a class?
Why make one anyways when you can use functions in the first place?

Please clarify these...thanks ;D

Response to As: Basic Oop 2007-03-17 18:22:11


Very interesting, what about interfaces, and child classes?

I want to be a Java and ActionScript program but would like more in depth features of AS OOP.

public class Character extends MovieClip
{
private name:String;
private age:int;
private items: String[] = new String[20]; //? Have to read over AS arrays i think XD

function setName(name:String):Void{
this.name = name;
}
function getName(): String{
return name;
}
function setAge(age:int):Void{
this.age = age;
}
function getAge():int{
return age;
}
function addItem(item:String):Void{
items.push(item); //Correct code?
}
setLocation(x:int, y:int):Void{
_x = x;
_y = y;
}
}

Would this be a valid code? As i said i'm basically taking my knowledge from Java and taking it to AS but i'm not sure if my this code i shown is valid. If someone could correct it and me tips I'd really appreciate that.