all groups > flash actionscript > september 2004 >
You're in the

flash actionscript

group:

AS2: Adding Prototype methods, strong typing



AS2: Adding Prototype methods, strong typing Whittaker007
9/22/2004 9:43:16 PM
flash actionscript: Hi there,

Is there an accepted OO practice for extending built-in classes such as
MovieClip etc. in AS2 that allows you to add properties and methods to a class
like you can do with the AS1 prototype object? For example I would like to
combine the mx.screens.Form.move(x,y) and mx.screens.Form.setSize(w,h) which
are used in all display type components into a single method like this:

mx.screens.Form.setLocAndSize ( x:Number, y:Number, w:Number, h:Number ) :
Viod {
this.move ( x , y );
this.setSize ( w , h );
}

myComponent.setLocAndSize ( 10, 10, 300, 200 );

Of course this doesn't work. Interestingly I found that I can still use the
old AS1 method to do this:

MovieClip.prototype.setLocAndSize = function ( x:Number, y:Number, w:Number,
h:Number ) : Viod {....}

and it works, but it has the following drawbacks: 1 - The method becomes
available to all objects that inherit from the MovieClip class, not just screen
components. 2 - Strong typing is totally ignored (I can use a string as one of
the parameters and the compiler gives no warning), 3 - prototype is really AS1
syntax and probably should not be used in AS2 projects.

Anyone know the correct way to do this? And no, making a subclass to extend
mx.screens.Form is not the answer since the actual components that inherit from
mx.screens.Form will not inherit from the new subclass.

Thanks in advance...
Re: AS2: Adding Prototype methods, strong typing Patrick Bay
9/23/2004 10:17:25 AM
Hi,

I was running into issues with so-called strong typing as well. I
had a variable defined as a number:

var myHourVar:Number=new Number();

I had getter/setter functions that were also strictly typed:

public function set myHour(pHour:Number) {
this.myHourVar=pHour;
}

public function get myHour():Number {
return this.myHourVar;
}

And if I assigned a string to it, it returned a string!!! Anyhow,
that being said, you will really have to make sure you type your output
*again* at the getter function:

public function get myHour():Number {
return Number(this.myHourVar);
}

As for your extended class not working, you could be running into
two problems.

1. You defined 'Viod' as your return (maybe just here)
2. Your method calls are trying to reference methods within your own
class. If you don't have them defined, or defined to do something else,
this obviously won't
work. You should be passing your method calls onto your parent
class like this:

super.move ( x , y );
super.setSize ( w , h );


Hope that helps!

Regards,
Patrick

-----------------------------------------------------
www.baynewmedia.com

IRC (www.dal.net) -> #baynewmedia
-----------------------------------------------------



[quoted text, click to view]
AddThis Social Bookmark Button