Showing posts with label MainTimeline. Show all posts
Showing posts with label MainTimeline. Show all posts

Saturday, July 6, 2013

Difference in accessing MovieClips on TimeLine and in ActionScript




I am always curious about how things happen. In flash I had a confusion as there is difference in accessing MovieClips on Timeline and accessing MovieClips in ActionScript. So somewhere on web I found this answer. I’ve just copy and pasted it here. Thanks to person “x” who explained this.


In Flash 9 when you create MovieClip instance on the Timeline and give it an instance name, you are doing two things:
  1. You're assigning the name property of the MovieClip instance to be the string equivalent to the instance name provided
  2. You're creating a variable in the current timeline with the name of the instance name that references that MovieClip instance

Flash does this behind the scenes when you publish your SWF to help you manage your movie clips on the screen. It's important to note that this behavior (specifically #2) is not seen with ActionScript. For Example:
Code:
// my_mc is the instance name of a movie clip
// created on this timeline
trace(my_mc); // [object MovieClip]
trace(my_mc.name); // my_mc

// create a new movie clip via AS
// add it to my_mc
var another_mc:MovieClip = new MovieClip();
another_mc.name = "child_mc";
my_mc.addChild(another_mc);

// instance name not available in parent timeline
trace(another_mc);  // [object MovieClip]
trace(my_mc.child_mc); // undefined
trace(my_mc.another_mc); // undefined
You can see that neither the instance name (name property) nor the variable to which the new MovieClip created with ActionScript was assigned is referencable through the movie clip in which it was added (my_mc). This is because another_mc was created and added to the timeline dynamically. Flash will only save instance names as variables for movie clips created on the timeline in Flash.

If you want to use an instance name to get a MovieClip (or any DisplayObject) instance from the timeline in which it exists, you can use getChildByName();
Code:
trace(my_mc.getChildByName("child_mc"));  // [object MovieClip]);
This will work for all movie clips despite where or how they were located as long as they are within the timeline/movie clip from which getChildByName was used.


Enjoy ActionScripting.................

Saturday, November 10, 2012

The MainTimeline and The Document class

 
In flash, Document Class represents the MainTimeline.
It means, the Variables or MovieClips or Functions defined on Timeline are also accessible in Document Class or vice versa.
And it also simply means, you cannot re-declare the Variables or MovieClips or Functions in Document Class, once you declare the same on Timeline. If you do so, you will get the error "1151: A conflict exists with definition [name] in namespace internal."

Let’s see how Timeline works:
The initialization of a SWF starts with the Document Class constructor function. The code written in constructor function of Document Class gets executed first, after that code written on Timeline frame1 will get executed.
If you don’t use Document Class to write code then flash internally create the one at the time of compiling swf and code written in each frame will get converted to frame number function in Document Class. Ex. If you write code on frame number 1, flash will create the function named frame0 and put all code within this function. This is the flow of execution for all frames and the code on particular function get executed when play head reaches to that frame. Remember one thing here, though Timeline starts with frame1, flash considers this as index 0. so frame1 code will be declared in function named "frame0" and so on.

The Variables and Functions declared on Timeline are accessible throughout the fla and Document Class, as they are considered as internal access specifires. MovieClips, Buttons declared on Timeline have limited scope in particular function, as they exists till the Playhead is present on that frame on timeline. Document Class is always considered and declared as public to access the things in all project.

If you are not using Document Class then also this execution is same for Document Class created by flash at compile time and in constructor function of it using addFrameScript method, which is undocumented feature of flash.

Since frame0, frame1 etc. are the function names that flash uses internally, we cannot use the same named functions. In such situation flash gives error: "1023: Incompatible override."/"1021: Duplicate function definition."
However you can use the same function name except in the same frame. ie. function frame10() will give above arror if it is written on frame10, but you can write the function frame10() on any other frames.
For Document Class you have to extends class as MovieClip or Sprite. but if you have a code on timeline you have to extend the class as MovieClip, extending sprite will give you error as Sprite does not have timeline. 
For more details, create your own swf file having code on timeline and decompile it using Sothink SWF decompiler. In ‘actions’ folder, in ‘resources’ panel you will get the flash compiled code.

Also to clear the difference between MainTimeline, Root and Stage check the following outputs on Timeline and Document Class sepretaly.

trace("this " + this);
trace("root " + root);
trace("root.parent " + root.parent);
trace("stage " + stage);
trace("parent " + parent);

Sunday, April 25, 2010

Access of MainTimeline to class file

Here is code for How to access MainTimeline objects in fla to .as class file.

In main.fla,
There are two text boxes SentText and gettext.
code is:

import com.sample

var sample:Sample=new Sample(this)
sample.SentText("Sent to Class File")
gettext.text=sample.GetText();

Here this keyword  represents MainTimeline, to class sample , when instance of that class is created.

in Sample.as

package com
{
public class Sample
{
private var SuperObject:Object;
public function Sample(mySuperObj:Object)
{
SuperObject=mySuperObj;
}
public function SentText(str:String)
{
SuperObject.Mytext.text=str
}
public function GetText():String
{
return SuperObject.Mytext.text;
}
}
}


Here SuperObject is a variable of type Object , which represent MainTimeLine.
When object of class Sample is created, MainTimeline is passed to Sample Class constructor.
If we trace (SuperObject) it shows [object MainTimeline].
If you write this code in document Class, this document Class represent MainTimeLine
and now we can access of objects of MainTimeLine as seen.

Thanx;
Enjoy Actionscripting..........