Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 7 of 7

Thread: Event.ADDED_TO_STAGE

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,460
    Rep Power
    8

    Event.ADDED_TO_STAGE

    amazing Flash templates
    Here we are again with a new trick of the day (even though I do not consider it as a trick but pure extract of OOP). This one could help us more then once when developing application with Flash CS3.
    I am talking about the event Event.ADDED_TO_STAGE.

    We need a good example to arrive to the point of this tutorial.
    Let us suppose that we have a MovieClip on stage named 'container_mc' and a second MovieClip named 'quadrato_mc'.
    We would like to attach another MovieClip placed in library (associated to a class named 'clip') inside container_mc. Up till there everything is fine as we know more then once how to do it: take a look at this article to refresh your memory if needed.

    My question to you at this point, would be:
    do we need to create the class associated to the MovieClip that we want to attach o shall we let Flash do the job for us to create it ?

    Personally, I do think that we should always create it our self.
    First of all, doing so, we do not need to write codes inside the MovieClip as we would write everything in the class keeping full control and keep away from unwanted errors.
    The second point is that we can maintain the whole hierarchical tree of our application.
    I explain myself better:
    And if we wanted to retrieve quadrato_mc placed on stage from the attached clip that is found inside container_mc'
    We know that the famous _root does not exist anymore and up till now we have used the technique to pass the value of the stage from the Document Class implementing the building function, from the class associated to the attached MovieClip, a receiving parameter inserted in a property.

    It is not easy to explain it by words so let us see a concrete example'
    Up till now, we have used this technique:

    - a FLA named 'main.fla' into which we have a MovieClip placed on stage with an instance name 'container_mc' and a second one named 'quadrato_mc'
    - a Document Class named 'Main.as'
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Main extends MovieClip
    	{
    		public function Main()
    		{
    			
    		}
    	}
    }
    - the class, named Clip.as, associated to the MovieClip, named mc_clip, placed in library
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Clip extends MovieClip
    	{
    		private var _fla:MovieClip;
    		
    		public function Clip(fla:MovieClip)
    		{
    			_fla=fla;
    		}
    	}
    }
    We attach mc_clip inside container_mc from the Document Class the following way:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Main extends MovieClip
    	{
    		public function Main()
    		{
    			attachClip();
    		}
    		
    		private function attachClip():void
    		{
    			var clip:MovieClip=new Clip(this);
    			container_mc.addChild(clip);
    		}
    	}
    }
    and if we wanted to retrieve quadrato_mc placed on stage from Clip.as:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Clip extends MovieClip
    	{
    		private var _fla:MovieClip;
    		
    		public function Clip(fla:MovieClip)
    		{
    			_fla=fla;
    			trace(_fla.quadrato_mc);
    		}
    	}
    }
    Is it possible that from Clip.as there is not a way to retrieve the stage without having to go through the Document Class'!'
    The answer is using Event.ADDED_TO_STAGE .

    Let us see how.
    The same Document Class:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Main extends MovieClip
    	{
    		public function Main()
    		{
    			
    		}
    	}
    }
    Clip.as becomes as following:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Clip extends MovieClip
    	{
    		public function Clip()
    		{
    			
    		}
    	}
    }
    If I instance Clip from Main:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Main extends MovieClip
    	{
    		public function Main()
    		{
    			attachClip();
    		}
    		
    		private function attachClip():void
    		{
    			var clip:MovieClip=new Clip();
    			container_mc.addChild(clip);
    		}
    	}
    }
    and in the building function of Clip.as I try to retrieve the parent (which would be container_mc)
    Code:
    package
    {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	
    	public class Clip extends MovieClip
    	{
    		public function Clip()
    		{
    			trace(parent);
    		}
    	}
    }
    I obtain a null!
    When I instance Clip, the building function is looking for the parent to carry out the trace. It can not find it as it has not been inserted yet into container_mc. In fact, container_mc.addChild(clip); is further down the line after I created the instance of Clip. This is why I needed to pass the value of the stage.

    Instead, if I add to Clip a listener to the event Event.ADDED_TO_STAGE, everything works out fine:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	
    	public class Clip extends MovieClip
    	{
    		public function Clip()
    		{
    			addEventListener(Event.ADDED_TO_STAGE,go);
    		}
    		
    		private function go(evt:Event):void
    		{
    			removeEventListener(Event.ADDED_TO_STAGE,go);
    			var _root:MovieClip=parent.parent as MovieClip;
    			trace(_root.quadrato_mc);
    		}
    	}
    }
    See you soon !

  2. #2
    Junior Member Settled In kasra is on a distinguished road
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Re: Event.ADDED_TO_STAGE

    Dear Flep,

    Your solution is very cool and saved me from a lot of headache, thanks.

    However, in order to make it even more professional, I'm asking you to please guide me how we can do it WITHOUT that container_mc.

    In other words, please write a project that :

    0) Uses solely movie clips that defined in library as classes (By export to action script dialog box in Library) and NOT by placing anything on stage by hand.

    1) Automatically places an Movie Clip (For example A_mc from library on stage, using addChild()

    2) And then add another movie clip inside the A_mc named B_mc and...

    3) from inside B_mc class, tries to access the stage or other objects properties.

    Now, that will be real pure OO!

    I'm working on such a thing and have lot's of problems....

    Regards
    Kasra

  3. #3
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,460
    Rep Power
    8

    Re: Event.ADDED_TO_STAGE

    Hi kasra,
    here it is:
    Attached Files

  4. #4
    Junior Member Settled In kasra is on a distinguished road
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Re: Event.ADDED_TO_STAGE

    Dear Flep,

    Thanks. It's a great piece of code. I'll try to understand it.

    Regards
    Kasra

  5. #5
    Junior Member Settled In buzcajun is on a distinguished road
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Re: Event.ADDED_TO_STAGE

    Flep,

    Thanks for the tutorial, but this doesn't exactly address my problem. I need to have the loaded SWF be able to send functions to the main stage.

    I try to add a stageEvent listener, but what is the syntax be?

    this.parent.gotoAndPlay("Label1", "Scene1")

    Thanks in advance.
    buzcajun

  6. #6
    Junior Member Settled In fauxtog is on a distinguished road
    Join Date
    Jan 2008
    Posts
    6
    Rep Power
    0

    Re: Event.ADDED_TO_STAGE

    OMG all I want to do is have my main flash movie "home.swf"
    slide in and play three instances of your image scroller at different intrevals.
    AS2 not a problem
    AS3 null errors is it because of external AS files? which aren't needed after publishing

    each image scroller swf plays fine on it's own
    I can call other external swf's no problem but if I call the image scroller swf null errors

    I have lost 2 days trying to do something I did last week in AS2 in 2 minutes
    this makes no sense why did they change AS?

  7. #7
    Junior Member Settled In Rheal is on a distinguished road
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Re: Event.ADDED_TO_STAGE

    Dear Flep,
    I can see that this little tutorial has lots of potential but am finding it difficult to follow along. (I'm a bit more of the visual learning type) I was wondering if it was possible to have a zipped file of the first example (with the instance on the stage).

    Greatly appreciated

+ Reply to Thread

Similar Threads

  1. Added_to_stage
    By Flep in forum Articoli e tutorials
    Replies: 31
    Last Post: 13 Hours Ago, 11:18
  2. event.target.name
    By angiveba in forum Actionscript 3.0 base
    Replies: 1
    Last Post: 05-11-09, 06:31
  3. Event target name
    By xPINKMERCEDESx in forum advanced Actionscript 3.0
    Replies: 1
    Last Post: 22-03-09, 16:09
  4. event
    By lmarkus in forum Flash Italiano
    Replies: 1
    Last Post: 31-10-07, 16:39
  5. mouse event in as 3
    By nootropic.kint in forum Actionscript 3.0 base
    Replies: 11
    Last Post: 03-08-07, 10:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Optimization by vBSEO