- Home /
Breaking a loop, Javascript?
Hello, Im wanting to call a particle animation only once, it is in the function update. Would I do something like this:
function Update () {
if (gear = 1) {
SomeParticleAnimation.Play();
Break;
}
if (gear = 2) {
SomeParticleAnimation.Play();
Break;
}
//ect...
}
I want to play an particle animation every time the gear changes.
Thanks Daniel
I take the time to accept and rate good answers!
Answer by SolidSnake · Jul 29, 2013 at 05:17 PM
As @Lovrenc said... Update() will keep on calling... Better to have a seperate function that controls the anims and which is called from update and which you can control how it is executed
You can do something like this:
var lastGear:int;// stores the value of last gear
var gear:int; // current gear
function Update()
{
GearAnimation();
}
function GearAnimation()
{
if(lastGear == gear) // did the gear change? no then return to exit
return;
lastGear = gear;
// then use switch or if statments to check the value (switch will suit this more)
switch(lastGear)
{
case 1:
SomeParticleAnimation.Play();
break;
case 2:
// etc....
}
}
==EDIT==
What @Eric5h5 is saying is that instead of checking the gear value every frame (i.e. using the Update()) to play an animation once you should play the animation when the gear changes which what you will need to do eventually anyway.
As an example:
// You will call this function only when the gear value need to change
function ChangeGear(gearValue: int)
{
gear = gearValue;
// play the corresponding animation for the current value ( using switch/ if statements)
switch(gear)
{
case 1:
SomeParticleAnimation.Play();
break;
case 2:
// etc....
}
// do other things if any...etc
}
So just to make sure break would cause the animation to ONLY be called once right? (@SolidSnake)
The break itself only prevents switch statement to fall into another "condition". If there was no break in:
case 1:
SomeParticleAnimation.Play();
break;
then (in case lastGear was 1) both statements under case 1: and under case 2: would execute.
But yes, as long as you will handle your lastGear variable correctly, your animation will only shoot once.
Unless you plan on calling that code from other functions, putting it in a separate function is just adding function call overhead while not changing anything.
@Eric5h5 I based my answer on his question in regard to breaking an update and not on how the gear is changing which is separate question. Also, Having a separate function even though it will add some overhead it will make the code readable.
@$$anonymous$$ G as @Lovrenc explained handling lastGear variable correctly will prevent the code from executing if gear didn't change value. But @Eric5h5 recommendation is valid if you don't need to check the gear value every frame. I simply based my answer thinking that gear is a variable you needed to check every frame since you used update function.
Answer by Lovrenc · Jul 29, 2013 at 03:50 PM
Update is a function that is called within the loop and is not a loop itself.
You can exit the function by calling
return;
But this seems like a bad practice and would probably produce error prone code. Just use switch on your
gear
variable and do what is apropriate. This was your code is encapsulated and you can do other things that you may need in that function.
Switch is essentially same as if statements but is easier to read than a huge blob of ifs.
switch(gear) {
case 1:
//Do your stuff
break;
case 2:
//Do your stuff
break
}
Answer by Eric5h5 · Jul 29, 2013 at 08:53 PM
Don't use Update for this; it's for things that happen every single frame. Instead just run code when you actually need it. It depends on where you're actually changing the "gear" variable, but wherever that is, that's where you put the code.
function SomeFunctionInWhichYouChangeGear () {
if (gear == 1) {
SomeParticleAnimation.Play();
}
...
}
Hmm, Im thinking, Seems like i could just read what the gear number is and execute the animation if that gear equals a specific number like you show, but I'm STILL lost as to WHERE i would put this "SomeFunctionInWhichYouChangeGear ();" In the update? Can you be specific :D @Eric5h5 If its in the update, would this cause it to NOT execute whats in the if statement (animation) here ever frame?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to access variable from another function? 2 Answers
How to make a selection system? 1 Answer
How to spawn objects in a specific range of random location 1 Answer