|
-
Can a function contain sequential commands?
Hi,
I'm trying to set up a function that will carry out one thing and then do another.
I know you can do combined functions, but does that mean the functions will occur concurrently...? I need them to occur one after the other...
For clarity, this would be for use with an eventlistener.
Can anyone help?
Cheers
-
Actionscript is not multithreaded so your code will always run sequentially...the point where this gets confusing is in event listeners - which are functions (aka encapsulated bits of code) that you are deferring to run until an event triggers that listener.
Sorry if you already know all this but just so we're on the same page: when you hit a frame, the player will create all the functions immediately and store them in memory without running them. Then it goes through (top to bottom) and runs each line of the remaining code in order. When a function is called, all the code in the function is run before returning to where the function was called and proceeding to the next command there. This structure is the same for running classes except the 'frame' code would be whatever you've got in the constructor.
When an event handler is triggered, it doesn't interrupt any of the rest of your code for a given frame - the handler function is put off until the beginning of the next frame and run in a priority state before any other code...so for most purposes you can say that event handling happens between two frames.
For your specific case, it sounds like you want to run two functions on one event - you could do that by directly calling the second function directly at the end of the event handler, or you could use two identical event listeners targeting each function (remember that events are handled in the order the listeners are attached). Does that answer your question?
-
Thanks for that. Yes, that does make sense (I think!). You are correct that I am trying to run two functions on one event. I'm going to put this into practice and see what happens!
And thanks for the explanation in para 2. I wouldn't have been on the same page if you hadn't! Will post back when I've tried it...
Cheers
-
Just had a go at the code... I think I've done something wrong somewhere cos its only running the second function and ignoring the first. See code below:
stop();
my_button_btn.addEventListener(MouseEvent.CLICK, first_bit);
my_button_btn.addEventListener(MouseEvent.CLICK, second_bit);
function first_bit(event_object:MouseEvent) {
gotoAndPlay (2); }
function second_bit(event_object:MouseEvent) {
gotoAndPlay (55); }
Any ideas why?
Cheers!
-
That would give you two buttons - clicking the first takes you to frame 2, clicking the second takes you to frame 55...what behavior are you trying to get?
-
Damn! I'm trying to make one button do both of the functions, one after the other... So in this case, play frame 2 - 45, and then play frame 55 - 93, without having to have another button to instigate the second part...
-
I only have a few clarifications to Nez's excellent explanations.
First, certain actions are asynchronous. Mostly loading and networking. Execution will NOT wait for those to finish before going on to the next lines.
Second, there is only one button in your code, but the reason you are only seeing the second animation is that it is executed directly after the first function. So the playhead moves to 2, then immediately moves to 55, without having had time to play the animation from 2 -45.
What you could do is either call gotoAndPlay(55) on frame 45, or you could set up a timer to call gotoAndPlay(55) after enough time has passed for that first animation to complete.
-
Thanks guys.
Gonna try putting a timer on it. Thanks again.
-
This is obviously a specific example to your need here but you could go to 2 and play, and attach an event listener to detect when you reach frame 45...I'm not sure what your project structure is but there are probably much better ways to organize this - here's the workaround:
PHP Code:
// check every frame until you hit 45....
addEventListener(Event.ENTER_FRAME, function(e:Event):void{
if(currentFrame == 45){
// once you hit 45, remove this checker and jump to 55
e.target.removeEventListener(e.type, arguments.callee);
gotoAndPlay(55);
}
});
gotoAndPlay(2);
-
Been thinking about this and I'm wondering if I might put a conditional statement on frame 45.
This whole code will apply eventually to several buttons. All of them will initiate the first function (playing frames 2-45), but each button has specific instructions for the second function (i.e. the example of gotoAndPlay (55)).
Therefore I was thinking that linking the button clicked on the first frame to an "if" statement on frame 45 (the end of the first function) detailing the second function might do the job.
What do you think?
-
Yup - that's exactly the way to do it...have each button play frame 2 and also set a variable of what frame to go to afterwards, then on frame 45 just gotoAndPlay(thatVariable);
-
Nez,
Thanks for the confirmation of my thought processes!! Glad I'm on the right lines.
Will post back once I've tried it...
Cheers
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|