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: Call from the timeline to the Document Class and vice versa with Flash CS3

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

    Call from the timeline to the Document Class and vice versa with Flash CS3

    amazing Flash templates
    Greetings to all.
    What I will show you next will not be classified as good norms in Actionscript development. But, view the numbers of Flash Designers in this community, it will be certainly useful.
    I often have been asked for help on how to handle the timeline of MovieClip placed on stage or of a nested MovieClip in another one. Specially, on how to be able to refer to the Document Class from the MovieClip on stage or from the nested one inside it.
    I hope that this example will help you to understand better this communication in between the timeline and the Document Class, although you should remember that it is always best to write the codes only in the Document Class or, if really you can not manage it, to write the code on the main timeline and not all over the application.
    Let us get into it… I create a FLA and save it as ‘main.fla’.
    Into it, I create a MovieClip and place an instance on stage named ‘clip_mc’.
    This MovieClip consists of an animation of more or less 30 frames which will move it from left to right.
    I create a Document Class, an AS file saved as ‘Main.as’, implemented the following way:
    Code:
    package
    {
    	import flash.display.MovieClip;
    	
    	public class Main extends MovieClip
    	{
    		public function Main()
    		{
    			clip_mc.stop();
    		}
    		
    		public function PlayClip():void
    		{
    			clip_mc.play();
    		}
    		
    		public function aumentaY():void
    		{
    			clip_mc.y+=60;
    		}
    	}
    }
    As we can see, we have the main building function (public function Main) which stops the timeline of ‘clip_mc’ placed on stage.
    Also, we have two public methods/functions which can be called from outside the class:
    - once called, PlayClip will start the timeline of ‘clip_mc’ placed on stage
    - once called, aumentaY will increase the y property of ‘clip_mc’ placed on stage

    Let us now see how to call those two methods from the timeline:
    On the first and unique keyframe of the main timeline, I write PlayClip();.
    Previewing the SWF, I realise that ‘clip_mc’ is moving right, meaning that its timeline has been started…by who? From the method PlayClip();. Doing this way, I called a function implemented in the Document Class. This example will take away any doubts. Writing in the Document Class is the same as writing on the main timeline.
    In fact, the call PlayClip from the timeline seems to have been called from the timeline itself.

    Said that, what would happen if we needed to call a method of the Document Class from the last keyframe of the clip_mc timeline? How would we do it?
    I tested it as followed:
    First I tried to write on the last keyframe of clip_mc: trace(this.parent); and the output was [object Main]. This means that the main timeline of our main.fla is an instance of the Main class that we created. So, logically if I would write:
    this.parent.avanzaY(); Flash should call the method avanzaY implemented in the Document Class…but…it is not this way. In fact if we preview it, Flash returns an error that this.parent does not have a method called avanzaY (strange isn’t it)…
    So, I went on using the ‘universal variable’:
    var m:*=this.parent;
    m.aumentaY();

    I created a variable which can hold any type of value/data and assign to it, this.parent.
    I then call avanzaY using the variable m.

    Source files:
    Attached Files

  2. #2
    Junior Member Settled In cyberluk is on a distinguished road
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Re: Call from the timeline to the Document Class and vice versa with Flash CS3

    this.parent.avanzaY(); - that is very strange! Looks like Adobe Flash is not working right. I hate those quirks :-( ...in AS 2.0 everything works right, but when you start playing with AS 3.0 you must rewrite all the code and in different way...pretty cool closed-source

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

    Re: Call from the timeline to the Document Class and vice versa with Flash CS3

    Hi,
    all depends on what you are creating...
    If you must build a site with 4 sections and many design...you can omit the Document Class and write code in the timeline.

    Otherwise, if you must build an application that requires up to 50% of Actionscript... i would prefer to use Document Class and AS files.

  4. #4
    Junior Member Settled In cyberluk is on a distinguished road
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Re: Call from the timeline to the Document Class and vice versa with Flash CS3

    Yep, I'm using Actionscript 3.0 at more than 50%...but I have some game sprites with named keyframes and some old actionscript on it, so I have to rewrite it all. Anyway thanks for this great server ;-) I've found it today on Google and now I'm a happy registered user ;-) Now I'm working on a really nice Flash 9 game with glow & blur effects - a remake of Gridwars PC game - |g|ridwars

  5. #5
    Member Flash Addict gwulfwud is on a distinguished road
    Join Date
    Nov 2007
    Posts
    59
    Rep Power
    3

    Re: Call from the timeline to the Document Class and vice versa with Flash CS3

    now that's an addicting game!!

  6. #6
    Junior Member Settled In sci-fi is on a distinguished road
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Re: Call from the timeline to the Document Class and vice versa with Flash CS3

    Hello,

    I'm using Flash CS4, although I have written some Action Script 2 before I have always written everything in the timeline, so I'm completely new to this.

    I have followed your instructions very carefully, but when I try to compile or run anything I get this error.

    1180: Call to a possibly undefined method PlayClip.

    It's as though my .fla file is not seeing the .as file or any of the code in it. Does anyone know how I can get any code at all to compile without giving me this error?

    Many thanks

  7. #7
    CSS.FlepStudio.org Moving My Stuff To The FlepStudio Onsitus is on a distinguished road Onsitus's Avatar
    Join Date
    Jul 2007
    Posts
    1,366
    Rep Power
    4

    Re: Call from the timeline to the Document Class and vice versa with Flash CS3

    Hi,
    did you put the correct name of the document class in the property panel?

+ Reply to Thread

Similar Threads

  1. Replies: 16
    Last Post: 12-03-09, 06:56
  2. La Document Class di Flash CS3
    By Flep in forum Articoli e tutorials
    Replies: 1
    Last Post: 23-04-08, 11:21
  3. Document class
    By ariel in forum Flash Italiano
    Replies: 2
    Last Post: 16-03-08, 01:37
  4. Replies: 0
    Last Post: 09-10-07, 19:50
  5. The Document Class of Flash CS3
    By Flep in forum Tutorials
    Replies: 0
    Last Post: 23-09-07, 17:58

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