Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

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

This is a discussion on Call from the timeline to the Document Class and vice versa with Flash CS3 within the Tutorials forums, part of the Flash English category; Greetings to all. What I will show you next will not be classified as good norms in Actionscript development. But, ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  3 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 23-09-07, 17:53
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Call from the timeline to the Document Class and vice versa with Flash CS3

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
File Type: zip timeline_and_DocumentClass_communication.zip (7.6 KB, 73 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 05-06-08 at 17:10..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 20-11-07, 09:38
Junior Member
 
Join Date: Nov 2007
Posts: 10
Rep Power: 0
cyberluk is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 20-11-07, 10:02
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
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.
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 20-11-07, 10:46
Junior Member
 
Join Date: Nov 2007
Posts: 10
Rep Power: 0
cyberluk is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 24-11-07, 17:46
Member
 
Join Date: Nov 2007
Posts: 59
Rep Power: 2
gwulfwud is on a distinguished road
Re: Call from the timeline to the Document Class and vice versa with Flash CS3

now that's an addicting game!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-12-08, 02:05
Junior Member
 
Join Date: Dec 2008
Posts: 1
Rep Power: 0
sci-fi is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 11-12-08, 14:15
Onsitus's Avatar
CSS.FlepStudio.org
 
Join Date: Jul 2007
Location: Nettuno Beach
Posts: 1,062
Rep Power: 3
Onsitus is on a distinguished road
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
chiamate dalla timeline verso la Document Class e viceversa con Flash CS3 Flep Articoli e tutorials 13 01-12-08 06:03
La Document Class di Flash CS3 Flep Articoli e tutorials 1 23-04-08 11:21
How to call another class from the Document Class with Flash CS3 Flep Tutorials 0 09-10-07 19:50
The Document Class of Flash CS3 Flep Tutorials 0 23-09-07 17:58
document class mariano.martucci Actionscript 3.0 avanzato 5 24-07-07 14:24


All times are GMT. The time now is 19:13.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap