
Welcome Guest
|
|
|
#1
|
||||
|
||||
|
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
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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
|
|
#4
|
||||
|
||||
|
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 |
|
#5
|
|||
|
|||
|
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 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 |
|
#6
|
|||
|
|||
|
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.. |
|
#7
|
||||
|
||||
|
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
|
|
#8
|
|||
|
|||
|
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.. |
|
#9
|
||||
|
||||
|
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
|
|
#10
|
|||
|
|||
|
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; Easier when re reading the code... ![]() C ya |
|
#11
|
||||
|
||||
|
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
|
|
#12
|
||||
|
||||
|
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) |
|
#13
|
||||
|
||||
|
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
|
|
#14
|
||||
|
||||
|
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) |
|
#15
|
||||
|
||||
|
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):VoidNow, 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 } }
__________________
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 |
«
Previous Thread
|
Next Thread
»
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |

Programming Languages
!):



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.
