PDA

View Full Version : Accessing a non-class variable from inside a class defined in ActionScript



Zomis
July 28th, 2007, 01:08 PM
In my main timeline, I've got a multidimensional array variable named "map".
On frame 1, this "map" variable is filled up with instances of a class named "maparea" (defined in an external actionscript file), but now I'd like to add a function named "neighbourcheck" in the "maparea" class, which will go through it's neighbours on the map and check some properties on them.

But how can I access the main timeline variable "map" from inside my "maparea" class?

I've tried to use "_global.map" but that left me with the error "1120: Access of undefined property _global."

Zomis
August 17th, 2007, 07:34 AM
I found the solution:
Define the global "map" variable as a part of maparea.prototype. Works perfect :) A class' prototype can even contain instances of the class itself, great :D

ikim
August 19th, 2007, 02:54 AM
I´m confused... Is this what it means when they talk about dinamyc classes? This are classes to which you can add properties from outside the class file right? That is the use for prototypes?

Zomis
August 19th, 2007, 05:16 AM
I´m confused... Is this what it means when they talk about dinamyc classes? This are classes to which you can add properties from outside the class file right? That is the use for prototypes?

Well, even though new properties can be added to a class prototype. The prototype is the same for all instances of a class. New properties can also be added to the instance of a class itself, if you declared a class as dynamic (or are using an already declared dynamic class, like the class "Object") then for example:


var a:mydynclass = new mydynclass();
var b:mydynclass = new mydynclass();
a.prop1 = "This is instance number 1";
trace(a.prop1);

// prototype is referenced through the class name, and not through a class variable
mydynclass.prototype.prop = "This is the prototype instance";
trace(mydynclass.prototype.prop);

b.prop1 = "This is another instance of mydynclass";
trace(b.prop1);
b.prop2 = "And this is the property named prop2 for the class instance b. We just added that property with this code.";
trace(b.prop2);

The above code uses ActionScript 3
(I haven't actually tested the code, but it should work like that)