AS3 - Returning a property of a class rather than the class itself

In ActionScript 3, there are some classes that will represent a value rather than the class itself. It's hard to explain properly what I mean, so take this example:

var str:String = "something";
var mc:MovieClip = new MovieClip();

trace(str); // something
trace(mc); // [object MovieClip]

You'll notice that the first trace outputs a value, rather than [object String]. Ontop of this, I can still make use of methods of String, like this:

var ar:Array = str.split('s');

Even though in a way you could almost read the above as:

"something".split('s');

I have a class AvLevelData that has some methods that deal with level data (which is essentially a String). At the moment there is a property data:String which represents the core level data.

The question I have is - can I replicate the behaviour of String in that when I trace or assign an instance of AvLevelData, the result is actually the String data.

For example, at the moment I need to go:

var levelData:AvLevelData = new AvLevelData();
trace(levelData.data);

To get the data. I instead want to be able to simply do the following:

var levelData:AvLevelData = new AvLevelData();
trace(levelData); // some level data string

Is this possible?

Answers


If you wan't your object to trace out your own fabricated string then you must implement a toString() function on your AvLevelData class.

In your example above, the MovieClip trace outputs: [Object MovieClip]; this comes from the default toString() implementation for Object (found on Object.prototype) . Note, you cannot override toString() as it only exists on the prototype of Object (remnants of the AS2/Javascript world), all you need to do is provide your own implementation with the same name. For instance:

public function toString():String {
   return "MyCustomObjectString";
}

Some of the most basic types - String, int, Number, uint, Boolean, to name a few - are not classes / objects per se, they are primitives. In some languages there is a wrapper class available for some of these so they can be treated like objects, though Flash doesn't do this so much from my experience.

Probably the best way to answer your question is to make a toString() method for your AvLevelData class:

public function toString():String {
    return data;
}

Any time you treat a class as a string (such as by putting it in trace()), flash (and many other languages) try to call toString() on the object. Typically this results in a string that's not helpful. But if you define your own toString() method, you can control what string gets output.

Another option is to simply do:

trace(AvLevelData.data);

Since that variable is a string, it should trace just fine.


Need Your Help

Want to generate carousel with owl images in it

javascript php jquery html css

I want a hidden div on owl carousel. Please Help me .

Adding transparency / opacity for images in PDF using iTextSharp

c# image pdf itextsharp opacity

I need to set the opacity for an image and add it to the PDF using iTextSharp. I have looked at the http://itextpdf.com/examples/ but could not find any working examples of doing this.