AS3 main class – interaction between methods and timeline (for beginners)
After you have setup main class for your flash project, and perhaps put it in to a package, you can start writing code for your application.
Calling timeline from main class
On main class you can access timeline straight from the code. Write stop(); to your main class and it will stop the playback of timeline.
package org.randomaccess{
import flash.display.MovieClip;
public class MyApplication extends MovieClip{
function MyApplication(){
/*
On main class you can access timeline straight from the code.
stop(); refers to the maintimeline and stops it.
*/
stop();
}
}
}
Calling main classes methods from the timeline
Visa versa you can also refer straight to the main classes methods from the timeline. Add to your main class new method aka function:
function myGotoAndStop(frame){
// This method is called on main timeline.
// As this is the main class, you can call the main timeline directly.
gotoAndStop(frame);
}
On first frame of your main timeline put command:
myGotoAndStop(10);
- Now the main class first stops the timeline on the first frame.
- On the first frame timeline calls the main classes function myGotoAndStop()
- myGotoAndStop() makes the timeline to go to the frame 10
Calling main classes methods from timeline is quite straightforward.
However, usually there shouldn’t be any reason to put any code to the timeline. It’s always better to write your code on methods and classes to keep them in nice, reusable order.