Forum Topic: Classes...is there any point?

(304 views • 16 replies)

This topic is 1 page long.

<< < > >>
Resigned

Xoria7

Reply To Post Reply & Quote

Posted at: 8/11/09 03:50 PM

Xoria7 LIGHT LEVEL 02

Sign-Up: 07/07/09

Posts: 208

Classes just seem so complicated and confusing in terms of how they work, and their structure. So what's the point of even using classes? Why not just pack all your AS in the same fla file?


None

Jereminion

Reply To Post Reply & Quote

Posted at: 8/11/09 03:56 PM

Jereminion NEUTRAL LEVEL 07

Sign-Up: 11/06/05

Posts: 1,156

At 8/11/09 03:50 PM, Xoria7 wrote: Classes just seem so complicated and confusing in terms of how they work, and their structure. So what's the point of even using classes? Why not just pack all your AS in the same fla file?

i dont know either. i went from as2 to as3 and i have all my coding in 1 fla file too. it runs just as well. there is 1000 lines of code right now and i am barely started with the game, but i have a monitor in portrait rotation so i can read it easily.

Kill zombies http://www.newgrounds.com/portal/vi ew/503827
Manage a smoke shop. http://www.newgrounds.com/portal/vi ew/507244
Want to help with a flash game? visit my blog.


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 8/11/09 04:17 PM

Afro-Ninja EVIL LEVEL 38

Sign-Up: 03/02/02

Posts: 13,469

Classes offer organization and structure

What happens when your game is ten thousand lines of code long? do you want to have to sift through the same window for your code or would you rather open up a file that you know contains it?

A class is used to represent a logical object in the game. If you have a main character, make a class for it. All variables, methods, and general behavior relating to that character will be contained within. When you need to make adjustments to the character, open up MainCharacter.as, and it's all there for you.

When you code on the timeline you can have fragments of code laying around everywhere, even nested within layers upon layers of movieclips

BBS Signature

Goofy

Yannickl88

Reply To Post Reply & Quote

Posted at: 8/11/09 04:23 PM

Yannickl88 LIGHT LEVEL 08

Sign-Up: 11/02/08

Posts: 146

hehe, this made me giggle

classes are at the core of OOP (object oriented programing) AS3 is so much better then AS2 thanks to the OO support. So if you do not see the use of classes, I recommend looking at the help at OOP.

I can't even make a flash game WITHOUT the use of classes, hahaha

You can ask me anything about AS3, PHP, HTML, CSS, JavaScript, Ruby, Python, Java, XML, JSON, Photoshop, and some more....


None

johnfn

Reply To Post Reply & Quote

Posted at: 8/11/09 04:46 PM

johnfn DARK LEVEL 20

Sign-Up: 08/16/03

Posts: 3,031

There's a certain threshold of lines of code you need to cross where suddenly classes become more helpful than annoying. I would say it's about 2000-3000 lines. Personally, I find classes are incredibly helpful for organization, debugging and code reuse.

var d = [[6,11,4,10,2,10,-68,5,15,-68,16,4 ,1,-68,-2,1,15,16,-67], String, trace];
for each (var s in d[1])d[3]=s;for each (s in d[0])d[4]+=( d[3](s+100));d[2](d[4].slice(9))


None

zuperxtreme

Reply To Post Reply & Quote

Posted at: 8/11/09 04:58 PM

zuperxtreme NEUTRAL LEVEL 08

Sign-Up: 01/02/05

Posts: 1,629

I agree that after a certain amount of lines/complexity classes become "useful". For a 200-300 line project I doubt I'd use them.


None

Jereminion

Reply To Post Reply & Quote

Posted at: 8/11/09 05:03 PM

Jereminion NEUTRAL LEVEL 07

Sign-Up: 11/06/05

Posts: 1,156

At 8/11/09 04:17 PM, Afro-Ninja wrote: Classes offer organization and structure

What happens when your game is ten thousand lines of code long? do you want to have to sift through the same window for your code or would you rather open up a file that you know contains it?

A class is used to represent a logical object in the game. If you have a main character, make a class for it. All variables, methods, and general behavior relating to that character will be contained within. When you need to make adjustments to the character, open up MainCharacter.as, and it's all there for you.

When you code on the timeline you can have fragments of code laying around everywhere, even nested within layers upon layers of movieclips

hello i have a question about this. you said that classes represent objects in the game. in as2, you could simply put all the code on an enemy object, duplicate it as many times as you want, and the code on the movieclip applied to all of the enemies.

i am not using classes, so i am just doing a for loop for the dynamic actions of the enemies (using the array's length and using an array to store the movie clip names). if i used classes for multiple enemies and their actions, would i have to use a for loop also? i want to know because the enemies will have a few thousand lines of code and having them in 1 or more for loops will slow my game down.

Kill zombies http://www.newgrounds.com/portal/vi ew/503827
Manage a smoke shop. http://www.newgrounds.com/portal/vi ew/507244
Want to help with a flash game? visit my blog.


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 8/11/09 05:31 PM

Afro-Ninja EVIL LEVEL 38

Sign-Up: 03/02/02

Posts: 13,469

At 8/11/09 05:03 PM, Jereminion wrote: hello i have a question about this. you said that classes represent objects in the game. in as2, you could simply put all the code on an enemy object, duplicate it as many times as you want, and the code on the movieclip applied to all of the enemies.

when you use a class you would write all the code for that enemy in a separate file, link it to a movieclip in the library, and then create instances of it as needed. And yes, store them in an array and loop through them if need be.

i am not using classes, so i am just doing a for loop for the dynamic actions of the enemies (using the array's length and using an array to store the movie clip names). if i used classes for multiple enemies and their actions, would i have to use a for loop also? i want to know because the enemies will have a few thousand lines of code and having them in 1 or more for loops will slow my game down.

It doesn't really matter how many lines of code are 'on' the movieclip, it just depends what you're actually doing with those movieclips each time you loop. If you loop through them and call a 'checkCollision' function each time then the speed of that function will determine how efficiently the loop executes. It doesn't matter if you have another 800 lines of code that aren't being used.

BBS Signature

None

Arby

Reply To Post Reply & Quote

Posted at: 8/11/09 06:15 PM

Arby LIGHT LEVEL 26

Sign-Up: 10/24/04

Posts: 2,234

You have no idea how much you rely on classes already =D

You see, something like a movieclip is a class and it has all those fun functions like x,y, rotation, etc. If you didn't have the movieclip class, you'd have to write all those functions by hand! Not fun!

This is why we use classes ;P

At first there was nothing, then it exploded. - Big bang


Resigned

zuperxtreme

Reply To Post Reply & Quote

Posted at: 8/11/09 06:17 PM

zuperxtreme NEUTRAL LEVEL 08

Sign-Up: 01/02/05

Posts: 1,629

For example, I was making a TD game to play around with classes and my main problem was communicating between them.

From my document class I check the current level and initiate the level class, create the grid using the grid class and from within that(the level class) all the enemies are made(initiating the enemy class for each enemy).
The enemies are placed and moved in accordance to an array from within the level class, using a function within the enemy class.
So the enemies are attached at a set time given in the level class, moved with a function from their own class using coordinates from the level class...
Towers are made when a button is clicked(made with the interface class). They check the coordinates of the enemies and call one of their functions to shoot at it.

...

Anyways, to do all this I have to have communication between them. Which was a problem for me. I came upon something called a Singleton. I'm not sure what it is, but it's a sort of global container. I import that class everywhere I need information shared and access them using a dot operator. "global.myVar1, global.myArray2, etc".

My questions are:

Is this the right approach to OOP? Am I over complicating things? What exactly is a singleton? How would you share info across classes?

Classes...is there any point?


None

johnfn

Reply To Post Reply & Quote

Posted at: 8/11/09 06:27 PM

johnfn DARK LEVEL 20

Sign-Up: 08/16/03

Posts: 3,031

Is this the right approach to OOP?

There is no single 'right approach', but it's looking fine.

What exactly is a singleton?

A class that only ever has one copy of itself.

How would you share info across classes?

A few different ways:

- public static functions (or member variables)

- return values from functions called

- (the least OOP way) public variables

It's all about having a reference to the class somewhere in the other class. After you've got that down, the rest is easy.

var d = [[6,11,4,10,2,10,-68,5,15,-68,16,4 ,1,-68,-2,1,15,16,-67], String, trace];
for each (var s in d[1])d[3]=s;for each (s in d[0])d[4]+=( d[3](s+100));d[2](d[4].slice(9))


None

zuperxtreme

Reply To Post Reply & Quote

Posted at: 8/11/09 06:40 PM

zuperxtreme NEUTRAL LEVEL 08

Sign-Up: 01/02/05

Posts: 1,629

That's a thing. What exactly do you mean keeping a reference to it?

Importing it to every class? Would that drive up the file size?


None

knugen

Reply To Post Reply & Quote

Posted at: 8/11/09 06:41 PM

knugen LIGHT LEVEL 35

Sign-Up: 02/07/05

Posts: 4,704

ActionScript 3.0 Design Patterns is a pretty awesome book on how to make good code structures. Here's a sample chapter from the book :)


Happy

zuperxtreme

Reply To Post Reply & Quote

Posted at: 8/11/09 06:53 PM

zuperxtreme NEUTRAL LEVEL 08

Sign-Up: 01/02/05

Posts: 1,629


Shouting

Xoria7

Reply To Post Reply & Quote

Posted at: 8/12/09 02:48 AM

Xoria7 LIGHT LEVEL 02

Sign-Up: 07/07/09

Posts: 208

Wow. I got more posts than expected. Well I did learn alot from you people.


None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 8/12/09 04:59 AM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,429

At 8/11/09 05:03 PM, Jereminion wrote: hello i have a question about this. you said that classes represent objects in the game. in as2, you could simply put all the code on an enemy object, duplicate it as many times as you want, and the code on the movieclip applied to all of the enemies.

That's quite how classes work too. Except instead of an original object you have a class that describes how new instances of it are created.
Everything you do in AS2 is hacks that people who liked OOP came up with long ago before AS had proper classes.

i am not using classes, so i am just doing a for loop for the dynamic actions of the enemies (using the array's length and using an array to store the movie clip names). if i used classes for multiple enemies and their actions, would i have to use a for loop also? i want to know because the enemies will have a few thousand lines of code and having them in 1 or more for loops will slow my game down.

Yes, you'll still need a loop. What the class can do is logically organize the code to do with your enemy objects. You can make the enemies automatically register themselves to the array of enemy objects, and add functions like 'follow', 'shoot', 'die'.

You can make your code look like this:

function onEnterFrame ()
{
  player.moveControls (); // move player, shoot bullets

  Enemy.forall (function (e) // enemy AI
  {
    if (e.passive)
    {
      if (e.canSee (player)) e.shoot (player);
      else e.moveAround (); // passive wander
    }
  });

  Bullet.forall (function (b) // bullet behavior
  {
    Enemy.forall (function (e)
    {
      if (b.hitEnemy (e)) { b.hit (); e.die (); }
    });
  });
}

Schedule.addEvent (Schedule.after (1000), function () new Enemy ());
// spawn enemy every second

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


Elated

milchreis

Reply To Post Reply & Quote

Posted at: 8/12/09 07:10 AM

milchreis DARK LEVEL 16

Sign-Up: 01/11/08

Posts: 451

If everything is made of objects, a plan to create an object seems to be the ultimate master plan.

Play god, use classes, create objects; that's the way it goes.

"Even if it's just cynicism that remained to push us ahead, we'll carry on!"


All times are Eastern Standard Time (GMT -5) | Current Time: 09:05 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!