Button 内的实例名称有问题
我在 Flash 中创建了一个按钮,其中有一个 TextField 和一个 MovieClip,两者都设置了实例名称。它们覆盖了按钮的所有 4 个帧,没有关键帧。
我发现我无法使用我设置的实例名称访问对象,所以我使用这段代码来看看发生了什么:
var obj:DisplayObject = this.m_graphics.btnChange.upState;
for ( var i:int = 0; i < obj.numChildren; i++ )
{
trace( "We have an object at " + i + " - " + obj.getChildAt( i ) + ": " + obj.getChildAt( i ).name );
var t:TextField = obj.getChildAt( i ) as TextField;
if ( t == null )
continue;
trace( " The textfield has text '" + t.text + "' );
}
我得到这个作为输出:
We have an object at 0 - [object Shape]: instance195
We have an object at 1 - [object TextField]: instance199
The textfield has text 'Change'
We have an object at 2 - [object MovieClip]: instance203
所以他们 TextField 和 MovieClip 在那里,他们刚刚有了它们的实例名称重置为通用“instance###”。
有人知道问题是什么吗?如果我将按钮设为 MovieClip,它就可以正常工作(尽管我必须自己控制帧)。
我知道可以使用不同的方法来解决此问题,但这意味着需要更改很多内容,并且我想知道为什么 SimpleButton 会忽略 Flash
EDIT
中设置的 实例名称再深入一点,似乎即使 Flash IDE 中的按钮中没有关键帧,Flash 也会为每个项目创建 4 个实例(例如,通过 upState
进行跟踪会给我一个 文本字段
与实例名称为“instance2”,而在 downState
中,实例名称为“instance4”)。如果我在一种状态下更改文本,它不会反映在其他状态中。
我认为当 Flash 创建对象时,它没有正确复制所有属性(即实例名称)
编辑
我同意 Shane 的答案是一种解决方法 - 我自己说过我自己知道解决问题的不同方法 - 但它忽略了问题,它没有解决它(这是我没有接受它的唯一原因)。我首先来到这里是为了看看是否有人知道为什么会发生这种情况(说实话,这似乎是 SDK 中的一个错误)。
我也理解“你不应该在 SimpleButton 中访问子项;如果你想要更多控制,请使用 Sprite 或 MovieClip”这一论点背后的推理,但我不同意它。 Flash IDE 允许您创建内部带有命名实例的按钮,SimpleButton 文档使您可以访问不同的状态,所以对我来说,这是可接受的行为。如果 SimpleButton 只能用于非常基本的、不可更改的静态按钮(也可以考虑本地化),那么它就毫无用处。您可以使用 Sprite 和 MovieClip,但是您必须自己控制不同的状态,这会变得很尴尬。我有自己的 Button 类来处理样板文件,但我不必重写基本的 SDK 功能,这就是为什么对我来说这是一个错误。
我会在赏金存在期间保持开放状态。如果我什么也没得到,那么我就把它给谢恩。
I've created a button in Flash, and inside that I have a TextField and a MovieClip, both with instance names set. They cover all 4 frames of the button, with no keyframes.
I found I couldn't access the objects using the instance names I'd set, so I used this piece of code to see what's going on:
var obj:DisplayObject = this.m_graphics.btnChange.upState;
for ( var i:int = 0; i < obj.numChildren; i++ )
{
trace( "We have an object at " + i + " - " + obj.getChildAt( i ) + ": " + obj.getChildAt( i ).name );
var t:TextField = obj.getChildAt( i ) as TextField;
if ( t == null )
continue;
trace( " The textfield has text '" + t.text + "' );
}
I get this as output:
We have an object at 0 - [object Shape]: instance195
We have an object at 1 - [object TextField]: instance199
The textfield has text 'Change'
We have an object at 2 - [object MovieClip]: instance203
So they TextField and MovieClip are there, they've just had their instance names reset to the generic "instance###".
Anyone know what the problem is? If I make the button a MovieClip, it works fine (though I have to control the frames myself).
I'm aware of the different methods I could use to work around this, but that means changing a lot of things, and I'd like to know why SimpleButton's ignore the instance names set in Flash
EDIT
Looking into it a bit further, it seems that even though there's no keyframes in the button in the Flash IDE, Flash creates 4 instances of each item (tracing through the upState
for example will give me a TextField
with the instance name "instance2", while in the downState
, the instance name is "instance4"). If I change the text in one state, it doesn't reflect in the others.
I'm thinking that when Flash creates the objects, it's not copying over all the properties properly (namely the instance name)
EDIT
I agree that Shane's answer is a workaround - I'd said it myself that I know of the different methods to get around the problem - but it ignores the problem, it doesn't solve it (it's the only reason why I haven't accepted it). I came to SO in the first place to see if someone perhaps knows why it's happening in the first place (tbh, it seems like a bug in the SDK).
I also understand the reasoning behind the argument "you shouldn't access children in SimpleButton; if you want more control, use Sprite or MovieClip", but I don't agree with it. The Flash IDE lets you create buttons with named instances inside them, and the SimpleButton docs give you access to the different states, so for me, this is accepted behaviour. If SimpleButton can only be used for very basic, non-changeable static buttons (think localisation as well), then it's pretty useless. You can use Sprite and MovieClip, but then you have to control the different states yourself, which gets awkward. I have my own Button class to handle the boilerplate, but I shouldn't have to rewrite basic SDK functionality, which is why for me it's a bug.
I'll keep the bounty open for the time it's there. If I get nothing else, then I'll give it to Shane.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 flash.display.SimpleButton 类不是从
DisplayObjectContainer
继承的,并且状态容器也不是 DisplayObjectContainer:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/SimpleButton.html基本上,简单按钮的定义就是简单。如果您想对内部对象进行更复杂的控制,则必须使用 Sprite 或 MovieClip,并将 buttonMode 设置为 true。
This is because the flash.display.SimpleButton class does not inherit from
DisplayObjectContainer
, and the state containers are also not DisplayObjectContainers: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/SimpleButton.htmlBasically, simple buttons are by their definition simple. If you want more complex control over the inner objects you will have to use a Sprite or MovieClip with buttonMode set to true.