PDA

View Full Version : pausing actionscript


boneyabba
12-01-2007, 03:06 AM
Man,

I've been reading, and reading, and reading, and not finding what I need. I think most of the questions aren't clear, and so the answers are for other things. I am sorry to ask about something that has been discussed so much- but I still don't see an answer, and am hoping I can pose the question such that the results are more useful.

I have series of 24frame movieclips. My file is set to 12fps. Based on a handful of variables, and conditional statements, the user can play some movieclips- but not others. After they tell a movieclip to play I update the variables, and based on the new data they have access to a different set of movies.

I want to prevent my variables from updating until the 24frame movieclip has completed. Or, for 2 seconds after the clip launches. I do not want to stop the movieclip.

As the actionscript executes the function from top to bottom, I want a simple entry that says, "Stop, count, keep going..." Like, pauseActionScript.2000; or something.

I am using AS3...

Thanks,
T

PJjerry
12-01-2007, 04:21 AM
http://www.quip.net/blog/2007/flash/how-to-pause-timeline-as3

does this help?

matbury
12-01-2007, 05:29 AM
Are you thinking of a timer?

var timer:Timer = new Timer(2000,1);

function startTimer():void {
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
}

function timerHandler(event:TimerEvent):void {
trace("Bingo!");
// update your variables
}

boneyabba
12-01-2007, 07:53 AM
That timer code might work- but it seems like a TON of work for what I want. The link was to something about pausing the timeline. I want to pause the execution of the actionscript and leave the timeline alone. A C++ friend says they have a thing called "Sleep" that does this.

Here is my code:

function p1ToP2(event:MouseEvent):void
{
if(_playerLocation == "P1"
&& _turns >= 1)
{
SetRouteAlpha();
route1_mc.alpha = 1;
route1_mc.gotoAndPlay(1); //24frames of movement @ 12fps
//I need code here that will delay the update of _playerLocation
_playerLocation = "P2";
_turns -= 4;
_displayTurns.text = "TURNS: " + (_turns)
}
}

I cannot believe AS3 doesn't have a readymade function that I can call in a single line, give a single parameter, and delay the execution of the rest of my AS3.

boneyabba
12-01-2007, 09:15 AM
Update:

Yeah, I can't get your suggestion to work... I am *SURE* it is my fault. I don't really know what I am doing.

PJjerry
12-01-2007, 02:59 PM
believe it or not, AS3 *doesn't* have the sleep/delay function you're looking for, so you have to implment it yourself.

I think at this point you should really practice putting some *working* code together, no matter how ugly it is... so ppl here can help you optimize it.

stompwampa
12-02-2007, 02:05 AM
Yea, there is no way to stop the execution of code for a given period of time.
I'm not sure what your set up is, but why not just put the code that you don't want to have accessed on Frame 24? This way when the user get there, they'll be able to use it, but not before.
Granted, that may not work depending how everything else is set up for your movie.

Other than that, a Timer would be a good way to do it as Matbury pointed out....it's crude, but it does get the job done ;-)

creynders
12-02-2007, 10:53 AM
Or use the setTimeout function

import flash.utils.setTimeout
myFunction( someArgument : String ){
trace( someArgument ); //outputs "whatever"
}
var waitTimeMS : Number = 2000;
setTimeout( myFunction, waitTimeMS, "whatever" );

PJjerry
12-02-2007, 03:31 PM
This looks decent!

nikefido
12-03-2007, 01:53 AM
you might be able to use ENTER_FRAME, which fires on every frame. you can make some sort of control so your functions or w/e you want to do only fires when you want to (you gotta come up with that code tho).

ENTER_FRAME is an event listener -

movieClip.addEventListener(Event.ENTER_FRAME, myEnterFrame);

function myEnterFrame(event:Event):void
{
if (/* your conditions */) {
//run my code
} else {
//don't run my code or do something else
}

}

matbury
12-03-2007, 02:30 AM
One thing I should point out - I think the problem is that you've got two functions stuck together. Why not separate them and fire them when you want?

function p1ToP2(event:MouseEvent):void {
if(_playerLocation == "P1" && _turns >= 1) {
SetRouteAlpha();
route1_mc.alpha = 1;
route1_mc.gotoAndPlay(1); //24frames of movement @ 12fps
}
}

function p1ToP2Continued():void {
//I need code here that will delay the update of _playerLocation
_playerLocation = "P2";
_turns -= 4;
_displayTurns.text = "TURNS: " + (_turns)
}
}

this way you can fire the functions in any given order at any given time. All you have to do is call them.

i.e. p1ToP2Continued(); somewhere on your timeline or on a timer or whatever.

boneyabba
12-07-2007, 01:41 PM
Seperating the functions might be a good idea... I hadn't thought of that flavor of solution- I might use it for something else. Thank you.

A lot of what I am going through right now is less about my skills with AS3 and more with my ability to see problems a certain way.

Anyway, I was able to get settimeout to do what I needed in this case. We will see if the solution scales!

Thanks,
T

matbury
12-07-2007, 08:36 PM
A good guideline is to keep things as simple as possible. A function should only really do one thing, but there are times when it's just easier to do a whole bunch of things at once, or in sequence. Anyhow, there's nothing wrong with having a bunch of functions and firing them off in sequence like this:

myFunctionOne();
myFunctionTwo();
myFunctionThree();

...er, well as long as there aren't any asyncronous events, such as loading external data or user input.