Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the actionscript3.0 class heirachy fail (sometimes)?

All Objects in actionscript3.0 inherit from the Object class, but the actionscript3.0 compiler seems not to be smart enough to understand this.

take a look at the following code:

package{
   public class TestOne{
      public function TestOne(){
        var t2: TestTwo = new TestTwo();
        trace(t2.toString()); // COMPILE TIME ERROR
        trace((t2 as Object).toString(); // [object TestTwo]

        var t22 : * = new TestTwo();
        trace(t22.toString()); // [object TestTwo]
        trace((t22 as Object).toString(); // [object TestTwo]
      }
   }
}
class TestTwo{}

t2.toString() gives a compile time error because the data type t2 does not include toString(). However, t2 does include toString() because it is an object as (t2 as Object).toString() shows. If we do not give the variable a datatype, like t22, then the problem is never encountered. Why cant the actionscript3.0 compiler relize that t2 is both TestTwo and Object?

like image 820
ForYourOwnGood Avatar asked Feb 18 '09 02:02

ForYourOwnGood


People also ask

What does ActionScript 3 do?

Action Script 3.0 in Flash allows you to create all kinds of fully interactive applications such as dynamic websites and computer games. Also, AS3. 0 is an object-oriented programming language; if you are familiar with AS3. 0, it will help you to learn other object-oriented language such as Javascript.

What is the difference using ActionScript 2.0 and ActionScript 3.0 for controlling video?

The main difference is that you can develop flash applications with a much stronger OOP influence than in AS2. AS3 makes it much easier to utilise third party code such as Greensock's Tweenlite, Papervision 3D and box2d.

What are the complex data types used in AS3 0?

In ActionScript 3.0, the Number data type can represent integers, unsigned integers, and floating-point numbers.

What is a class in ActionScript?

In ActionScript 3.0, every object is defined by a class. A class can be thought of as a template or a blueprint for a type of object. Class definitions can include variables and constants, which hold data values, and methods, which are functions that encapsulate behavior bound to the class.


1 Answers

This is because

Methods of the Object class are dynamically created on Object's prototype. To redefine this method in a subclass of Object, do not use the override keyword. For example, a subclass of Object implements function toString():String instead of using an override of the base class.

So if you cast TestTwo to an Object, the compiler knows those methods will be implemented. If you don't cast it, TestTwo does not inherit those methods and so they have not been implemented and will error.

It's a bit of a weird one!

like image 66
James Hay Avatar answered Oct 07 '22 09:10

James Hay