单击事件未到达根闪存 object
click events don't reach root flash object
我不久前开始使用 as3,主要使用 starling 而不是原生 flash objects。今天我决定尝试一下raw flash,并创建了一个简单的按钮:
private var _button: SimpleButton;
public function Main()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
_button = new SimpleButton(new BUTTON); //BUTTON is an embedded bitmap
addChild(_button);
_button.useHandCursor = true;
_button.addEventListener(MouseEvent.CLICK, OnClick);
}
private function OnClick(e:MouseEvent):void
{
trace("clicked");
}
这在 Starling 中绝对有效,但我发现由于某种原因,点击事件不会分派给按钮。鼠标悬停时也不会显示手形光标。我尝试了鼠标悬停、鼠标抬起和其他鼠标事件,但其中 none 有效。我检查了按钮是否已正确添加到舞台上。此外,当我将鼠标点击事件侦听器添加到舞台本身时,点击会正常注册。当我将侦听器添加到 Main 时,不会再次注册任何事件。
我可能误解了一些明显的东西。也许我需要手动将事件发送到 children?但那将是 strage,虽然。或者它可能是 FlashDevelop 中的错误?请帮忙
SimpleButton
的构造函数有4个可选参数:
public function SimpleButton(upState:DisplayObject = null,
overState:DisplayObject = null,
downState:DisplayObject = null,
hitTestState:DisplayObject = null)`
其中您提供第一个 (upState
):
_button = new SimpleButton(new BUTTON); //BUTTON is an embedded bitmap
其他默认为null
。这包括 hitTestState
与 class hitTestState
:
的 属性 相同
Specifies a display object that is used as the hit testing object for the button. [...] If you do not set the hitTestState property, the SimpleButton is inactive — it does not respond to user input events.
它还包括您的问题的解决方案:
For a basic button, set the hitTestState property to the same display object as the overState property.
但是,如果您想要的只是向 Bitmap
添加点击功能,它本身没有,因为它不是 InteractiveObject
, 只需使用 Sprite
:
// entry point
_button = new Sprite();
_button.addChild(new BUTTON); //BUTTON is an embedded bitmap
addChild(_button);
_button.useHandCursor = true;
_button.addEventListener(MouseEvent.CLICK, OnClick);
仅当您需要其 4 种状态的功能时才使用 SimpleButton
。
请注意重载的术语 "stage":
I checked that the button is correctly added to stage.
如果您指的是您可以在 Adobe Flash IDE 中看到的 "drawing area" 或带有 "stage" 的主要时间线,那么是的,您的按钮是正确的添加到那个。
然而 DisplayObject
的 stage
属性 是不同的。它是显示列表的最顶部元素,也是添加 Main
class 实例的容器,发生在 FlashPlayer 中开始执行 .swf 时。
当您的 Main
class 通过调用自己的 addChild()
将 _button
添加到自身时,按钮将添加到 Main
的实例中,并且不是stage
,这是两个不同的对象。
我不久前开始使用 as3,主要使用 starling 而不是原生 flash objects。今天我决定尝试一下raw flash,并创建了一个简单的按钮:
private var _button: SimpleButton;
public function Main()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
_button = new SimpleButton(new BUTTON); //BUTTON is an embedded bitmap
addChild(_button);
_button.useHandCursor = true;
_button.addEventListener(MouseEvent.CLICK, OnClick);
}
private function OnClick(e:MouseEvent):void
{
trace("clicked");
}
这在 Starling 中绝对有效,但我发现由于某种原因,点击事件不会分派给按钮。鼠标悬停时也不会显示手形光标。我尝试了鼠标悬停、鼠标抬起和其他鼠标事件,但其中 none 有效。我检查了按钮是否已正确添加到舞台上。此外,当我将鼠标点击事件侦听器添加到舞台本身时,点击会正常注册。当我将侦听器添加到 Main 时,不会再次注册任何事件。 我可能误解了一些明显的东西。也许我需要手动将事件发送到 children?但那将是 strage,虽然。或者它可能是 FlashDevelop 中的错误?请帮忙
SimpleButton
的构造函数有4个可选参数:
public function SimpleButton(upState:DisplayObject = null, overState:DisplayObject = null, downState:DisplayObject = null, hitTestState:DisplayObject = null)`
其中您提供第一个 (upState
):
_button = new SimpleButton(new BUTTON); //BUTTON is an embedded bitmap
其他默认为null
。这包括 hitTestState
与 class hitTestState
:
Specifies a display object that is used as the hit testing object for the button. [...] If you do not set the hitTestState property, the SimpleButton is inactive — it does not respond to user input events.
它还包括您的问题的解决方案:
For a basic button, set the hitTestState property to the same display object as the overState property.
但是,如果您想要的只是向 Bitmap
添加点击功能,它本身没有,因为它不是 InteractiveObject
, 只需使用 Sprite
:
// entry point
_button = new Sprite();
_button.addChild(new BUTTON); //BUTTON is an embedded bitmap
addChild(_button);
_button.useHandCursor = true;
_button.addEventListener(MouseEvent.CLICK, OnClick);
仅当您需要其 4 种状态的功能时才使用 SimpleButton
。
请注意重载的术语 "stage":
I checked that the button is correctly added to stage.
如果您指的是您可以在 Adobe Flash IDE 中看到的 "drawing area" 或带有 "stage" 的主要时间线,那么是的,您的按钮是正确的添加到那个。
然而 DisplayObject
的 stage
属性 是不同的。它是显示列表的最顶部元素,也是添加 Main
class 实例的容器,发生在 FlashPlayer 中开始执行 .swf 时。
当您的 Main
class 通过调用自己的 addChild()
将 _button
添加到自身时,按钮将添加到 Main
的实例中,并且不是stage
,这是两个不同的对象。