Skip to main content
Participant
June 15, 2006
Answered

Detecting the end of a movie clip

  • June 15, 2006
  • 7 replies
  • 339 views
Hi,
Objective: Display a book that the user can turn the pages to view more text. I used a tutorial from the web to create the turning page.

Process: On layer 1, frame 1 of the main time line, I put an image of a book. On layer 2, frame 1 and also frame 2 of the main time line, I put text for the book. I then created a movie clip (MC) called PageTurnMC that looks like a page in the book is being turned. On layer 3 of the main time line, I put an instance of the MC and called it PageTurn. On layer 4 of the main time line, I put a button to play the MC and advance to frame 2 called drawer1cont (see code).

//code on the button used to turn the book's page
on (release) {
PageTurn.play(); //plays my PageTurnMC
gotoAndStop("drawer1cont"); //advance to the next frame of text
}

Problem: I need to detect when the end of the MC before I advance to the next frame. I have to use the PageTurnMC many times, so it is not possible for me to put the action in the last frame of MC. As of right now, my frame 2 text appears as the PageTurnMC plays which is not what I want.

Thanks for any help.
This topic has been closed for replies.
Correct answer VtRoxy
I finally got this to work! I had to do the following.

In the first frame of the PageTurnMC, I had to define a the global variable as follows:
_global.frameID = "";

In the last frame of the PageTurnMC, I put the following code:
_root.gotoAndStop(frameID);

I then put an instance of the PageTurnMC on the main time stage with the following code:
onClipEvent (load) {
frameID = "drawer1cont";
}

Then for each instance of the PageTurnMC, I just needed to set the variable frameID to the needed frame label.

Thanks for all your help.

7 replies

VtRoxyAuthorCorrect answer
Participant
June 19, 2006
I finally got this to work! I had to do the following.

In the first frame of the PageTurnMC, I had to define a the global variable as follows:
_global.frameID = "";

In the last frame of the PageTurnMC, I put the following code:
_root.gotoAndStop(frameID);

I then put an instance of the PageTurnMC on the main time stage with the following code:
onClipEvent (load) {
frameID = "drawer1cont";
}

Then for each instance of the PageTurnMC, I just needed to set the variable frameID to the needed frame label.

Thanks for all your help.
June 16, 2006
Anytime you start a statement with " about x of the same with the only difference being...", you can bet there is a better way to do it. Try setting the name of the frame you want to go to before playing page turn.

For each button, do:
on (release) {
frameID = "correctFrame";
PageTurn.play();//in last frame, PageTurn will use frameID, which is now set to the frame we want it to go to
}

The basic idea is to let PageTurn know what frame it should go to. So if each button knows what the next frame is (after the page turn movie), it can let PageTurn know by setting this global variable. As usual, I'm leaving it up to you to make sure you reference the frameID correctly within the scope of the button event.
VtRoxyAuthor
Participant
June 16, 2006
Hi TwiceAlive100,
I got it to work!!! Your help has been great. I had to do the following:

In frame 1 of the main time line, I defined the following global variable:
_global.frameID = "drawer1cont";

In the last frame of the PageTurnMC, I put the following code:
_root.gotoAndStop(frameID);

I attached the following code to the button"
on (release) {
PageTurn.play(); // Instance name of the PageTurnMC that I added to the main time line
}

I still need to solve the problem of being able to use many instances of the PageTurnMC. Is the following the easiest way to do this (my gut tells me that what I list below is NOT the best method to use).
1. Create another global variable in frame 1 of the main time line:
_global.frameID2 = "drawer2cont"
2. Duplicate the PageTurnMC (in the library) and call it PageTurn2MC.
3. In the last frame of the PageTurn2MC put the following code:
_root.gotoAndStop(frameID2);
2. Put an instance of the PageTurn2MC on the stage in the next frame needed (frame 4 of the main time line) and name the instance PageTurn2.
3. Add a button to frame 4 with the following code:
on (release) {
PageTurn2.play();
}

I will basically end up with about 20 of the same movie clip with the only difference being the last frame containing the
_root.gotoAndStop(myVariableName);

Is there a way in the last frame of the the PageTurnMC to detect what frame from the main time the movie clip was called. Then I would only have to change the code on the button. Im thinking of something like the If...Else statements?

Again....thanks for all the help!
June 16, 2006
I just tested it and got my quick example to work. Here are a couple of possible areas to double check:

In the last frame of PageTurnMC, gotoAndStop must reference the main movie clip, e.g. _root.gotoAndStop().
Also, make sure the parameter _root.frameID isn't in quotes because it is a variable, not the actual string.

As I said, I'm not a Flash expert nor do I know the setup of your movie, but this should work as long as you have all of your references correct. If you can't get it to work, try to trace necessary variables to make sure you are accessing the appropriate variable.

The suggestion by nilu_only will also work, but it will waste some cycles because you will have to wait for the movie clip to end. Here is what I mean:

Immediately after playing PageTurn, accessing PageTurn._currentFrame will return the currentFrame only once. So if PageTurn isn't on its lastFrame, your condition statement (PageTurn_currentframe == PageTurn._totalframe) will fail and gotoAndStop will never be called.

Instead, you would need to wait for some time and continually check the _currentFrame until the condition statement is true, e.g:

on (release) {
PageTurn.play();
while (PageTurn_currentframe != PageTurn._totalframe) {} //wait until reaches the last frame
gotoAndStop ("drawer1cont")
}

This approach is likely to be suboptimal since you are wasting cycles checking the current frame instead of just having the movie let you know when it has reached the last frame. Depending on how Flash handles that while statement, you could have serious slowdown from that while statement. And since Flash doesn't have a sleep() function, you would have to do some sort of setTimeout() to slow down those checks. Either way, I would venture to say this approach is better off avoided.
VtRoxyAuthor
Participant
June 16, 2006
First, thanks for the help. I tried both suggestions, and I still can not get this to work.

Suggestion from TwiceAlive100:

On frame 1 of the main time line, I added the following:
var frameID:String;

In the last frame of the PageTurnMC, I added the following:
gotoAndStop("_root.frameID");

I attached the following code to the button located on the second frame of the main time line:
on (release) {
frameID = "drawer1cont";
PageTurn.play();
}

I even tried this code on the button:
on (release) {
_root.frameID = "drawer1cont";
PageTurn.play();
}


Suggestion nilu_only:

I put the following code on my button:
on (release) {
PageTurn.play();
ifFrameLoaded (PageTurn_currentframe == PageTurn._totalframe) {
gotoAndStop ("drawer1cont")
}
}

I even tried this on the code on the button:
on (release) {
PageTurn.play();
if (PageTurn_currentframe == PageTurn._totalframe) {
gotoAndStop ("drawer1cont")
}
}
Known Participant
June 16, 2006
you can use _totalframe to detect the end of movie

ie movie._currentframe == movie._totalframe if it happen that means end of frame
June 15, 2006
I can think of a couple of ways that you could do this, depending on how well you know actionscript and how elegant of a solution you want.

First, you could have some variable that contains the next frame you want to go to. Then in your PageTurnMC's last frame, access that var to determine which frame to goto. So:

//code in first frame:
_root.frameID:String;

//code on button
on (release) {
_root.frameID = "drawer1cont"; //set frame
PageTurn.play(); //plays my PageTurnMC
}

//code in last frame of PageTurnMC
gotoAndStop(_root.frameID);

I'm more of a programmer and less of a Flash guru, so you will want to check out/clean up the reference to the frameID var.

A more elegant (and requiring more work) would be to extend the MovieClip class and make this new class your PageTurnMC. Then use the EventListener class to dispatch an event whenever the last frame is reached. This approach is probably overkill if you only need it for one movieClip, but it may be a good idea as far as good design and programming goes if you have multiple movieClips that you need to know about. If you want more info or some example code, let me know.

Hope this helps,
twiceAlive100