Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-12-2012, 09:05 PM   #1
WarKirby
Senior Member
 
Join Date: May 2011
Posts: 283
Default Can i create variables on another object?

Hi everyone. I'm wondering if it's possible to remotely create variables/properties on another object. Specifically, the stage.

I'd like to do something like this

public var stage.myArray:Array = new Array(1,2,3,4,5);

Perhaps that's not the right syntax. But assuming the array doesn't already exist there, can i create it (from an object that is on the stage)
WarKirby is offline   Reply With Quote
Old 02-12-2012, 09:27 PM   #2
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,398
Default

Clarify your intentions.
You might be reinventing the wheel here.

Best practice for instantiating an array of elements is like this:
var array:Array = [1, 2, 3, 4, 5];

You example syntax () would be used to create a new empty Array, or an Array with a specific length.
ie: var array:Array = new Array(5);// 5 empty slots
or: var array:Array = new Array();// empty
[afz]snickelfitz is offline   Reply With Quote
Old 02-12-2012, 09:53 PM   #3
WarKirby
Senior Member
 
Join Date: May 2011
Posts: 283
Default

[QUOTE='[afz]snickelfitz;1116938']Clarify your intentions.
You might be reinventing the wheel here.

Best practice for instantiating an array of elements is like this:
var array:Array = [1, 2, 3, 4, 5];

You example syntax () would be used to create a new empty Array, or an Array with a specific length.
ie: var array:Array = new Array(5);// 5 empty slots
or: var array:Array = new Array();// empty[/QUOTE
Well this is a bit sidetracked. It's an array with 5 elements, with values 1 in element 0, 2 in element 1, etc...
That's rather besides the point though. The question is how/if i can create this array as a property of the stage, from another object. the reason being that everything on the display list already has a reference to the stage, so i'm commonly using it as a place to centrally store data. i'm just concerned at having to continually add more timeline code to put new storage properties there
WarKirby is offline   Reply With Quote
Old 02-12-2012, 10:19 PM   #4
[afz]snickelfitz
Senior Member
 
[afz]snickelfitz's Avatar
 
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 2,398
Default

The stage is a global container for the project, and does not extend Object AFAIK, so I doubt that you can assign any custom properties, such as an Array, to it.

Why not just do this in the Document Class?
If an object has access to the stage, surely it has access to the Document class.
I guess I don't see what problem this would solve that isn't usually solved another way.
[afz]snickelfitz is offline   Reply With Quote
Old 02-12-2012, 10:20 PM   #5
tadster
tadster
 
tadster's Avatar
 
Join Date: Feb 2009
Location: Texas
Posts: 2,111
Default

No you can't do that, because the stage is not a dynamic class.

If you want a quick globally accessible object, you can use a static object.
ActionScript Code:
public static var globalVarsHolder:Object = {};
Because the Object class is dynamic you can add properties to it at run time. Because the variable is static any code that can reference the class in which the static object belongs can reference that static object.

In your case I think you would need to create such a variable in the Document Class, then use the Document Class name to reference it from anywhere.
ActionScript Code:
DocumentClass.globalVarHolder.myArray = [1,2,3,4,5];

You would not be able to properly declare the type of such variables however.
__________________
www.actiontad.com - ActionScript and JavaScript sitting in a tree...
tadster is offline   Reply With Quote
Old 02-12-2012, 10:54 PM   #6
henke37
Senior Member
 
henke37's Avatar
 
Join Date: Mar 2009
Location: Sweden
Posts: 10,833
Send a message via Skype™ to henke37
Default

What you are attempting is called adding a dynamic property. It is not allowed except for a few classes marked as dynamic. You can mark your own class as dynamic, but there is little point then, since you could just add the property for real instead.
__________________
Signature: I wrote a pair of articles about the timeline.
henke37 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:02 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.