Posted by: Actionscription | March 14, 2009

Get the Set out of Here

Getter Setter methods? I never really understood the purpose. If you want a property read/writable, then why not just make it a public variable? Well, that logic has been sufficient until possibly now.

Context

In attempting a 3D tween of a sprite without using Player 10 and it’s Z axis, I came across a conflict in using my preferred tween engine (TweenLite) with an onFrameEnter 3D class (Kieth Peters).  The tween engine and my application run on a time-based tween structure. The 3D class runs on frame rate.  That is the first dilemma. The second is that the tween engine tweens properties only–not methods (that in turn set numerous properties).  This would be the more important dilemma in this posting.

Back to the Point

My little predicament might just be the perfect scenario for using a getter setter function. No? Yes?…  Well, according to my understanding, getter/setter functions enable a programmer to treat a method as a property. That is, you can refer to your hypothetical coolSquareObject.adjustedX if the coolSquareObject class has a public get and/or set method for adjustedX.  Confusing? Well, the basic thing to understand is: This enables the happy programmer to use a method as a propert, essentially allowing him/her to create a custom property.  So, the following is a method that helps me rotate an object around the Y axis:

public function rotateY(angleY:Number):void
{
	var cosY:Number = Math.cos(angleY);
	var sinY:Number = Math.sin(angleY);

	var x1:Number = x * cosY - z * sinY;
	var z1:Number = z * cosY + x * sinY;

	x = x1;
	z = z1;
}

Using a setter function, I can turn the method into a property:

public function set rotateY(angleY:Number):void
{
	var cosY:Number = Math.cos(angleY);
	var sinY:Number = Math.sin(angleY);

	var x1:Number = x * cosY - z * sinY;
	var z1:Number = z * cosY + x * sinY;

	x = x1;
	z = z1;
}

Voila! The method now can be tweened as a property in my tween engine….  Um, not Quite!  Things aren’t that easy. I also need to set up a getter function.  I’m guessing (but not certain) that the tween engine refers to the current state of the property while it calculates how much to change it over time. Thus, we change the set function a little. It needs to store the inputed angle. And we return that angle in the get function. See below:

public function set rotateY(angleY:Number):void
{
	_angleY = angleY;
	var cosY:Number = Math.cos(angleY);
	var sinY:Number = Math.sin(angleY);

	var x1:Number = x * cosY - z * sinY;
	var z1:Number = z * cosY + x * sinY;

	x = x1;
	z = z1;
}

public function get rotateY():Number
{
	return _angY
}

Well, I’m not getting enough work done and the veggie garden patch is calling, so I have to end this here and continue later.  See you soon.

Special note about Getter/Setter functions (Flash CS3): These keywords are supported only when used in external script files, not in scripts written in the Actions panel. Also, you must specify ActionScript 2.0 and Flash Player 6 or later in the Flash tab of your FLA file’s Publish Settings dialog box. – Flash Help Documentation


Categories