简体   繁体   English

如何扩展Flash(AS3)舞台上已经准备就绪的Textfield?

[英]How to extend a Textfield that is allready on the stage in Flash (AS3)?

I'm looking for a way to extend a TextField that's allready on the stage in Flash (AS3) something like this: 我正在寻找一种扩展TextField的方法,该方法已经在Flash(AS3)的舞台上实现,如下所示:

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

I've placed a TextField with instance name 'thetextfield' on the stage. 我在舞台上放置了一个实例名称为“ thetextfield”的TextField。 Now I would like to tell flash this textfield is of type ChildTextField. 现在,我想告诉Flash,此文本字段的类型为ChildTextField。 So in my Document Class I declare that textfield as a ChildTextField: 因此,在我的文档类中,我将该文本字段声明为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? 是否可以像使用库符号一样更改Flash IDE中用于文本字段的类?

Afraid not. 不怕。 You will have to use ActionScript if you want to add your extended textfield class. 如果要添加扩展的文本字段类,则必须使用ActionScript。

EDIT: there is a hack way. 编辑:有一种破解方法。 source: http://board.flashkit.com/board/archive/index.php/t-738887.html 来源: 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. 我将告诉您我的原始解决方案全是as3,但是有一个致命的缺陷,而我当前的解决方案是as3和jsfl的组合。

The all as3 solution is great except for 2 things: First, it happens at runtime rather than build-time. 所有的as3解决方案都很棒,除了两件事:第一,它发生在运行时而不是构建时。 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. 基本思想是检测要更改的TextField,构建要替换的TextField,然后在舞台上替换它们。 You can do this with either by extending TextField, or building a class which contains a TextField and handles the interface to it. 您可以通过扩展TextField或构建包含TextField并处理其接口的类来实现此目的。 Let's say you're doing the first. 假设您正在做第一个。 Add a constructor to SmartTextField that copies all the fields you care about: 向SmartTextField添加一个构造函数,该构造函数将复制您关心的所有字段:

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 主电影中的代码具有检测并替换要替换的TextField的代码

 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. JSFL解决方案过于复杂,无法详细介绍,但这是基础知识。 Have an as3 class which wraps a textfield with the new behavior you want. 有一个as3类,该类用所需的新行为包装文本字段。 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. 编写一个jsfl脚本,该脚本遍历所选项目,如果是文本字段,则将其转换为具有新包装器类的基类的符号。 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. 它的缺点是jsfl有很多烦人的怪癖要解决。

edit: Of course, if this is only for a single movie, you could forego the jsfl converter script, and just do it by hand. 编辑:当然,如果这仅用于单个电影,则可以放弃jsfl转换脚本,而只需手动完成即可。 convert to symbol -> wrapper baseclass. 转换为符号->包装器基类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM