Forum Topic: OOP: Worth it?

(159 views • 7 replies)

This topic is 1 page long.

<< < > >>
None

Doneveno

Reply To Post Reply & Quote

Posted at: 10/1/09 05:00 PM

Doneveno LIGHT LEVEL 09

Sign-Up: 01/08/04

Posts: 18

One thing I noticed about AS3 is how incredibly difficult they made OOP, for seemingly no reason. With non-OOP I can just do it all in one frame in CS4 in the actionscript editor. With OOP I have to make a new file for every goddamn class, organize them into folders, then make code that finds and uses these classes. Why? It's incredibly stupid. Is there any way around this, or did they just ruin OOP with unnecessary rules?

Of course, I could be wrong about it and the tutorials I've found are just trash. I've noticed AS3 doesn't have many decent tutorials. Anybody have some AS3 OOP tutorials you reccomend? Just as long as I don't have to buy a physical book I'm fine.


None

Skeik-Sprite

Reply To Post Reply & Quote

Posted at: 10/1/09 05:13 PM

Skeik-Sprite LIGHT LEVEL 15

Sign-Up: 06/19/05

Posts: 1,399

At 10/1/09 05:00 PM, Doneveno wrote: One thing I noticed about AS3 is how incredibly difficult they made OOP, for seemingly no reason. With non-OOP I can just do it all in one frame in CS4 in the actionscript editor. With OOP I have to make a new file for every goddamn class, organize them into folders, then make code that finds and uses these classes. Why? It's incredibly stupid. Is there any way around this, or did they just ruin OOP with unnecessary rules?

Of course, I could be wrong about it and the tutorials I've found are just trash. I've noticed AS3 doesn't have many decent tutorials. Anybody have some AS3 OOP tutorials you reccomend? Just as long as I don't have to buy a physical book I'm fine.

Honestly, I don't see much difficult with OOP in AS3. The idea behind using a different file for each class is that you can use classes you've created in a multitude of projects instead of having spaghetti timeline code that's unmanageable and hard to read. To access these classes all you have to do is import them. It's as easy as

Import package.class

Then you have access to everything you need.


None

Chainsawasaurus

Reply To Post Reply & Quote

Posted at: 10/1/09 06:04 PM

Chainsawasaurus NEUTRAL LEVEL 12

Sign-Up: 12/27/07

Posts: 148

You can follow it as strictly as you want. I cut corners alot and don't follow it so religiously. Its just a set of guidlines really, nobodys forcing you to use it on everything you write up.


None

pixelshaded

Reply To Post Reply & Quote

Posted at: 10/1/09 06:06 PM

pixelshaded NEUTRAL LEVEL 02

Sign-Up: 09/26/09

Posts: 17

OOP is amazing once you understand it.

Programming is not a learning curve, its learning steps. I had to read tons of content on how to use objects before I finally understood what was going on. You'll just hit your head against the wall until one day, for no reason, you get it. You can also look up classes because they are essentially the same thing.

The value of OOP really only becomes apparent when you are working on something with thousands of lines of code. If your stuff is 500 lines or less, it doesn't matter all the much.

Short Run Down on OOP:

Lets use a human character in a game for example -

Two things to understand about OOP: an object has both methods and properties. A method is basically a function, something that your object does. Lets say in the case of a human, jump might be a method of our human object which makes him animate and move down in y. A property is more along the lines of a characteristic. Costume could be a property for our human. So now, we load the first level of our game, and we just change the costume by writing human.custom = "Ninja"; Then our object would do all the stuff it has to do to change the costume to our Ninja costume. Then the player presses space, which then calls human.jump(); So essentially, our human object is just a nice enclosed block of code that we can tell to do things like jump (change weapon, run, block, punch, etc) or change the characteristics of like costume (or hair color, jump height, movement speed, etc).

Now Syntax Wise:
A package holds our class (an instance of our class would be called an object). The function within the class that has the same name as our class is called the constructor. A constructor basically sets up our object when we make a new one. So we might say var enemy1:Human = new Human(); or we might want to pass properties to the constructor, so we might do var enemy1:Human = new Human("Ninja"); so when we create the human object, it already has our Ninja costume etc.

A class has private and public functions. You dont want things outside your class being able to edit its variables all willy nilly (this security is called Encapsulation), so most of your stuff should be private. If we create function in the class and want it to be a method of the class, it should be public.

So jump might look like this:

public function jump():void {
//jump code weeeeeeee
}

so you would call it object_name.jump();

You access properties with functions as well, but these are called getters and setters.

public function set costume(name:String):void {
ChangeCostume(name);
}

public function get costume():String {
return custom_var;
}

So I might type trace(object_name.costume) and it might print "Ninja".
Or I could set it to "Superman" by object_name.costume = "Superman".
The setters and getters override the = operand, so that when you type =, you are actually calling the set function or get function.

Hope that helps a bit. Good luck!


None

dylan-double-c

Reply To Post Reply & Quote

Posted at: 10/1/09 06:10 PM

dylan-double-c FAB LEVEL 16

Sign-Up: 03/05/08

Posts: 431

At 10/1/09 06:06 PM, pixelshaded wrote: a bunch of AS smarticles stuff.

That is one hell of a first post, dude...


None

pixelshaded

Reply To Post Reply & Quote

Posted at: 10/1/09 06:32 PM

pixelshaded NEUTRAL LEVEL 02

Sign-Up: 09/26/09

Posts: 17

Check out my game Blockectomy (currently struggling with the preloader atm -_-)

Now I made the balls you shoot and the blocks that fall both objects (classes)
Here is the AS3 file for the blocks.

////////////////////////////////////////////////////////////////////////////////
//
//  Box version 1.0.
//  Copyright 2009 Alexander Fisher, United States
//  All Rights Reserved.
//
// - Description :
//	- Creates a square graphic with a random color (green, blue, or red).
//	- This is the class for all the falling boxes in the game class.
// - Function: N/A
//
// - Constructor() -> Box()
// - Methods:
//	- RandomColor()
//		- Picks a random color, Green, Blue, or Red and sets the box to that color (aka clears and creates new graphics)
// - Properties:
//	- color : Read and Write. String()
//		
//		Can be set to "Green", "Blue", "Red", or "Grey". 
//		Defaults to black if string if not one of the above.
//
//	- column : Read and Write. int()
//		
//		Intended for column tracking in game based on 
//		x property when dropped.
//
// - Required Classes: None
//
////////////////////////////////////////////////////////////////////////////////
package {
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.text.*;
	import flash.geom.*;

	public class Box extends MovieClip {

		
		private var box:Sprite = new Sprite();
		
		//Object Properties
		private var _box_color:String = new String();
		private var _column:int = new int();
		
		//Square Generation Properties
		private var _width:int = new int();
		private var _height:int = new int();
		private var _corner:int = new int();

		private var rand:int = new int(); //random number used at creation
		
		public function Box() {	
			
			//Generation Properties
			_width = 23;
			_height = 23;
			_corner = 6;
			
			addChild(box); //add the sprite to the display list
		}

		//////////////////////////////////////////////////////////
		//			MakeBox
		//////////////////////////////////////////////////////////
		//
		// Clears graphics and makes a new box graphic based on 
		// sent color
		//
		//////////////////////////////////////////////////////////
		private function MakeBox(box_color:String):void {
			
			_box_color = box_color;
			
			box.graphics.clear(); //clear if we already have craphics

			//Make linestyle and fill color based on sent color (usually from RandomColor method)
			switch (box_color) {
				case "Green":
					box.graphics.lineStyle(6, 0x00FF00, 0.2);
					box.graphics.beginFill(0x00FF00, 0.5);
					break;
				case "Blue":
					box.graphics.lineStyle(6, 0x0000FF, 0.2);
					box.graphics.beginFill(0x0000FF, 0.5);
					break;
				case "Red":
					box.graphics.lineStyle(6, 0xFF0000, 0.2);
					box.graphics.beginFill(0xFF0000, 0.5);
					break;
				case "Grey": //when the box becomes inactive
					box.graphics.lineStyle(6, 0x666666, 0.2);
					box.graphics.beginFill(0x666666, 0.5);
					break;
				case "Grey Highlighted": //when the box is at the bottom of the inactive area
					box.graphics.lineStyle(6, 0x666666, 0.2);
					box.graphics.beginFill(0xFFFF66, 0.5);
					break;
				default:
					box.graphics.lineStyle(6, 0x000000, 0.2);
					box.graphics.beginFill(0x000000, 0.5);
			}
			
			//Make our rounded rectangle
			box.graphics.drawRoundRect(0, 0, _width, _height, _corner);
			box.graphics.endFill();
		}
		
		//////////////////////////////////////////////////////////
		//			RandomColor
		//////////////////////////////////////////////////////////
		//
		// Sends a random color to MakeBox function.
		//
		//////////////////////////////////////////////////////////
		public function RandomColor():void {
			
			rand = Math.floor(Math.random() * 3); //random number between 0 and 2 (floor)
			
			//Create a square graphic based off random number
			switch (rand) {
				case 0:
					MakeBox("Green");
					break;
				case 1:
					MakeBox("Blue");
					break;
				case 2:
					MakeBox("Red");
					break;
			}
		}
		
		//GETTERS AND SETTERS
		
		public function get color():String {
			return _box_color;
		}

		public function set color(_the_color:String):void {
			MakeBox(_the_color);
		}

		public function get column():int {
			return _column;
		}

		public function set column(column:int):void {
			_column = column;
		}

	}

}

If you'll notice, its all within a package. I import any of actionscripts classes that I'll need. My class extends MovieClip which means it inherits all the methods and properties of MovieClip (things like movieclip.x, movieclip.y, movieclip.rotation, etc). So basically Im making a MovieClip object with extra properties and methods. Now as you see, my Box() function which is the constructor doesnt take any arguments because I dont want to do anything with it right away. It does however have some variables that I will use later to generate the graphics. I put these in the constructor so I could easily change box sizes later on if I decided to without having to change code all over the place. And I also make sure to add my box sprite to the movieclips display list so it will be visible when I add the object to the display list in my game movieclip.

So notice its properties and methods. I use color for a few things. The setter calls a function that changes the graphics for my sprite. The getter returns the color that my object is. The balls in my game also have a color property. So I can use the properties of my object to compare them during collision detection. If their color properties are the same, remove both. If they arent, remove the ball but turn the block grey. This is the power of OOP. I have up to 6 different box objects falling at the same time. And Im constantly calling RandomColor() method to create new ones to be dropped. Imagine doing this without OOP. You would have 6 sprites variables, all with their own graphics, all having their own color string. And then I would have to write listeners so I would know which color variable to reference based on which sprite returns true on collision detection. AND THEN! If I wanted to change anything, OMG I have to do it for each sprite instead of just once in my class. It would be a nightmare.


None

Matt-Porter

Reply To Post Reply & Quote

Posted at: 10/1/09 07:28 PM

Matt-Porter FAB LEVEL 28

Sign-Up: 08/09/04

Posts: 1,065

If you're serious about game design, definitely go OOP. It's neater, far more optimized, saves you tremendous time when doing future projects, and is the best possible way to work with other members, local or online.


None

Diki

Reply To Post Reply & Quote

Posted at: 10/1/09 08:09 PM

Diki NEUTRAL LEVEL 13

Sign-Up: 01/31/04

Posts: 927

To reiterate what has already been said: yes; OOP is worth it (very worth it).
It's confusing as all hell when you first look at it, but once you fully understand it it's a godsend.


All times are Eastern Standard Time (GMT -5) | Current Time: 06:35 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!