as3中tileList单元格响应鼠标事件

发布于 2024-11-06 09:08:42 字数 810 浏览 0 评论 0原文

我正在制作一个 Flash 游戏(基本上是同一个游戏的一个版本),并且我使用了一个 tileList 作为填充有 movieClip 片段的棋盘。我希望这些片段能够响应 mouseOvermouseOutmouseClick 事件。

通过查看其他问题/答案,我了解到我需要一个自定义 imageCell。这是要走的路吗?我希望通过将操作放入 movieClips 本身中来获得我想要的响应,如下所示,但这似乎不起作用。 (顺便说一句,我是这个社区的新手,但通过谷歌搜索多次来到这里……感谢你们的帮助,我已经从你们这些出色的人那里得到了帮助。干杯。)

this.addEventListener(MouseEvent.MOUSE_OVER, OverHandler);
this.addEventListener(MouseEvent.MOUSE_OUT, OutHandler);
this.addEventListener(MouseEvent.CLICK, ClickHandler);

this.stop();

function OverHandler(event:MouseEvent):void
{
event.target.play();
}

function OutHandler(event:MouseEvent):void
{
event.target.stop();
}

function ClickHandler(event:MouseEvent):void
{
    event.target.play();
}

I’m making a Flash game (which is basically a version of the same game), and I’ve used a tileList for the board populated with movieClip pieces. I would like the pieces to respond to mouseOver, mouseOut, and mouseClick events.

From looking at other question/answers, I get the idea I’ll need a custom imageCell. Is that the way to go. I was hoping I could get the responses I want by putting the actions into the movieClips themselves as below, but that doesn’t seem to work.
(btw I’m new to the community but have wound up here several times from Google searches … Thanks for help I’ve received already from you wonderful folks. Cheers.)

this.addEventListener(MouseEvent.MOUSE_OVER, OverHandler);
this.addEventListener(MouseEvent.MOUSE_OUT, OutHandler);
this.addEventListener(MouseEvent.CLICK, ClickHandler);

this.stop();

function OverHandler(event:MouseEvent):void
{
event.target.play();
}

function OutHandler(event:MouseEvent):void
{
event.target.stop();
}

function ClickHandler(event:MouseEvent):void
{
    event.target.play();
}

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

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

发布评论

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

评论(1

一人独醉 2024-11-13 09:08:42

我可能会为游戏中所有已准备好事件的部件设置一个基类。就像这样:

package
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class Piece extends MovieClip
    {
        /**
         * Constructor
         */
        public function Piece()
        {
            addEventListener(MouseEvent.CLICK, _click);
            addEventListener(MouseEvent.ROLL_OVER, _rollOver);
            addEventListener(MouseEvent.ROLL_OUT, _rollOut);
        }

        /**
         * Called on MouseEvent.CLICK
         */
        protected function _click(e:MouseEvent):void
        {
            trace(this + " was clicked");
        }

        /**
         * Called on MouseEvent.ROLL_OVER
         */
        protected function _rollOver(e:MouseEvent):void
        {
            trace(this + " was rolled over");
        }

        /**
         * Called on MouseEvent.ROLL_OUT
         */
        protected function _rollOut(e:MouseEvent):void
        {
            trace(this + " was rolled off");
        }

        /**
         * Destroys this
         */
        public function destroy():void
        {
            // listeners
            addEventListener(MouseEvent.CLICK, _click);
            addEventListener(MouseEvent.ROLL_OVER, _rollOver);
            addEventListener(MouseEvent.ROLL_OUT, _rollOut);

            // from DisplayList
            if(parent) parent.removeChild(this);
        }
    }
}

如果您需要单击、roll_over 或 roll_out 对某些部件执行任何独特的操作,只需扩展并执行此操作:

package
{
    import flash.events.MouseEvent;

    public class MyPiece extends Piece
    {
        override protected function _click(e:MouseEvent):void
        {
            trace("mypiece was clicked");
        }
    }
}

I'd probably set up a base class for all of your pieces in the game that have the events there and ready.. Like so:

package
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class Piece extends MovieClip
    {
        /**
         * Constructor
         */
        public function Piece()
        {
            addEventListener(MouseEvent.CLICK, _click);
            addEventListener(MouseEvent.ROLL_OVER, _rollOver);
            addEventListener(MouseEvent.ROLL_OUT, _rollOut);
        }

        /**
         * Called on MouseEvent.CLICK
         */
        protected function _click(e:MouseEvent):void
        {
            trace(this + " was clicked");
        }

        /**
         * Called on MouseEvent.ROLL_OVER
         */
        protected function _rollOver(e:MouseEvent):void
        {
            trace(this + " was rolled over");
        }

        /**
         * Called on MouseEvent.ROLL_OUT
         */
        protected function _rollOut(e:MouseEvent):void
        {
            trace(this + " was rolled off");
        }

        /**
         * Destroys this
         */
        public function destroy():void
        {
            // listeners
            addEventListener(MouseEvent.CLICK, _click);
            addEventListener(MouseEvent.ROLL_OVER, _rollOver);
            addEventListener(MouseEvent.ROLL_OUT, _rollOut);

            // from DisplayList
            if(parent) parent.removeChild(this);
        }
    }
}

If you need click, roll_over or roll_out to do anything unique on certain pieces, just extend and do this:

package
{
    import flash.events.MouseEvent;

    public class MyPiece extends Piece
    {
        override protected function _click(e:MouseEvent):void
        {
            trace("mypiece was clicked");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文