Cloning a MouseEvent

I’ve seen the question of how to dispatch a MouseEvent through code, but i’ve never seen it answered. I figured it out so i’ll share it with your here.

Let’s say you have a BallSprite class and a RectSprite class (BallSprite being a graphic of a ball…..etc). And i want a MouseEvent.Click on an instance of BallSprite to trigger a MouseEvent.Click on an instance of RectSprite

Give the instance of BallSprite access to the instance of RectSprite. Note how i dispatch a clone of the click on the BallSprite instance. Now any MouseEvent.Click listeners on RectSprite instance will be triggered.

package myClasses
{
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.events.MouseEvent;
    public class Ball extends Sprite
    {
       
        private var _rect:Rect;
        public function Ball(dispObj:DisplayObject)
        {
            _rect = dispObj:DisplayObject;
            this.addEventListener(MouseEvent.CLICK,clickHandler);
// add a graphic to our Ball instance
            this.graphics.beginFill(0x00ff);
            this.graphics.drawCircle(50,50,50);
            this.graphics.endFill();
        }
       
       
        private function clickHandler(e:MouseEvent):void {
            trace('ball click, now rect dispatches click');
            _rect.dispatchEvent(e.clone());
        }
    }
}
This entry was posted in code, Professional and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Rocket Number 9

    Rocket 9 is the personal and professional online presence of Flash developer and citizen Mike Connor. Cloud Swing, Inc. is our software development shop.