r3dux.org

A number-pimping side project from the valleys in *NEW* upside-down flavour.

  • Home
  • ABOUT
  • OLD SITE
  • SEARCH
  • FEEDBACK

ActionScript 3.0: Event Propagation

r3dux | March 29, 2010

I learnt AS3 in a hurry – I had a couple of weeks to get my head around it and get things working – no nicities – just get it working so you can teach it… And what I missed by scrabbling through the ActionScript 3.0 Bible was the propagating nature of the event model. And it’s awesome! You layer things on other things and the event propagates down the tree until it hits the last branch..

In the example below I just create three boxes bound to a Box class, and when adding them to the screen I don’t add them all to the stage – I just add the first one to the stage, add the second one to the first one, and add the last one to the middle one, and just look at MOUSE_OVER and MOUSE_OUT events. Because they’re MovieClips added to MovieClips etc, the event travels down the tree and hits each branch using the current status to work on. Move your mouse over the boxes below to see what I mean:

That’s going to come in really useful… I can just feel it.

Source Code:

Box class:

package
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
 
	public class Box extends MovieClip
	{
 
		public function Box():void
		{
			this.addEventListener(MouseEvent.MOUSE_OVER, isOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, isOut);
		}
 
		private function isOver(evt:MouseEvent):void
		{
			this.alpha = 0.5;
		}
 
		private function isOut(evt:MouseEvent):void
		{
			this.alpha = 1;
		}
	}
}
var topBox:Box = new Box;
topBox.x = 150;
topBox.y = 150;
addChild(topBox);
 
var middleBox:Box = new Box;
middleBox.x = 100; // Remember this is -RELATIVE- to the MovieClip this MovieClip is being embedded in....
middleBox.y = 100;
topBox.addChild(middleBox);
 
var bottomBox:Box = new Box;
bottomBox.x = 100;
bottomBox.y = 100;
middleBox.addChild(bottomBox);

Related posts:

  1. ActionScript 3.0: A Dynamic Frame Rate Switching Class to Lower CPU Usage
  2. ActionScript 3.0 Particle Systems #2: Snow Effect
  3. ActionScript 3.0 Particle Systems #1: Random Motion
  4. ActionScript 3.0 Particle Systems #6: Particle Attraction
  5. ActionScript 3.0 Particle Systems #4: Bubbles
Categories
Coding
Tags
ActionScript 3.0, Event, Propagation
Comments rss
Comments rss
Trackback
Trackback
Print This Post Print This Post

« The Time Controlling Properties of SimCity Wii Preloader April Fools Error »

2 Responses to “ActionScript 3.0: Event Propagation”

  1. foxvor says:
    November 3, 2011 at 10:15 am

    I think is very usefull, and you will probably hit me for asking this, but: How do I prevent event propagation?

    I have a parent MC with a ‘MouseOver’ event listener, and I don’t want that on the children MC.

    Thanks.

    Reply
    • r3dux says:
      November 3, 2011 at 5:18 pm

      To find out how to stop events propagating in ActionScript 3, try this.

      The very first link should do the job just fine ;)

      Reply

Leave a Reply

Click here to cancel reply.

Translate

Categories

Archives

Tags

3D ActionScript ActionScript 3.0 Adobe AI Ballarat Bash C++ Class Convert CS4 Effect Error Film Flash GLSL Gnome Hack How-To install Jaunty Java Kinect Linkage Linux Mash-Up Microsoft Motion OpenGL Particle Problem PS3 Remix Retro script Slides Sound Systems Texture Ubuntu Video VirtualBox Wii Windows XBox

Gamercard

OpenR3dux

Misc.

Flattr this

RSS Feed

r3dux twitter feed



“Those who live by the sword get shot by those who don’t.”

rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox