giannis christofakis
giannis christofakis

Reputation: 8321

AS3 parameter coercion

I have a simple question,in the below example when i created a new TransitionManager and pass this as a parameter a error occured that says Implicit coercion this is type Class.Why i have to typecast the Object in order to work,i thought object becomes a MovieClip after it extends it.

package {

 import fl.transitions.*;
 import fl.transitions.easing.*;
 import flash.display.MovieClip;
 import flash.events.MouseEvent;

 public class Border extends MovieClip
 {   
   var trManager:TransitionManager = new TransitionManager( MovieClip(this) ); //<--

   public function Border( ) {
   }   

   public function doRotate ( ev : MouseEvent)
   {
      trManager.startTransition({type:Rotate, direction:Transition.OUT, duration:3, easing:Strong.easeInOut, ccw:false, degrees:90});
   }
  }
 }

Upvotes: 0

Views: 273

Answers (1)

Cristi Mihai
Cristi Mihai

Reputation: 2565

It seems like you stumbled across a strange oddity in the Flex compiler.

For a while, it seemed like the this referred to the static class type, but I was wrong, it seems to be just a bug in the compiler, misinterpreting the type of this at compile time.

I wouldn't recommend using this approach though, as the members will be initialized before the constructor is called. Have a look at this example:

package  
{
    public class Foo
    {
        private var bar1:Bar = new Bar("at bar1", Foo(this));

        public function Foo()
        {
            var bar2:Bar = new Bar("in constructor", this);
        }

        private var bar3:Bar = new Bar("at bar3", Foo(this));
    }
}

class Bar {

    public function Bar(scope: String, x:*):void {
        trace(scope, x is Foo);
    }

}

and the obvious answer is:

at bar1 true
at bar3 true
in constructor true

If the constructor was actually suppose to do some real initializations, you would actually use it uninitialized.

In conclusion, keep it simple, move the field initialization in constructor, that's what they are for anyway.

var trManager:TransitionManager;

public function Border( ) {
    trManager = new TransitionManager(this);
}   

Upvotes: 1

Related Questions