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

时间:2011-05-09 23:06:43

标签: flash actionscript-3 flash-cs5

我正在制作Flash游戏(基本上是同一游戏的一个版本),我使用tileList作为填充movieClip件的棋盘。我希望这些内容可以回复mouseOvermouseOutmouseClick个事件。

通过查看其他问题/答案,我明白我需要一个自定义imageCell。这是要走的路。我希望通过将操作放入movieClips本身,我可以获得我想要的响应,但这似乎不起作用。 (顺便说一下,我是社区的新手,但是已经多次从Google搜索到这里了...感谢您的帮助,我已经收到了很多人的祝贺。干杯。)

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();
}

1 个答案:

答案 0 :(得分:0)

我可能会为游戏中的所有棋子设置一个基础类,其中包含那些事件并准备就绪..就像这样:

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);
        }
    }
}

如果您需要click,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");
        }
    }
}