简体   繁体   English

AS3-Sprite内的SimpleButton

[英]AS3 - SimpleButton inside of a Sprite

I have a menu that is a sprite and I'm adding a button to be able to minimize the menu. 我有一个菜单,它是一个精灵,我要添加一个按钮以使菜单最小化。 I'm using a SimpleButton() and I added the button as a child of the menu so menu.addChild(button) The problem I'm having is that when I mouse over and click the button none of the events are firing. 我正在使用SimpleButton() ,并将按钮作为菜单的子项添加到菜单中,所以menu.addChild(button)的问题是,当我将鼠标悬停并单击按钮时,不会触发任何事件。 The menu sprite has its own mouse over event and the button should be placed relative to the menu so it makes sense to be a child of the menu. 菜单精灵具有其自己的鼠标悬停事件,并且应该相对于菜单放置按钮,因此可以将其作为菜单的子元素。 How can I make the button still work? 如何使按钮仍然可以使用?

Thanks in advance! 提前致谢!

Edit 编辑
Here is my code: 这是我的代码:

protected const rectangle:Sprite = new Sprite(); 
private const minimizeBtn:SimpleButton = new SimpleButton(new ButtonDisplayState(MINIMIZE_BTN_BG_COLOR, 15, 15), new ButtonDisplayState(0xFF0000, 15, 15), new ButtonDisplayState(0x00FF00, 15, 15), new ButtonDisplayState(0x0FF000, 15, 15)); 
rectangle.addEventListener(MouseEvent.MOUSE_OVER, rectMouseOver);
addChild(rectangle);
rectangle.addChild(minimizeBtn);

The parent sprite (menu) may be eating the mouse events. 父对象(菜单)可能正在吃鼠标事件。 Try setting mouseChildren = true on that object. 尝试在该对象上设置mouseChildren = true。

First of all, why the heck are you defining your button as a const? 首先,为什么要将按钮定义为const? Define it as a non-const variable that's just silly unless you have a REALLY good reason for it. 除非您有充分的理由,否则将其定义为一个愚蠢的非常量变量。 Second, add your event listeners DIRECTLY to the minimizeBtn, not the rectangle container (the parent object). 其次,将事件侦听器直接添加到minimumBtn中,而不是矩形容器(父对象)中。

Also be aware that although the documentation says that the four states of the button are optional in the constructor, they really are not optional at all. 还应注意,尽管文档说按钮的四个状态在构造函数中是可选的,但实际上它们根本不是可选的。 The class is very specific about the constructor parameters which will cause the class to break and not function correctly at runtime (without compile or runtime errors of course, since flash is the champion of that). 该类非常具体地涉及构造函数参数,这些参数将导致该类中断,并且在运行时无法正确运行(当然,没有编译或运行时错误,因为flash是这种情况的冠军)。

So although you are providing all four constructor arguments here, it may be something to tinker with if my previous suggestions fail. 因此,尽管您在此处提供了所有四个构造函数参数,但是如果我之前的建议失败的话,可能需要修改一下。 See the following link for more NFO about the constructor. 有关构造函数的更多NFO,请参见以下链接。

http://selfdocumentingcode.blogspot.com/2009/04/flash-simplebutton-and-mouseover-and.html http://selfdocumentingcode.blogspot.com/2009/04/flash-simplebutton-and-mouseover-and.html

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

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