FLASH AS3: ROOT DETECTOR

PathCorrectorAS3

Path correction-detection component

Recently, a frequently asked question is how within a MovieClip one can detect the main timeline or stage or how from a movie one can detect the timeline of a parent movie. This component makes it possible. It can be placed in a MovieClip of a MovieClip and will still detect objects. There are several parameters:

There is an eventhandler, which will also give the object, which is entered in the component timeLine parameter. Give the component a name like "pc". Then enter this script, in the same frame where the component is located.



				pc.addEventListener("pathFinder",pf); 
				function pf(e:Event):void 
				{ 
					trace("Path: "+e.currentTarget.getPath()); 
				}
		
		

Use "e.currentTarget.getPath()" as the object to refer to.

Example 1

Create a new movie and save it as RootFinder1.fla. Craete frames on the main timeline using a tween for example and name the second frame "frame2". Place a stop() in the first frame. Create a MovieClip and save it as "Clip1". Create a second MovieClip and save it as "Clip2". Place an instance of Clip2 into Clip1. Then place an instance of the PathCorrectorAS3 component into Clip2. Click on the component and open the component inspector. Fill out the table as follows:

Now place an instance of Clip1 on the main timeline. Test the movie and you will see that the tween will be played.

Example 2

Create a new movie and save it as PathParent.fla. Create a shape and tween it. Place a stop in frame 1 and the last frame of the tween. In frame 2 add a frame name "frame2". Add a Document class to the movie and call it "LoadCheck.as". The script in LoadCheck.as looks like this.



			package 
			{
				import flash.display.*;
				import flash.events.*;
				import flash.net.*;
				public class LoadCheck extends MovieClip
				{
					private var mc1:Sprite;
					public function LoadCheck ():void
					{
						mc1=new Clip();
						mc1.name="mc1";
						addChild (mc1);
						var myLoader:Loader = new Loader();
						addChild(myLoader);
						var url:String = "Pathchild.swf";
						myLoader.load(new URLRequest(url));
						myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,cHandler);
					}
					private function cHandler(e:Event):void
					{
						trace("loaded");
					}
				}
			}
		
		

Now create a movie and save it as Pathchild.fla. Place a MovieClip on the timeline and name it "mc". Place the PathCorrectorAS3 component on the timeline, name it "pc" and fill out the component menu.

Now add the following script in the first frame of Pathchild.fla.



			var pathObject:Object;
			pc.addEventListener("pathFinder",pf);
			function pf(e:Event):void
			{
				pathObject = e.currentTarget.getPath();
				trace(pathObject);
			}
			mc.addEventListener(MouseEvent.CLICK,ch);
			function ch(e:MouseEvent):void
			{
				trace("Path: "+pathObject);
				pathObject.gotoAndPlay("frame2");
			}
		
		

Now test both movies. When the child movie is loaded press the mc MovieClip and the parent movie will play. Play around with this example and instaed of "root" add the name for the Sprite of the parent "mc1".