views:

420

answers:

1

I'm looking for a way to extend a TextField that's allready on the stage in Flash (AS3) something like this:

public class ChildTextField extends TextField 
{
    //code for childTextField comes here 
}

I've placed a TextField with instance name 'thetextfield' on the stage. Now I would like to tell flash this textfield is of type ChildTextField. So in my Document Class I declare that textfield as a ChildTextField:

public class DocumentClass extends Sprite()
{
    public var thetextfield : ChildTextField;
}

This throws a Type Coercion failed Error. Is it possible to change the class that is used for a textfield in the Flash IDE like you can do with library symbols?

+1  A: 

Afraid not. You will have to use ActionScript if you want to add your extended textfield class.

EDIT: there is a hack way. source: http://board.flashkit.com/board/archive/index.php/t-738887.html

Actually, I've run into this exact problem before. In my case, I was trying to create a textfield with extra behaviors for other some other non-coder artists to use. I'll tell you my original solution which is all as3, but had a fatal flaw, and my current solution, which is a combination of as3 and jsfl.

The all as3 solution is great except for 2 things: First, it happens at runtime rather than build-time. That means there's a small but real portion of time where the movie isn't correctly initialized. Second, it does not play well if there are multiple frames in the movie. The basic idea is to detect the TextFields you want to change, build things to replace them with, then replace them on stage. You can do this with either by extending TextField, or building a class which contains a TextField and handles the interface to it. Let's say you're doing the first. Add a constructor to SmartTextField that copies all the fields you care about:

public function SmartTextField(TextField tf)
{
this.text = tf.text; //continue with copy of anything relevant. 
}

in your main movie have code which detects and replaces the TextFields you want to replace

 var toreplace:Array = findTextFields(); 
 var tf:TextField;
 var stf:SmartTextField; 
 var where:int;
 for (int i = 0; i < toreplace.length;i++)
 { tf = TextField(toreplace[i]);
   stf = new SmartTextField(tf); 
   where = getChildIndex(tf); 
    addChildAt(stf,where); 
    removeChild(tf); 
 }

This works fine, except for the points above.

The JSFL solution is a bit too complex to go over in detail, but here's the basics. Have an as3 class which wraps a textfield with the new behavior you want. Write a jsfl script which iterates over the selected items, and if it's a textfield, converts to a symbol with a baseclass of your new wrapper class. This has the advantages that it happens at author time, and things like position, instancename and other stuff is automatically preserved. It has the disadvantage that jsfl has a lot of little annoying quirks to work through.

edit: Of course, if this is only for a single movie, you could forego the jsfl converter script, and just do it by hand. convert to symbol -> wrapper baseclass.

Allan
Thanks for all your useful information Allan,I think for my purpose, the easiest way would be to create a wrapper class containing a Textfield and adding the functionality to the Wrapper instead of the TextField. I think it Would be a nice feature to be able to change all stage object's base classes in a future Flash version. Maybe in CS5 ....
yens resmann
yes that is a good idea :). Hopefully CS5 delivers some additions and improvements.
Allan