子精灵如何阻止鼠标事件到达其父精灵?

发布于 2024-08-07 14:33:29 字数 2974 浏览 3 评论 0原文

下面是一个简单的 Flex 动作脚本项目的代码。精灵部分覆盖了超链接。发生的情况是,当您将鼠标悬停在精灵上时,如果您也将鼠标悬停在超链接上,则超链接将被激活。我想阻止这种情况发生。我希望只有当鼠标悬停在超链接上时才激活超链接,但当房子悬停在覆盖它的精灵上时则不激活。

我们这里有一个精灵,它是超链接所在文本字段的子级。因此,问题是(我认为):子显示对象如何中断事件流,以便鼠标悬停事件永远不会到达父级?

package {
import flash.display.Sprite;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TextEvent;
import flash.external.ExternalInterface;
import flash.filters.BevelFilter;
import flash.geom.Rectangle;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.text.Font;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.setTimeout;


public class SpriteHyperlinkTest extends Sprite
{
    private var style : StyleSheet = new StyleSheet();
    public function SpriteHyperlinkTest()
    {
        createOutputTextField();
    }

    public var output_txt : TextField;

    private function createOutputTextField() : void {

        ////////////// SETUP  TEXTFIELD ///////////////

        var hover : Object = new Object();
        hover.fontWeight = "bold";
        hover.color = "#0000FF";
        var link : Object = new Object();
        link.fontWeight = "bold";
        link.textDecoration = "underline";
        link.color = "#555555";
        var active : Object = new Object();
        active.fontWeight = "bold";
        active.color = "#FF0000";

        var visited : Object = new Object();
        visited.fontWeight = "bold";
        visited.color = "#cc0099";
        visited.textDecoration = "underline";

        style.setStyle("a:link", link);
        style.setStyle("a:hover", hover);
        style.setStyle("a:active", active);
        style.setStyle(".visited", visited);
        output_txt = new TextField();
        output_txt.backgroundColor = 0xFFFFFF;
        output_txt.background = true;
        //output_txt.embedFonts = true;
        output_txt.wordWrap = true;
        output_txt.multiline = true;

        output_txt.name = "output_txt";         
        output_txt.x = 100;
        output_txt.y = 100;
        output_txt.width = 300;
        output_txt.height = 200;

        output_txt.htmlText = "<b>sample <a href='http://www.google.com'>hyperlink text</a></b>"; 
        addChild(output_txt);
         var mySprite:Sprite = new Sprite();
         mySprite.graphics.lineStyle(.5,0x000000);
         mySprite.graphics.beginFill(0xff0000, 1);
         mySprite.alpha = .7;
         mySprite.graphics.drawRect(100, 100, 90, 20);
         mySprite.graphics.endFill();
         mySprite.useHandCursor = true;
         mySprite.mouseChildren = true;
         mySprite.buttonMode = true;
         mySprite.name = "Sprite1";
         this.addChild(mySprite);

         output_txt.styleSheet = style;
    }

}
}

Below is the code for a simple Flex actionscript project. A sprite is partially covering a hyperlink. What's happening is that when you hover over the sprite, if you're also hovering over the hyperlink, the hyperlink is activated. I want to prevent that. I want the hyperlink to be activated only when the mouse hovers over it -- but not when the house hovers over the sprite which covers it.

What we have here is a sprite which is a child of the textfield in which the hyperlink resides. Therefore the question is (I think): how can a child display object interrupt the event flow so that the mouseover event never reaches the parent?

package {
import flash.display.Sprite;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TextEvent;
import flash.external.ExternalInterface;
import flash.filters.BevelFilter;
import flash.geom.Rectangle;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.text.Font;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.setTimeout;


public class SpriteHyperlinkTest extends Sprite
{
    private var style : StyleSheet = new StyleSheet();
    public function SpriteHyperlinkTest()
    {
        createOutputTextField();
    }

    public var output_txt : TextField;

    private function createOutputTextField() : void {

        ////////////// SETUP  TEXTFIELD ///////////////

        var hover : Object = new Object();
        hover.fontWeight = "bold";
        hover.color = "#0000FF";
        var link : Object = new Object();
        link.fontWeight = "bold";
        link.textDecoration = "underline";
        link.color = "#555555";
        var active : Object = new Object();
        active.fontWeight = "bold";
        active.color = "#FF0000";

        var visited : Object = new Object();
        visited.fontWeight = "bold";
        visited.color = "#cc0099";
        visited.textDecoration = "underline";

        style.setStyle("a:link", link);
        style.setStyle("a:hover", hover);
        style.setStyle("a:active", active);
        style.setStyle(".visited", visited);
        output_txt = new TextField();
        output_txt.backgroundColor = 0xFFFFFF;
        output_txt.background = true;
        //output_txt.embedFonts = true;
        output_txt.wordWrap = true;
        output_txt.multiline = true;

        output_txt.name = "output_txt";         
        output_txt.x = 100;
        output_txt.y = 100;
        output_txt.width = 300;
        output_txt.height = 200;

        output_txt.htmlText = "<b>sample <a href='http://www.google.com'>hyperlink text</a></b>"; 
        addChild(output_txt);
         var mySprite:Sprite = new Sprite();
         mySprite.graphics.lineStyle(.5,0x000000);
         mySprite.graphics.beginFill(0xff0000, 1);
         mySprite.alpha = .7;
         mySprite.graphics.drawRect(100, 100, 90, 20);
         mySprite.graphics.endFill();
         mySprite.useHandCursor = true;
         mySprite.mouseChildren = true;
         mySprite.buttonMode = true;
         mySprite.name = "Sprite1";
         this.addChild(mySprite);

         output_txt.styleSheet = style;
    }

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人九事 2024-08-14 14:33:29

精灵是文本字段的子元素

错误 - 这是不可能的。 TextField 不是 DisplayObjectContainer,因此不能拥有自己的任何子级。在您的代码中,文本字段和精灵是兄弟姐妹 - 两者都是文档类的子级。

子显示对象如何中断事件流,以便鼠标悬停事件永远不会到达父显示对象?

父母可以使用 mouseChildren 属性来阻止孩子获取鼠标事件,而不是其他方式。

sprite which is a child of the textfield

Wrong - it's impossible. TextField is not a DisplayObjectContainer and hence cannot have any children of its own. In your code text field and the sprite are siblings - both are children of the document class.

how can a child display object interrupt the event flow so that the mouseover event never reaches the parent?

Parents can prevent children from getting mouse events using the mouseChildren property, not the other way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文