开发者

tileList cells respond to mouse events in as3

开发者 https://www.devze.com 2023-03-04 07:06 出处:网络
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, mous

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


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

精彩评论

暂无评论...
验证码 换一张
取 消