PDA

View Full Version : gotoAndPlay



Dan Woodbury
October 25th, 2009, 09:13 PM
i have setup some frame labels so that i can pass a variable into the swf and the animation plays from that label.

i have put the gotoAndPlay code in the first frame so that it can get the variable sent in and then jump to the desired label, except it dosnt work. whatever value i put into the function it goes to the same frame. if i put in frame 10 or frame 100 it will always go to the same point.

firstly i tried this:

gotoAndPlay("teaching");

then did some research and tried this:

addEventListener(Event.ENTER_FRAME,myFunction);
function myFunction(event:Event) {
gotoAndPlay("teaching");
}

when i use the latter one it jumps to the right frame but dosnt play it just stops

any ideas to why this is happening would be great

thanks

[MuG]
October 26th, 2009, 05:57 AM
Put a trace inside the function so it reads:


addEventListener(Event.ENTER_FRAME,myFunction);
function myFunction(event:Event) {
trace("myFunction called");
gotoAndPlay("teaching");
}You should see that your function is being called more then once. The reason why this is happening is that your function is being called EVERY FRAME. Whatever you've added your listener to is on stage and as such is firing off the ENTER_FRAME event every frame.

Now if you want to carry on using the listener to fire off the gotoAndPlay then you need to remove the listener so that its not called again:


addEventListener(Event.ENTER_FRAME,myFunction);
function myFunction(event:Event) {
removeEventListener(Event.ENTER_FRAME, myFunction);
gotoAndPlay("teaching");
} The only problem you might find is that once the playhead reaches this frame again the listener will be added and the function will be called again.

It seems to be an odd solution to a problem I don't quite follow :P You're better of just using:


gotoAndPlay("teaching");