ActionScript 3.0: Trivial setter calls getter three different times. Why? - actionscript-3

Take the following code:
private var m_iWidth:int;
[Bindable]
public function get width():int
{
Alert.show("getter");
return m_iWidth;
}
private function set width(pValue:int):void
{
Alert.show("setter");
m_iWidth = pValue;
}
private function someFunction(pWidth:int):void
{
width = pWidth;
}
The output of width = pWidth; is:
getter
setter
getter
getter
Please explain. Thanks.

1) When the property is set, the code first calls the getter to see if the value is different. If it's the same then the setter isn't called (explains the first get/set pair).
2) If the property is bound, after it's set any access will call the getters (explains the last two getter calls)

Related

How to define an object in actionscript 3 using a custom class

Hi my problem is i have to be able to reference certain fields inside my Customer object.]
I am studying AS3 at the moment and being taught custom classes, but we are taught to use the toString method of returning a value i guess you could call it, what i need is to be able to call one field to identify the object i.e. name field from the object in the array, here's my code
package valueObjects
{
public class Person
{
//instance variables
protected var name:String;
protected var address:String;
protected var phoneNo:String;
public function Person(n:String,a:String,p:String)
{
name=n;
address=a;
phoneNo=p;
}
public function toString():String
{
//returns string
return name+":"+address+":"+phoneNo;
}
}
}
some reason it will not put that whole block of code together like THIS IS
So now how do i define it not toString but in object form ??
I think what you are trying to do is access the name, address and phoneNo vars from a different class?
If so, you have to declare them as public vars instead of private vars.
public var name:String; //now this can be accessed from other classes: thisClassInstance.name
If you want to have them read-only from other classes, you have to use a getter method:
protected var name_:String; //local var name for full access;
public function get name():String {
return name_; //this can be access by doing thisClassInstance.name
}

How to call some mxml function from class?

I have 2 files: Main.mxml with application and one MyObject.as.
I create the instance of MyObject in mxml and can call its every public function from mxml. But what if for some reason I need to call some function declared in mxml from MyObject class? How to do that? I thought that I could pass the reference to main.mxml class into this object but I couldn't figure out what exact class is it (it inherits Application, right, but what exact class is it?)
Thanks
It is of type Main (it takes on the name of the mxml file). You can add a static variable and getter method to it:
private static var _instance : Main;
public static function get instance () : Main {
return _instance;
}
Then let instance refer to this after the application is complete:
private function applicationCompleteHandler():void
{
_instance = this;
}
Don't forget to set applicationComplete="applicationCompleteHandler" in your <mx:Application> tag.
After that you can call Main.instance from anywhere in your program to access the methods and variables.
If you are instantiating the MyObject class in your Main.mxml, you could also accomplish access to a method in Main by passing the method as a Function into the object.
Suppose you have in Main.mxml the function:
private function doSomething():*{
...
}
With an appropriate setter in MyObject.as:
private var _mainFunction:Function;
public function set mainFunction(f:Function):void
{
_mainFunction = f;
}
Then you can pass the method when you instantiate the MyObject class in the mxml:
<*:MyObject mainFunction='doSomething'/>
And now you just call _mainFunction in the MyObject.as code whenever you need it.
Of course, Weltraumpirat's suggestion would be more efficient if you needed to access more than one method and/or variable on your Application.

ActionScript Calling Private Functions By Changing Public Variables?

i've never tried to do this before, so my head a swimming a bit. i'd like to have a public boolean called enabled in myClass custom class. if it's called to be changed, how do i trigger a function from the change?
should i add an Event.CHANGE event listener to my variable? can i do that? or is there a more standard way?
We usually use properties for that.
Properties are just like public variables for the outside -- you can set instance.enabled = true; and so forth.. But you define properties as getters and/or setters functions for the class.
They are the perfect place for custom logic to be executed on value changes.
For example:
public class CustomClass {
private var _enabled:Boolean = false;
public function set enabled(value:Boolean):void {
trace('CustomClass.enabled is now', value);
this._enabled = value;
}
public function get enabled():Boolean {
trace('CustomClass.enabled was retrieved');
return this._enabled;
}
}
Note that they can't have the same name as your private variable and you don't need both of them defined. Actually, you don't even need a variable for a setter/getter. You could use them just like any function -- they just supply you with a different syntax.
For example:
var object:CustomClass = new CustomClass();
object.enabled = false;
if (object.enabled) {
...
}
They are great to expose a simple API, keeping you from rewriting outside code if the class' internals have to change.
AS3 Reference on getters and setters.

Actionscript 3.0 Setter - Getter

I want to pass Value from Constructor in my Main Class to another Class.
Main Class:
public function Main() {
Snap.locationX = 350;
}
Another Class:
public function get locationX():Number{
return _value;
}
public function set locationX(x:Number):void{
_value = x;
}
It returns 1061: Call to a possibly undefined method locationX through a reference with static type Class.
What am I doing wrong?
The setter and getter methods you have defined above are INSTANCE methods. It seems like you are calling Snap.locationX on the Snap class itself and not on an instance of the Snap class.
try (under Main()):
var snapObj:Snap = new Snap();
snapObj.locationX = ...

AS3 Vectors: using getters and setters?

Is there a way to use getters and setters for Vectors?
Say, in my Main class, I would like to write
myVector.push(item);
and in another class, I have written:
public function get myVector():Vector.<int> {
return _opponentCardList;
}
public function set myVector(myVector:Vector.<int>):void {
_myVector = myVector;
}
This doesn't really work as you have to set _myVector to a Vector. But what if you just want to push(), pop() or splice?
Your getter and setter use different variables - is that intentional?
If the getter/setter myVector is in a different class, you need an instance of that class in your Main class before you can access it from there.
//in the Main class.
var obj:OtherClass = new OtherClass();
//the constructor of OtherClass should initialize _myVector
//otherwise you will get a null pointer error (1009) in the following line
obj.myVector.push(item);