简体   繁体   English

在Flash中的单独图层中创建的按钮内的TextField实例在Flex中为null

[英]TextField instance inside a button created in a separate layer in Flash is null in Flex

I've created an button object in flash. 我已经在Flash中创建了一个按钮对象。 The button contains 2 layers. 该按钮包含2层。 One is the background image and on top of it is a textField. 一个是背景图片,最上面是一个textField。 The textfield is dynamic. 文本字段是动态的。 I use the button inside a movieclip and I export it in a SWC. 我使用动画片段中的按钮,然后将其导出到SWC中。 The I'm trying to use it in flex. 我正在尝试在flex中使用它。

I'm trying to do this: 我正在尝试这样做:

var myComponent:MyComponent = new MyComponent();
myComponent.button01.theTextField.text = "Caption";

I get and instance of the button( myComponent.button01 is not null in Flex debugger), but the instance of the textField( myComponent.button01.theTextField ) is null and I'm not able to change the text(but the default text appears onscreen). 我得到了按钮的实例(在Flex调试器中myComponent.button01 不为null ),但是textField( myComponent.button01.theTextField )的实例为null ,我无法更改文本(但默认文本出现了)在屏幕上)。 The code is compiled correctly in flex. 该代码在flex中正确编译。 Does anyone has any idea what is wrong? 有人知道哪里出了问题吗?

I exported the in swc the button control as well. 我也导出了swc中的按钮控件。 So the button is not the default SimpleButton from Flash, but an derived class generated by flash(with the same name as the symbol defined in flash). 因此,该按钮不是Flash的默认SimpleButton,而是Flash生成的派生类(与Flash中定义的符号同名)。 It contains theTextField memeber, which is null. 它包含TextField成员,该成员为null。

Here is the button timeline(Layer 2 contains the textfield, and the textfield instance is named theTextField): alt text http://img59.imageshack.us/img59/5002/timeliney.jpg 这是按钮时间轴(第2层包含文本字段,并且该文本字段实例名为theTextField): 替代文本http://img59.imageshack.us/img59/5002/timeliney.jpg

Actually, there is no problem, everyhting works as it should. 实际上,没有问题,一切正常。 :-) :-)

After you instantiate a class (new MyComponent()) the child objects are not instantiated themselves, that procedure is deffered. 实例化一个类(新的MyComponent())之后,子对象本身并未实例化,因此该过程被推迟。 Only after you add the component to the display list will all the subcontrols be instantiated. 只有将组件添加到显示列表后,所有子控件才会被实例化。 So, you need to access the subcontrols only after you actually added the object to the display list. 因此,仅在将对象实际添加到显示列表之后,才需要访问子控件。

In flex controls you have a creationComplete event that is used for just that purpose. 在flex控件中,您有一个creationComplete事件仅用于该目的。

You can read up on details of object creation here . 您可以在此处阅读有关对象创建的详细信息。

I believe you can not reference children of a button (SimpleButton object). 我相信您不能引用按钮(SimpleButton对象)的子级。

If you write a quick test in Flash, trying to modify the .text property of theTextField , I think you will get a runtime error saying something like: 如果您在Flash中编写一个快速测试,尝试修改theTextField.text属性,我认为您将收到一个运行时错误,内容如下:

1119: Access of possibly undefined property theTextField through a reference with static type flash.display:SimpleButton. 1119:通过静态类型为flash.display:SimpleButton的引用访问可能未定义的属性theTextField。

Sorry, I don't have information on how to resolve your problem. 抱歉,我没有有关如何解决您的问题的信息。 Hopefully, this will be a good starting point to help you figure out an answer to that, though. 希望这将是一个很好的起点,可以帮助您找出答案。

The problem is SimpleButton appears to be a final class. 问题是SimpleButton似乎是最后一堂课。 In other words, you can't addChild to it, or add any properties dynamically. 换句话说,您不能向其添加child,也不能动态添加任何属性。 I did a simple test with a Button (flash.display.SimpleButton) and a TextField. 我对一个Button(flash.display.SimpleButton)和一个TextField做了一个简单的测试。 There was no way to get a handle on the TextField that I could find when it was placed on the Button in the IDE. 当将它放在IDE中的Button上时,找不到在TextField上能找到的句柄。 When I made the button a MovieClip that worked great. 当我将按钮设置为效果很好的MovieClip时。 So you have to put the Button instance on a layer underneath the TextField inside a MovieClip/Sprite Symbol. 因此,您必须将Button实例放在MovieClip / Sprite符号内TextField下方的一层上。 I ended up adding the Symbols through code instead, since I had two slightly different visual representations. 我最终通过代码添加了Symbols,因为我有两个略有不同的视觉表示。

var button:MovieClip;
if (val1 < 10) {
    button = new LowButton();
    button.addEventListener(MouseEvent.CLICK, onLowClick);
    button.textField1.text = "You";
    button.textField2.text = "Stinker";
} else if (val1 > 10 && val1 < 100) {
    button = new MidButton();
    button.textField.text = "Middie";
... 

You get the idea! 你明白了!

It sounds like your reference path to theTextField is problematic. 听起来您对TextField的引用路径有问题。 If you're debugging in Flex, examine the variables tab and see if you can locate myComponent , then myComponent.button01 , and then myComponent.button01.theTextField . 如果要在Flex中进行调试,请检查“变量”选项卡,看看是否可以找到myComponentmyComponent.button01myComponent.button01.theTextField

Create a movieclip. 创建一个动画片段。

Make 3 frames, assign names for them: "out" - frame #1, "click", "over". 制作3帧,并为其指定名称:“输出”-帧1,“单击”,“上方”。

Add textfield to other layer like on your timeline (in first post) 将文本字段添加到其他层,例如在时间轴上(在第一篇文章中)

Add this code to frame #1 将此代码添加到第1帧

stop();
addEventListener(MouseEvent.MOUSE_OVER, on_over, false, 0, true);
addEventListener(MouseEvent.CLICK, on_click, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, on_out, false, 0, true);


function on_out(e:MouseEvent):void 
{
    gotoAndStop("out");
}

function on_click(e:MouseEvent):void 
{
    gotoAndStop("click");           
}

function on_over(e:MouseEvent):void 
{
    gotoAndStop("over");
}

Now, in code, you can use this clip as Super-Button (create, add click event listener, assign caption text without null exceptions, etc...) 现在,在代码中,您可以将此剪辑用作“超级按钮”(创建,添加单击事件侦听器,分配标题文本而没有null异常等)。

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

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