LOADING
Loading
Hi , welcome back.
LogoutLOGOUT
 
  Lost password?  
Hi
 




ReplyREPLY THREAD
 
Thread Tools Display Modes
  #1  
Old 06-12-2004, 08:28 AM
FrozenMedia's Avatar
FrozenMedia FrozenMedia is offline

Frozen Moderator
 
Join Date: Apr 2003
Location: United Kingdom
Posts: 2,701
Rep Power: 13
FrozenMedia is on a distinguished road
How do you eat yours?

I was staring at a lot of code last night and thinking, how is everyone dealing with AS2?

By this I mean, we are all coming from different backgrounds, personally I've taken the C route and stayed fairly procedural until the last couple of years, but others have come from C++ and Java and brought with them many OO practices.

The question is, when it comes to coding, what's your style? I hope this proves to be of interest to others like me, curious of "what is the best way to code in AS2?"....

Here's my answer to kick things off (if it's bad, just tell me I won't be mad !):

- I always no longer use "this" (as in AS2 it is implicit as in Java) to access properties in a class as I feel it is still easily readable if the classes are abstracted enough. (Not sure if there are speed issues as with _global?). Obvious exceptions to the rule are when the method parameter has the same name as a class's property, this.property = property; then works great

- I place all public and private properties at the start, before the constructor. (This seems to contradict MM's latest classes, such as Remoting AS2)

- I only use getter/setter methods when something else (other than getting/setting) is done within the method.

- I always use _array _mc etc to speed up my coding by 2x


I've noticed on recent MM code the complete reverse is for that first point, Any comments?

If this even get's 1 reply, that will be great!!
Rich

* Also with games, how do you set yours up? I like to create a Game class extending MovieClip that has an onEnterFrame loop check the "gameState" every frame with a switch statement and call the relevant methods based on the state, e.g. Logic/Move/Render...
__________________
Blog: http://www.richardleggett.co.uk | If your happy with the help, let us know about it
Reply With Quote
  #2  
Old 06-14-2004, 02:19 AM
prisma prisma is offline

Super Moderator
 
Join Date: May 2001
Location: Munich, Germany
Posts: 1,668
Rep Power: 14
prisma is on a distinguished road
I also always use this to access class properties.
I place all variables before the constructor, all functions after it.
I try to avoid using getter & setter if the code involved is more than some very simple stuff. I don't like to give the user the impression he's changing a variable, while there's actually extensive code executed. getter is, of course, very usefull to create read-only variables.

I started Flash without much programming experience except some very basic Delphi.
And I learned some OOP before AS2 from tutorials of various languages while trying to create my own socket server for Flash applications. (I ended us using Delphi, which didn't need much OOP after all, but I still got some insight on the way)
__________________
... take me to your coder.
Reply With Quote
  #3  
Old 06-15-2004, 06:22 PM
FrozenMedia's Avatar
FrozenMedia FrozenMedia is offline

Frozen Moderator
 
Join Date: Apr 2003
Location: United Kingdom
Posts: 2,701
Rep Power: 13
FrozenMedia is on a distinguished road
Thanks for the reply Prisma,

Delphi was cool (and they had that whole crazy free version called Kylix for linux), remember even Pascal had interfaces!

Rich
__________________
Blog: http://www.richardleggett.co.uk | If your happy with the help, let us know about it
Reply With Quote
  #4  
Old 07-06-2004, 02:50 PM
FlashMove's Avatar
FlashMove FlashMove is offline

FlashMove Master
 
Join Date: Jan 2000
Location: Singapore
Posts: 4,597
Rep Power: 10
FlashMove is on a distinguished road
This is a nice topic so bumped up.
I always define the datatype when declaring variables. This will help speed up processing of Actionscript and provide code hinting for arrays! It's a definate two for one deal! Plus if the variable is assigned to a wrong datatype, it would help make debugging alot easier and addresses basic type checking issue. Now how's that for a double meal deal.
E.g.
Actionscript:
var myNumber:Number = 2.00; // speeds up processing time
var myString:String = "ActionScript is cool!";
var myArray:Array = new Array(); // code hinting
Reply With Quote
  #5  
Old 08-02-2004, 09:05 AM
pompeyd pompeyd is offline
Registered User
 
Join Date: Aug 2004
Location: UK
Posts: 5
Rep Power: 0
pompeyd is on a distinguished road
Hi,

I was very procedural in 5 and MX ( The last coding I did prior to Flash was 'Basic' on a Commodore many eons ago!! ), but with AS2.0 I spent some time learning OOP - well worth it.

Now I do very little coding in the FLA, most work is in the Class files and in creating V2 Components.

I declare all properties prior to the constructor, all private functions after that, then any public custom functions, then the getters and setters.

I would declare the properties as follows
Actionscript:
private var _myString:String = null;
private var _myArray:Array = null;
private var _speed:Number = null;
// Each property is preceeded by an underscore. Declaring the variable type Number, Array etc handles the code hinting and debugging
I then use the same property names without the underscore for the getters and setters, makes it easier to follow I think. and set the initial values of the properties using Inspectable.
Actionscript:
[Inspectable(name="1. Speed" , type=Number , defaultValue=5 , enumeration="1,2,3,4,5")]
	public function get speed():Number
	{
		return _speed;
	};
	[Inspectable(name="1. Speed" , type=Number , defaultValue=5 , enumeration="1,2,3,4,5")]
	public function set speed( s:Number ):Void
	{
		_speed = s;
	};

I use getters/setters all the time if the user needs to set any properties. If changing a property results in a requirement for more action to be taken, then I place one call inside the setter to a private function to handle the changes. So the most I have inside a setter is two lines of code - only ever one inside the getter - obviously.

As far as 'this' is concerned, I always use it in the Class file,
Actionscript:
private function createButtons( Void ):Void
	{
		this.image1.onMouseDown = function( Void ):Void
		{
			var tempX = this._xmouse;
			var tempY = this._ymouse;
			var tempW = this._width;
			var tempH = this._height;
			
			switch( tempX > 0 && tempY > 0 && tempX < tempW && tempY < tempH )
			{
				case true:
					_parent.moveViewfinder(tempX , tempY);
			};
		};
	};
__________________
[ Coponents & Extensions ]
www.flashism.org

[Application Design & Development]
www.pompeyd.net
Reply With Quote
  #6  
Old 08-19-2004, 01:38 AM
clint317 clint317 is offline
Registered User
 
Join Date: Aug 2004
Location: Atlanta GA USA
Posts: 18
Rep Power: 0
clint317 is on a distinguished road
I'm no guru by far but I come from a PHP and CFML background. I did use a little OOP in PHP, but not like in Flash. Flash is THE BEST tool for learning OOP that I can possibly think of. It's fun and easy at the same time, especially compared to C++, which I did a little of also. I used to hate OOP and got confused quite often, now I love it and don't know what I'd do without it. I'm building impressive interactive games at light speed and enjoying every minute.

I have more to learn still, especially with Physics, but here's what I do so far:

Set the class, set the variables, make the constructor, create the get and set functions, then all other functions after that as I think of them. I'm also getting into OOA and OOD or UML as you may know it and it really helps with planning.

Here's my very first OOP code I created in Flash. Stuff I do now would fill up the page but I thought it would be interesting to other newbies. I even did an extending class.

Actionscript:
class Rockets extends MovieClip{
	public var vtime:Number;
	public var ymomentum:Number;
	public var xmomentum:Number;
	public var gravity:Number;
	public var basetop:Number;
	public var basebottom:Number;
	public var baseright:Number;
	public var baseleft:Number;
	private var yposition:Number;
	private var xposition:Number;
	function Rockets(){
		this.vtime = 1;
		this.ymomentum = 0;
		this.xmomentum = 0;
		this.gravity = .5;
		
		this.basetop = 0;
		this.basebottom = 305;
		this.baseright = 550;
		this.baseleft = 0;
	}
	function fly(){
		this._y = 150;
		this._x = 300;
		//trace(this.xmomentum);
		if(_root.moonfloor.hitTest(this._x,this._y,true)){
			this._alpha = 50;
		}
		if(this.yposition > this.basebottom){
			this.yposition = this.basebottom;
			this.ymomentum = 0;
			this.xmomentum = 0;
		}
		if(this.yposition < this.basetop){
			this.yposition = this.basetop;
			this.ymomentum = 0;
		}
		if(this.xposition < this.baseleft){
			this.xposition = this.baseright;
		}
		if(this.xposition > this.baseright){
			this.xposition = this.baseleft;
		}
		if(Key.isDown(Key.LEFT)){
			this._rotation = -10 * -this.xmomentum;
			xmove();
			if(this.xmomentum < 1.5){ this.xmomentum += .02; }
		}else{
			xmove();
		}
		if(Key.isDown(Key.RIGHT)){
			this._rotation = -10 * -this.xmomentum;
			xmove();
			if(this.xmomentum > -1.5){ this.xmomentum -= .02; }
		}else{
			xmove();
		}
		if(Key.isDown(Key.UP)){
			ymove();
			if(this.ymomentum < 15){ this.ymomentum += .1; }
		}else{
			ymove();
			this.ymomentum -= .05;
		}
	}
	private function ymove(){
		this.yposition -= this.ymomentum * this.gravity;
		this._y = this.yposition;
		this.yposition = this._y;
	}
	private function xmove(){
		this.xposition -= this.xmomentum;
		this._x = this.xposition;
		this.xposition = this._x;
	}
}


The extension:

Actionscript:
class RocketFire extends Rockets{
	function RocketFire(){
		this._visible = false;
	}
	function fire(){
		if(Key.isDown(Key.UP)){
			this._visible = true;
		}else{
			this._visible = false;
		}
	}
}

And another:

Actionscript:
class RocketBullets extends Rockets{
	function RocketBullets(){
		this._y = false;
	}
	function shoot(){
		if(Key.isDown(Key.SPACE)){
			this._visible = true;
			this._y -= 5;
			for(var i:Number = 1; i < 500; i++){
				if(i == 20){
					this.duplicateMovieClip("bullet" + i, 1);
				}
			}
		}else{
			this._y = this._parent._y;
			this._visible = false;
		}
	}
}

Guess what game I was trying to build. And yes I did finally finish it after starting over. But I keep all my work, fail or succeed.
__________________
Code Junkie 24/7 CFMX FLMX04 DWMX 3DSwift Maya..
Reply With Quote
  #7  
Old 08-19-2004, 01:42 AM
FrozenMedia's Avatar
FrozenMedia FrozenMedia is offline

Frozen Moderator
 
Join Date: Apr 2003
Location: United Kingdom
Posts: 2,701
Rep Power: 13
FrozenMedia is on a distinguished road
Hi clint,

Thanks for the post, I'm a little confused tho why RocketBullets extends Rockets? That would mean holding LEFT down would move the bullet's left as they move? Otherwise it would not use any of the methods of Rockets and therefore perhaps shouldn't extend them?

Maybe a Rocket Class could incorporate an instance of Rockets and any necessary RocketBullets (which now extends MovieClip) and RocketFire (extends MovieClip) when needed?

Rich
__________________
Blog: http://www.richardleggett.co.uk | If your happy with the help, let us know about it
Reply With Quote
  #8  
Old 08-19-2004, 02:52 AM
clint317 clint317 is offline
Registered User
 
Join Date: Aug 2004
Location: Atlanta GA USA
Posts: 18
Rep Power: 0
clint317 is on a distinguished road
Yes your correct. In after thought I realized that a firing mechenism is an extension of the rocket but the actual bullets are an object of their own, after they leave the ship, and so they should take on their own dynamics, effect of wind for instance. I don't want my ship to blow up when the bullet hits the enemy. Any ways, it was my very first attempt and realized that things like RocketFire was not an extension of Rockets at all. An extension of Rockets would be a Klingon class or Enterpise class. RocketFire should've been a function of all Rockets depending on if the ship had a fire_mc at all. My mistake was in planning, none. After studying UML I began to realize WHAT is an object and WHAT should be an extension.
__________________
Code Junkie 24/7 CFMX FLMX04 DWMX 3DSwift Maya..
Reply With Quote
  #9  
Old 08-19-2004, 10:29 AM
FrozenMedia's Avatar
FrozenMedia FrozenMedia is offline

Frozen Moderator
 
Join Date: Apr 2003
Location: United Kingdom
Posts: 2,701
Rep Power: 13
FrozenMedia is on a distinguished road
You got it Hope the game turns out well, maybe post it on this forum?

Rich
__________________
Blog: http://www.richardleggett.co.uk | If your happy with the help, let us know about it
Reply With Quote
  #10  
Old 09-08-2004, 08:20 PM
xxlm xxlm is offline
Registered User
 
Join Date: Aug 2002
Location: New-Caledonia
Posts: 6
Rep Power: 0
xxlm is on a distinguished road
For my case I'm doing this this way:

properties (var)
constructor
function in alphabetical order.

For the properties I'm doing this way
Actionscript:
_nTest:Number = 2;
_sMystring:String = "I eager to see f8";
_bAnswer:Boolean = true;
As you can see for each properties there is un underscore plus the first letter is the type of my property. _n is a Number _s a String, ...
Easier when re reading the code...

C ya
Reply With Quote
  #11  
Old 09-08-2004, 08:43 PM
FrozenMedia's Avatar
FrozenMedia FrozenMedia is offline

Frozen Moderator
 
Join Date: Apr 2003
Location: United Kingdom
Posts: 2,701
Rep Power: 13
FrozenMedia is on a distinguished road
I use the standard convention of using an "_" underscore before private properties and leaving it off for public ones.

Anyone coming from a VisualC++ background, would be interested to hear your comments on properties of a class, if I remember rightly the standard for VC Microsoft stylee used to be "m" in front for member variables and "g" for something else I can't remember!! and then hungarian notation on top of that e.g. "b" for bool, "i" for int, "dbl" for double etc. You ended up with something like this:

m_bIsIt = true;

Rich
__________________
Blog: http://www.richardleggett.co.uk | If your happy with the help, let us know about it
Reply With Quote
  #12  
Old 10-25-2004, 05:50 PM
scooter's Avatar
scooter scooter is offline
Registered User
 
Join Date: Sep 2004
Location: athens, OH
Posts: 116
Rep Power: 9
scooter is on a distinguished road
see i dont like using the underscore, cause it makes me think property as in _currentframe. For variables i use the letter for the data type Svariable, Nvariable, Bvariable. Where S,N, and B represent string, number, and boolean. As for symbols, I use MC, BTN, and G at the end of my symbol name. This way i can sort my library by name, then i can always choose to sort by type as well. I love that you can define variable data types, helps save debug time. I really wish there was a way to give a symbol the instance name of the symbol name. Like a one click button, I almost always name my instances the same as my symbols. What do you guys think of that idea?
-sc00ter
__________________
//setInterval(visitflashmove.com, 1000)
Reply With Quote
  #13  
Old 10-26-2004, 01:42 PM
FrozenMedia's Avatar
FrozenMedia FrozenMedia is offline

Frozen Moderator
 
Join Date: Apr 2003
Location: United Kingdom
Posts: 2,701
Rep Power: 13
FrozenMedia is on a distinguished road
Interesting stuff. Recently I've changed from prepending an _ for private toa double __ as per MM code, and _ for member properties (public), leaving off the _ all-together for variables declared within a method, e.g. with scope limited to a particular function or loop.

As for the suffix of _btn or _g, there was a list of standard types used in the Flash IDE editor, but you can add your own in by modifiying the XML files in the Flash config folder for auto-popup code-completion (I know this isn't really needed in the strict datatyping of AS2 but it still helps when skimming through code).

@scooter, nice to see your site complete, very cool intro, even though I'm not a big fan of being able to cloase windows inside a flash movie leaving you with a blank content pane Is that sound bite you or a film splice?

Rich
__________________
Blog: http://www.richardleggett.co.uk | If your happy with the help, let us know about it
Reply With Quote
  #14  
Old 10-28-2004, 02:43 PM
scooter's Avatar
scooter scooter is offline
Registered User
 
Join Date: Sep 2004
Location: athens, OH
Posts: 116
Rep Power: 9
scooter is on a distinguished road
the sound bite is me, then i went to a coffee shop and got some ambiant sound, mixed it down, and threw it on there. for the props on the site. I like you ideas about underscores to show whether the variable is open or closed. I always wanted to come up with a system for that.
-sc00ter
__________________
//setInterval(visitflashmove.com, 1000)
Reply With Quote
  #15  
Old 09-26-2005, 08:14 PM
Scottae's Avatar
Scottae Scottae is offline
God Moderator
 
Join Date: Oct 2003
Location: United States
Posts: 6,532
Rep Power: 16
Scottae is on a distinguished road
Chat My two cents

Just re-read through this thread again. I remember when it was first put up here (over a year ago) and I had no idea how to do anything with AS 2.0. My understanding of OOP was very limited. Now I almost code exclusively in class files and rarely on the fla. I make a good bit of V2 components. Speaking of which.....pompeyd, you don't need the Inspectable meta data for both the getter and setter methods. Only for one or the other. You may know already, but I figure I'd make that point for others.

I am with most of you guys:

Actionscript:
/** Imports */
import SomeClass;

class ClassName
{
	/** Class properties */
	private var mProperty1:Type;
	private var mProperty2:Type;
	private var mProperty3:Type;
	
	/** Constructor function */
	public function ClassName ()
	{
		
	}
	
	/** Class methods */
	private function method1 (pParam:Type):Void
	{
		
	}
	
	private function method2 (pParam:Type):Void
	{
		
	}
	
	private function method3 (pParam:Type):Void
	{
		
	}
}

ActionScript is my first scripting language, so I kind of go with the flow when it comes to naming conventions and coding practices. I work with some Java guys and they use the m in front of class property names (for member variable) and p in front of parameter names (obviously for parameter).

Something else these Java guys do is put a comment at the end of a function or loop to show what the closing bracket is for:

Actionscript:
private function method2 (pParam:Type):Void
{
	
	for (var i = 0; i < 100; i++)
	{
		
	}// End for loop
	
}// private function method2 (pParam:Type):Void

Now, I like the End for loop. I have found this keeps things clear when you have a bunch of nested loops and if statements and whatever. You end up with something like this:

Actionscript:
}
						
					}
					
				}
				
			}
		}
	}

Man that is hard to tell what is going on. But if you label each closing bracket like this:
Actionscript:
}// End for loop thru related blah
						
					}// End if statement for blah
					
				}// End for loop thru tblah
				
			}// End for loop thru blah
		}
	}
That is much easier to read. I don't like the ending of methods with the full method name and everything. This seems to make the code cluttered to me. On top of that, when I do a search, I see the method name twice as much which is really annoying.
__________________
Our greatest glory is not in never falling but in rising everytime we fall. - Confucius

Blog | Shared Items

Last edited by Scottae : 09-26-2005 at 08:18 PM
Reply With Quote
ReplyREPLY THREAD


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Eat 3D m. allen west 3D Programming 1 05-21-2009 11:45 AM
asp problems havey Server Side 3 01-14-2004 01:00 PM
eat this!! veejay Cool Sites 3 01-19-2001 10:43 AM




All times are GMT. The time now is 08:03 PM.