Actionscript 3.0 Setter - Getter - actionscript-3

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

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)

Get stage in ActionScript-3 without a DisplayObject?

How can I get a reference to my stage without having a Sprite/DisplayObject that is added to the stage already ?
More info: I have a static class that is a utility class and I want it to initialize in static class constructor but I also need the reference to the stage.
public class UtilClass
{
trace("init: " + stage);
}
First thing that is called in my AS-3 apps is the constructor of my main Sprite/DisplayObject and it has access to the stage. So the stage exists at that point. Then I call utility methods of my UtilClass. Now I want it to initialize by itself on the first use (when stage is already in existance).
I want to know if stage object can be accessed from anywhere without being initialized from outside of the utility class.
Edit:
public class SimpleSprite extends Sprite
{
public static var aaa:int = 12;
public static function test():void
{
trace("here I am");
}
trace(aaa, Capabilities.screenResolutionX+", "+Capabilities.screenResolutionY);
test();
}
The stage reference is available in your MainTimeline or Main instance, depending on platform. You can add code there to pass that reference to other classes should you need it. The class should have a method (static, in your case) that'll accept a Stage parameter and store it somewhere inside the class.
public class UtilClass {
private static var theStage:Stage=null;
public static function initialize(s:Stage):void {
if (theStage) return; // we're initialized already
theStage=s;
}
// once you call this, you can "trace(theStage)" and get correct output
// other methods can also rely on theStage now.
}
Then you call UtilClass.initialize(stage); and you're set.
You will need to initialise your UtilClass and pass the stage reference. I recommend you to have a Class only for 'manage' Stage reference.
You could try something like this (just a quick example):
public class StageReference
{
public static const STAGE_DEFAULT:String = 'stageDefault';
protected static var _stageMap:Dictionary;
public static function getStage(id:String = StageReference.STAGE_DEFAULT):Stage
{
if (!(id in StageReference._getMap()))
throw new Error('Cannot get Stage ("' + id + '") before it has been set.');
return StageReference._getMap()[id];
}
public static function setStage(stage:Stage, id:String = StageReference.STAGE_DEFAULT):void
{
StageReference._getMap()[id] = stage;
}
public static function removeStage(id:String = StageReference.STAGE_DEFAULT):Boolean
{
if (!(id in StageReference._getMap()))
return false;
StageReference.setStage(null, id);
return true;
}
protected static function _getMap():Dictionary
{
if (!StageReference._stageMap) StageReference._stageMap = new Dictionary();
return StageReference._stageMap;
}
}
When you start your application (Main Class or where you start to include your logic)
StageReference.setStage(stage);
And when you need to get the stage reference
trace('Checking the Stage: ', StageReference.getStage());

Getting error Type 1061: Call to a possibly undefined method addEventListener through a reference with static type

I moved my code from my Application to a separate AS class file and now I'm getting the following errors:
1061: Call to a possibly undefined method addEventListener through a reference with static type Class.
1180: Call to a possibly undefined method addEventListener.
.
package managers {
import flash.events.Event;
import flash.events.EventDispatcher;
public class RemoteManager extends EventDispatcher {
public function RemoteManager() {
}
public static function init(clearCache:Boolean = false):void {
addEventListener(SETTINGS_CHANGE, settingChangeHandler);
addEventListener(ITEMS_UPDATED, itemsUpdatedHandler);
}
}
}
Your code
addEventListener(SETTINGS_CHANGE, settingChangeHandler);
evaluates to
this.addEventListener(SETTINGS_CHANGE, settingChangeHandler);
There is no this in a static method, since it's designed to function without an instance. Furthermore, you cannot attach an event listener and dispatch events from a static class.
Either change your function declaration to
public function init(clearCache:Boolean = false):void
Or implement a singleton pattern to kinda get a "static class, that dispatches events".
Singleton with event management.
You have declared your method init as static , so all you can use into this one is static field, static method, no object that belong to the instance.
Remove the static from the function or try to implements a singleton if it what you are after.
here a quick really simple one :
public class RemoteManager extends EventDispatcher {
private static var _instance:RemoteManager;
public function static getInstance():RemoteManager{
if (_instance == null) _instance=new RemoteManager();
return _instance;
}
public function RemoteManager() {
if (_instance != null) throw new Error("use getInstance");
}
public static function init(clearCache:Boolean = false):void {
getInstance().addEventListener(SETTINGS_CHANGE, settingChangeHandler);
getInstance().addEventListener(ITEMS_UPDATED, itemsUpdatedHandler);
}
}
// use it
RemoteManager.init();

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