Ultrashock Forums > Blogs > Whirled Views
Passing disparate arguments to getters and setters
Member Blogs
 
Join me for a different perspective on Flash/Actionscript programming: a perspective saturated with hyperbole and couched in precocious vernacular.
With diligent perseverance and a little luck, this blog could become the center of a radical new view of the web.
'Course I'd settle for entertaining and/or insightful.

Welcome to my Whirled View!
Bookmark and Share!
Rate this Entry

Passing disparate arguments to getters and setters

Posted 2008-06-11 at 21:00 by jaseinatl
Part 2 --Intro to getters/setters
-------------------------------------------------

Okay, so lately I have been toying with getters and setters to create some interesting situations.

For example. I want to define a function that an object can use to change its' own properties in some way:
Code:
class raceCar extends MovieClip {
public var car:movieClip;
private var _speed:Number;
private var _raceAction:Function;
public var destSpeed:Number;

public function get speed () {
return (this._speed);
}

public function set speed (num:Number) {
this.destSpeed=num||0; //sets our destination speed
this.onEnterFrame=function () {
if (Math.abs(this.destSpeed-this._speed)>2) {
this._speed+=this.acceleration;
} else {
this._speed=this.destSpeed;
delete this.destSpeed;
delete this.onEnterFrame;
}
}

public function get acceleration():Number {
var diff:Number=this.destSpeed-this._speed;
diff=Math.abs(diff);
var amt:Number =this._raceAction.apply(this._speed,[this.destSpeed]);
return (Math.abs(amt)<diff?amt:diff-1);
}
public function set acceleration(func:Function) {
this._raceAction=func||function ():Number {return (.5*(this.destSpeed-this._speed)||0);};
}
}
Basically, though there are bound to be typos and mistakes in the above code, I wanted to demonstrate an interesting feature of getter setters.

Notice that my getter/setters for "speed" allow the object to change its speed based on it's acceleration (which is really a getter/setter property)

But notice that the GETTER value of acceleration is determined by the private variable _raceAction of type Function and returns a value of type NUMBER.

Whereas the SETTER value of acceleration takes a function for an argument and uses that function to define the private _raceAction property.

the SETTER defines a function in this example, but the GETTER returns a Number.

I doubt this is "best practice" material, but it makes for some interesting interactions.

---------------------------------------------more to come
 
Recent Blog Entries by jaseinatl