AS3 access class instance from everywhere - actionscript-3

for my current project I am starting to work with AS3 and I have written a ClipManager class where I can define an MC like "mainView" during initialization like this:
clipManager:ClipManager = new ClipManager(mainView);
With my clipManager I can now easily load stuff into the mainView etc. The problem is that I want every button throughout the whole thing to access Class Methods of this instance to alter the mainView. Can I have something like a global Class instance in Flash or is there any smarter way to achieve what I am trying to do?

You can either add your ClipManager class as a static somewhere - i.e. a god object - (perhaps your main class) and access it through that, or you can use the Singleton pattern.
A common way to implement it in as3:
public class Singleton
{
private static m_instance:Singleton = null; // the only instance of this class
private static m_creating:Boolean = false;// are we creating the singleton?
/**
* Returns the only Singleton instance
*/
public static function get instance():Singleton
{
if( Singleton.m_instance == null )
{
Singleton.m_creating = true;
Singleton.m_instance = new Singleton;
Singleton.m_creating = false;
}
return Singleton.m_instance;
}
/**
* Creates a new Singleton. Don't call this directly - use the 'instance' property
*/
public function Singleton()
{
if( !Singleton.m_creating )
throw new Error( "The Singleton class can't be created directly - use the static 'instance' property instead" );
}
}
Now, to access your class, you call Singleton.instance. There'll only ever be one instance of this class.
As for anti-patterns etc, well that's another post :)

Related

Flash AS3 Dyanmic Text keeps giving an error 1119

So I have a method that takes in a String and then is suppose to set the dynamic textbox on a button to said String.
public function setText(caption:String) {
this.btext.text = caption;
}
I really don't understand why this method is producing a 1119 error.
Access of a possibly undefined property btext through a reference with static type Button.as
The instance name of the Dynamic Textbox is btext and I have tried deleting the textbox and making a new one however this still produces a 1119 error. I also read on another stack question that trying this['btext'].text = caption; which gave me plenty of runtime errors.
Basically what am I doing wrong?
Thank you for any help.
EDIT
Here is the code I am using, and I create an instance of button add it to the stage and store it in an array with this code.
Code to create button
this.buttonArray.push(this.addChild(weaponButton));
Button.as
package {
import flash.display.MovieClip;
import flash.filters.*;
public class Button extends MovieClip {
public function Button() {
}
public function setPosition(xpos:int, ypos:int) {
this.x = xpos;
this.y = ypos;
}
public function setScale(xScale:Number, yScale:Number) {
this.scaleX = xScale;
this.scaleY = yScale;
}
public function addDropShadow():Array {
var dropShadow:DropShadowFilter = new DropShadowFilter(2,45,0, 1,4,4,1,1,true);
return [dropShadow];
}
public function removeDropShadow():Array {
return null;
}
public function setText(caption:String) {
this.btext.text = caption;
}
}
}
As you have stated btext is an instance name of an object. Here is where I assume btext is an object you created in your library.
In your class you are doing 2 things wrong. So lets examine your method.
public function setText(caption:String) {
this.btext.text = caption;
}
The first thing wrong is you are using "this". "this" is a reference to the current instance of the class you are in. And you are saying btext is a property on said instance. Which as I am assuming it is not because you defined btext as an object in your library. This will give you the property is undefined error you are gettting.
Now the second issue at hand is you are about to ask "OK how do I reference btext in my class then". What you need to know is that only objects added to the display list IE:stage can access objects via the stage.
You can do this 3 ways.
The first way is to pass a reference to the button into the class and store it as a property of the class.
The second way is to add your class to stage and in the class listen to the addedToStage event. At that time you can then access the object.
MovieClip(root)["btext"].text
The first 2 methods are not good practice since btext is not apart of the class and a general rule of thumb would be to encapsulate your class.
To make this work what you could do is have your class assign the value to a property in your class then fire an event and make the parent of this class listen to that event then just grab the value and assign.
Here is some suggested reading
I think the variable btext doesn't exist at all, or is it inherited from Movieclip?

Refer to a Spark view component in code

I'm building a mobile AIR app using Flash Builder 4.5. The initial view in my views package is TestHomeView.mxml. I want to refer to it in one of my .as classes elsewhere in the app, and I'm not sure how to do that.
Theoretically I should be able to add an "id" attribute to TestHomeView.mxml, but FB gives me an error: "id is not allowed on the root tag of a component". The root tag is s:view.
The reason I need to do this is that within another class I make various calculations and then need to pass an array of values to a component in my view class. So in SomeOtherActionScriptClass.as I first assemble the array, myArray, and then in that class I want to do this:
myViewComponent.viewArray = myArray;
If I'm going to do that, I also need to import the view class into the .as class, which strikes me as weird. So is there a simple way to do what I want, or do I have to dispatch a custom event which contains the array, and listen for it in the view class?
EDIT - Based on the below MVC suggestion I did the following in model:
[Bindable]
public class Model
{
private static var myModel:Model;//doesn't let me name it 'model' because
//I have a package named 'model'
public var myArray:Array; //its value set later in model code
public function Model()
{
if ( Model.myModel != null ){
throw new Error( "Only one Model instance should be instantiated" );
}
}
// singleton: always returns the one existing static instance to itself
public static function getInstance() : Model {
if ( myModel == null ){
myModel = new Model();
}
return myModel;
}
Then in the view code I have:
[Bindable] //do I actually need this?
private var myModel:Model = Model.getInstance();
var viewArray = new Array();
viewArray = myModel.myArray;
But it is coming back null. It isn't null when I put a breakpoint in the Model class, but when I try to access it from the view class, it's null. The model itself isn't null, but that variable is.
What am I doing wrong?
Thanks.
First, if you are trying to make a singleton in AS3 you should first create a class, within the same class file as Model, that is used to ensure you can only create the class once.
Add this class at the bottom of the Model class file (outside of the Model class):
internal class SingletonEnforcer{}
Then create the Model constructor like this:
public function Model(enforcer:SingletonEnforcer){ Init(); } // if init code is needed
public static function get Instance():Model
{
if (!myModel){
myModel = new Model(new SingletonEnforcer());
}
return myModel;
}
Now you don't have to throw an exception for creating a second instance because it isn't possible.
I'm not sure about your first question of referencing your app's main mxml, but if you were asking how to call the app that is running (like WindowedApplication in AIR) then you would call it like this:
// my WindowedApplication file = MyApp.mxml
MyApp(this.parentApplication)
That will return the app's instance.
Once you've set up the Singleton like I have it above you should be able to access your array like:
Model.Instance.myArray;
I hope this helps!
Follow the MVC pattern.
Create Model class (make it Bindable) with a property viewArray. Bind to this property from your View. And in any other class just change viewArray property of the model. The binding event will be fired and this property will also be changed in your View. To make your Model "visible" from any point, you can make it a Singleton.

Bindable getter function of singleton instance never being called in a data binding expression

I have a singleton class that looks something roughly like this (only with more bindable public properties):
public class Session extends EventDispatcher
{
private var _Id:String;
private static const _instance:Session = new Session( SingletonLock );
private static const SESSID_CHANGED:String = 'SessionIdChanged';
public function Session( lock:Class ){
//SingletonLock is an empty class not available outside this file
if( lock != SingletonLock ){
throw new Error("Don't instantiate Session. Use Session.instance");
}
_Id = "";
}
public static function get instance():Session{
return _instance;
}
// Changes a blob object (from the server xml for sessions) to a session object
public function updateFromXMLObj(s:ObjectProxy):void
{
_instance.Id = s.Id;
}
[Bindable(event=SESSID_CHANGED)]
public function get Id():String{
return _Id;
}
public function set Id(new_id:String):void{
if(this._Id != new_id){
this._Id = new_id;
this.dispatchEvent(new Event(SESSID_CHANGED));
}
}
public function registerOnSessionChange(listener:Function):void{
addEventListener(SESSID_CHANGED,listener);
}
public function unregisterOnSessionChange(listener:Function):void{
removeEventListener(SESSID_CHANGED,listener);
}
}
The idea is that in some mxml code, I have a databinding expression like the following:
<mx:HTTPService id="homeReq" url="{URLs.homepath(Session.instance.Id)}" ... />
where I want the url for homeReq to be updated when the sessionId changes. In addition, other parts of the code (written in Actionscript) need to be able to register their listeners for when the sessionId changes, so they call registerOnSessionChange and unregisterOnSessionChange to manage those listeners.
The abnormal behavior I'm discovering is that the event listeners registered through registerOnSessionChange are indeed being called when the session Id changes, but the MXML data binding expression is not updating. I've tried all combinations of dispatching the event during the capture phase, and making it not cancelable, but to no avail. My understanding of [Bindable (event= ...)] is that the MXML should update the url string when the event specified is dispatched, so what am I doing wrong or misunderstanding?
Note: I realize there are lots of different ways of doing the singleton pattern in Actionscript, but unless the way I am doing it is actually causing my problem somehow, I'd appreciate not getting sidetracked by discussing alternatives.
I think that {URLs.homepath(Session.instance.Id)} this is not binding to a variable instead is executing a method of an object, have you tried to do something like this:
[Bindable]
private var _url:*
Then setting the initial value to _url at init or complete:
_url = {URLs.homepath(Session.instance.Id)};
Linking to the binded variable in the MXML
<mx:HTTPService id="homeReq" url="{_url}" ... />
Then updating the _url variable should automatically update the HTTPService url...
Make an MXML form containing a combobox for course number of 5th semester. On selecting the coruse, display the course name and max marks for the selected course.
Data Binding: <mx:Binding>

How to reference objects in AS3?

I've learned one way to do that, but I want to improve my knowledge. For simplicity I'm not going to use import neither extends in the code below.
1
public class Main
{
public function Main()
{
new MyCustomObject(stage);
}
}
2
public class MyCustomObject
{
public var referenceStage:Stage = new Stage();
public function MyCustomObject(xxx:Stage)
{
this.referenceStage = xxx;
referenceStage.addChild(this);
}
}
I've learned it reading a tutorial over internet, but I want to know where I can find more samples on how to reference objects in AS3. For future codes, I want to add hitTest and the like.
Thanks !
The best place is the ActionScript 3 Reference from Adobe: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html
Here is the specific section on objects: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Object.html
if you absolutely want to pass a stage reference through an argument to a constructor, you can do so about how you have it laid out (although get rid of the new Stage() call, which won't do anything).
that said, .stage is a property available to all display objects that are in the display list (meaning: the have been added via addChild or addChildAt).
you're probably getting that error trying to reference a .stage property of an object before it's been added to the display list. this is a common error, and can be handled by waiting to reference the .stage property until it has been added, usually using addEventListener(Event.ADDED_TO_STAGE...
so instead of
public class MyObject extends Sprite {
public function MyObject():void{
this.x = this.stage.stageWidth/2;
}
}
you'd use something like this
public class MyObject extends Sprite {
public function MyObject():void{
this.addEventListener(Event.ADDED_TO_STAGE, this.addedHandler, false, 0, true);
}
private function addedHandler(e:Event):void{
this.x = this.stage.stageWidth/2;
}
}
HTH
In your example, you don't need do call new Stage() in your CustomObject
public var referenceStage:Stage;
is enough
A hitting function may be found here http://troygilbert.com/2007/06/pixel-perfect-collision-detection-in-actionscript3/
Possible solutions are:
Instead of passing the stage object, you can also pass the main object and calling functions in the main object for the custom object
Maintain an array in the MainObject with which you want do collisions test.
Implementing an Interface (extend an object) with a function which do the hit test agains the array in the MainObject (for example went the EntreFrame Event is fired)
Custom Events are the solution for communicating with the main object loosely
Passing a reference to an object in the constructor is a classic OOP pattern

ActionScript 3 Singleton instantiation - advice

I have an AS3 Singleton:
package
{
public class Singleton
{
public function Singleton(enforcer:SingletonEnforcer):void
{
if(!enforcer){throw new Error("Only one instance of Singleton Class allowed.");}
}
private static var _instance:Singleton;
public static function getInstance():Singleton
{
if(!Singleton._instance)
{
Singleton._instance=new Singleton(new SingletonEnforcer());
}
return Singleton._instance;
}
}
}
class SingletonEnforcer{}
Consider prop and func() to be a property and method respectively of the Singleton class.
How should I access them?
1. Make them static and use this:
Singleton.getInstance();
Singleton.prop;
Singleton.func();
2. Not make them static and use this:
Singleton.getInstance().prop;
Singleton.getInstance().func();
Does it matter, or is it just visual prefference?
Thank you.
The reason to use a singleton instance is so that you can have class members used in a (relatively) static way.
I won't get into the arguments over whether or not to use a singleton here, there's a very long debate over whether it's a good pattern or not.
Typically, when singletons are used, you store access to them in a local variable and use them like any other class instance. The primary difference, is instead of using:
foo = new Foo();
You use:
foo = Foo.instance;
//Actionscript supports properties which makes this a self-initializing call
-or-
foo = Foo.getInstance();
Followed by
foo.bar();
foo.baz();
foo.fizz = 'buzz';
This doesn't mean that Foo can't have static members of the class, but the rules for adding static members on a Singleton are the same for adding static members to any other class. If the function belongs to the instance, it should be used on the instance, if it belongs to the class, it should be static.