AS3 Vectors: using getters and setters? - actionscript-3

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);

Related

How to access, change, and then return the changed value from another class as3

I am trying to make a game similar to pong, and I am putting the code into each class of my movieclips instead of into the timeline. I have a variable ballspeedx in my class ball. What ballspeedx does is change the speed of which the object moves horizontally. I am trying to call that variable in that class from my main class, named main. Not only do I want to be able to call it, I want to be able to change it and then return the changed value. Can someone explain how to do that?
Make your variable "ballspeedx" a public variable in "ball" class. When you create its object in main class, you can access its public variables. You can also change this. You can add getter and setter functions in "ball" class to change/access the variable value if its private.
Ball.as:
public var ballspeedx:int = 10;
Main.as:
var ball:Ball = new Ball();
trace(ball.ballspeedx);
ball.ballspeedx = 20;
trace(ball.ballspeedx);
You can make ballspeedx a public member of your ball class.
public var ballspeedx:Number;
OR, you can create a getter and setter for the private property:
private var _ballspeedx:Number;
public function get ballspeedx():Number { return _ballspeedx:Number; }
public function set ballspeedx($value:Number):void { _ballspeedx = $value; }

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

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)

ActionScript3: Inheriting constructor arguments from parents

I'm making a game in action script 3. In it, I have an actor class from which player and enemy classes will be derived. I'm doing this so that unless I need to provide specific AI or fancy behavior (such as for bosses), I can just make a new clip in the library for each enemy without making an actionscript file.
However, I've run into a problem.
Whenever I try to pass arguments to the construction of an enemy (make it spawn with more health), I get error 1136 (Incorrect number of arguments.)
This is because the constructor created automatically at runtime doesn't have the same arguments as it's parent class. Is there any way to get around this without making a class file where I copy and paste the parent constructor function for each of my hundreds of enemies?
Edit
actually rereading your question I think you may be looking for super();
Example
public class Actor{
private var myHelth:uint;
public function Actor(helth:uint = 100){
myHelth = helth; //this will be set to 100 if nothing it passed in or the value passed
}
}
Class that extends Actor:
public class Boss extends Actor{
public function Boss(){
super(200); //passes 200 to Actor;
}
}
If you're trying to pass data into a classes constructor you need to make sure it's accepting arguments.
public class Actor{
private var myHelth:uint;
public function Actor(helth:uint = 100){
myHelth = helth; //this will be set to 100 if nothing it passed in or the value passed
}
}
Then to use
var a:Actor = new Actor(200); //setting health to 200
var b:Actor = new Actor(); //using the default of 100
Make sure your symbols in Flash Pro have appropriate AS linkage, then use pass constructor arguments in super statements:
Actor - base class
package
{
public class Actor
{
public function Actor(name:String, role:String)
{
}
}
}
Player - inherits from Actor defining its own constructor parameters:
package
{
public final class Player extends Actor
{
public function Player(... params:Array)
{
// pass desired inherited constructor parameters
super("name", "role");
}
}
}
Enemy - inherits from Actor defining its own constructor parameters:
package
{
public final class Enemy extends Actor
{
public function Enemy(... params:Array)
{
// pass desired inherited constructor parameters
super("name", "role");
}
}
}

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 = ...