PDA

View Full Version : if (number is getting bigger) Peform Function


ainsleyclark
07-11-2009, 11:23 AM
Hey there,
I have quite a simple question about an if statement I am battling with.
I have a variable which pretty much behaves like a rollover, Only if it is fully rolled over it returns 1 and 0 if its not. This variable also returns anything in between, so if its just about to be rolled over it will return 0.5. (The var is called size).
So ideally this is what I want.

if (//..number returns to 0 and then returns to 1 again) {
//..run code
}

Any help would be greatly appreciated.
Thankyou :)

cjx3711
07-11-2009, 12:33 PM
How the heck do you get a partial rollover?

Anyway, there are a few ways to do this.

One way is that you can have 2 booleans like is0 and is1. When it reaches 0, then make is0 true, when it reaches 1, then make is1 true. If it does anything else, reset both booleans to false.

The other way is to have 2 variables to store the previous value. If the previous value is smaller than the current value and the difference exceeds your threshold, execute your script.

Personally I think the second would be better.

If you need any help with the actual code, I would be willing to help.

ainsleyclark
07-11-2009, 01:44 PM
Hey cjx3711,
Thankyou for your suggestions. I think the second one would be better too. However I am having trouble. implementing it.
I have created a variable called rolledover. If this is at 1 it is fully rollved over at 0 and it isnt. I am having no trouble at tracing this variable it returns a message once rolled over.
However I cant seem to create a if statement that says if (rolledover returns to 0 the returns to 1 again).
Would you mind showing me how to do this?
Thankyou ever so much..

cjx3711
07-11-2009, 02:16 PM
Well first of all, you need a enter frame function that constantly saves the previous piece of data.

AS2

var prevNum;
var currNum;
onEnterFrame = function() {
if (currNum > prevNum && prevNum == 0 && currNum==1) {
//Do code
}
prevNum = currNum;
}

Hope this helps :)

ainsleyclark
07-13-2009, 03:22 PM
Thanks for the code cjx, I really appricate your help.
However im still having trouble implenting the code.
Would you be able to show me how it would work with the _ymouse?
For example if _root._ymouse is getting bigger perform code.
Im just having a little bit of trouble with the variables prevNum and currNum
Thanks for your help :)