ChildButton class

Separating functionality of the child button from the parent button

Before already we saw that we could separate the Stage-mouse-event from the button. Now we have a child button. If we move its parent the child will move as well. That is how we know it from AS2. And that was it. We were not able to separate the parent mouse handler from the child mouse handler and the parent would always be dominant over the mouse. So how do we separate those two handlers? You can lready guess the answer, since we have done it for the Stage and the parent button. We use the same method "event.stopPropagation ()" for the child button and we are able to move the child button and give the button its own functionality.

package
{
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	import flash.text.TextField;
	public class ChildButton extends MovieClip
	{
	       public function ChildButton (xpos:uint, ypos:uint)
	       {
	               this.x = xpos;
	               this.y = ypos;
	               this.buttonMode = true;
	               this.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
	               this.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
	       }
	       private function mouseDownHandler(event:MouseEvent):void
	       {
	               event.stopPropagation ();THIS IS ALL YOU NEED TO SEPARATE THE BUTTON FUNCTIONS
	               trace("Child mousedown");
	               var myText:TextField = new TextField();
	               myText.text = "I am a child of myButton.";
	               myText.mouseEnabled = false;
	               myText.width = this.width;
	               myText.height = this.height;
	               this.addChild(myText);
	               this.startDrag();
	       }
	       private function mouseUpHandler(event:MouseEvent):void
	       {
	               event.stopPropagation ();
	               trace("Child mouseup");
	               this.stopDrag();
	       }
	}
}
This brings to the conclusion of this tutorial. All files can be downloaded.

previous