Copy link to clipboard
Copied
Heya! I got a simple block of code that stops the main timeline on a certain frame, but so far I've been unable to form a code that would gotoAndPlay when mouse click happens on a button I made, and mind you I'm doing this in a document class.
I've heard different instructions how to make the button, some say make it a movieclip, some say nest it in a movieclip... so if anyone could point me to a tutorial how to make this happen, or would have some insight to this, it would be most welcome!
Keep it simple for yourself and just create a movieclip and assign clicking code for it. Create the movieclip and give it an instance name on the stage... maybe call it btn_mc. Then add code for it in the timeline where the button exists...
btn_mc.addEventListener(MouseEvent.CLICK, goPlay);
function goPlay(evt:MouseEvent):void {
gotoAndPlay(#); // where # is whatever frame you intend to go to and play
}
Copy link to clipboard
Copied
Keep it simple for yourself and just create a movieclip and assign clicking code for it. Create the movieclip and give it an instance name on the stage... maybe call it btn_mc. Then add code for it in the timeline where the button exists...
btn_mc.addEventListener(MouseEvent.CLICK, goPlay);
function goPlay(evt:MouseEvent):void {
gotoAndPlay(#); // where # is whatever frame you intend to go to and play
}
Copy link to clipboard
Copied
Alright, now I keep getting TypeError: Error #1009: Cannot access a property or method of a null object reference. at toiminnallisuus /frame1
Toiminnallisuus is the name of my document class.
This is what I have in there:
package {
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.MovieClip;
public class toiminnallisuus extends MovieClip {
public function toiminnallisuus() {
addFrameScript(244, stopScript);
}
private function stopScript():void{
stop();
}
}
}
And the code you gave me is in the first frame of the actions layer on the main timeline.
The code on the document class works in itself when the actions layer has no code in it, but when I added yours there as you instructed, it started giving this error. Any ideas how to fix or go around this?
Copy link to clipboard
Copied
I believe you don't have an instance of the button on the stage at frame you inserted the suggested script. The MovieClip instance should be named "bnt_mc" as Ned suggested. If you have several keyframes in the layer which holds the clip, make sure you name it in each one of them.
Copy link to clipboard
Copied
I created a simple example for you, you can check it out here: http://dev.flashlabs.eu/examples/timeline-control/
You can also download the source code: http://dev.flashlabs.eu/examples/timeline-control/source.zip
Copy link to clipboard
Copied
Thanks!
I moved the actions keyframe around and the error message disappeared. However, the movieclip button was unresponsive still, eventhough I fixed the instance names.
Then I went and removed the entire document class and instead put a simple stop(); along with the actions keyframe and it works. It seems the Document class was somehow interfering with the actions on the main timeline.
It works now, but I would have much preferred learning the use of document classes solely, because I've gotten a lot of comments about how "that's the right way to do it" and "shoot anyone who uses actions on a timeline with AS3"
Thanks you two for all your efforts!
Copy link to clipboard
Copied
I've gotten a lot of comments about how "that's the right way to do it" and "shoot anyone who uses actions on a timeline with AS3"
No argument there But I think they referred to defining functions and whole business logic inside the timeline. The timeline is still timeline and code like "stop" is just an simple instruction telling animation to stop. I wouldn't call it programming
The addFrameScript approach is really really confusing not to mention fixed frame-number referencing. What if you expand your animation? You'll have to go to the class definition and change all the numbers in the addFrameScript function calls.
Don't worry I make a living with this since early 2005 so take a good look at example I created for you. It's as simple as it gets so you can learn the principle and extend it for your use.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now