PDA

View Full Version : Simple mathematical troubles...



jaelle
January 6th, 2004, 04:38 PM
I'm a bit rusty with my Flash skillz from not having touched Flash in quite a few months. So I have a very simple question.

I have a dynamic textbox represented by the variable "sidereal" with an initial value of 00.00, and I want it to continually increment by 0.38.

The code I have right now is:



onClipEvent(enterFrame) {
_root.sidereal=sidereal+0.38;
}


But the output that gives me is "0.38" and it then doesn't increment after that point. Also I would like it to say "00.38" if at all possible.

Thanks in advance for any help.. Sorry to trouble you with such a nitpicky detail, but I seem to be having a braindead moment today. :(

Laslett
January 6th, 2004, 04:42 PM
you can use _root.sidereal+=0.38 then play with your var value using and convert it to a string

jaelle
January 6th, 2004, 04:44 PM
Hmmm... When I try that, I end up getting output that looks like "00.000.38" o_O Like its concatenating the two, rather than adding them mathematically.

claudio
January 6th, 2004, 04:59 PM
Something like this?
onClipEvent(enterFrame) {
_root.sidereal = "0"+(Number(_root.sidereal)+0.38).toString();
}

jaelle
January 6th, 2004, 05:01 PM
:o! Yes! Almost exactly like that.. Thank you so much. :) That's a bit more complicated then I recall it being... Now I just need to fix it so the "0" dissapears after it gets to 2 digit #'s... But hopefully I can manage that. :) Thanks again.

claudio
January 6th, 2004, 05:10 PM
Okay, scream if you have trouble with that ;)

Voetsjoeba
January 6th, 2004, 05:12 PM
When you use



onClipEvent(enterFrame) {
_root.sidereal=sidereal+0.38;
}


You are setting the value of _root.sidereal equal to 'sidereal+0.38'. However, sidereal is not the same sidereal as _root.sidereal. _root.sidereal is located on the main timeline of the movie, where sidereal is a local variable of the symbol this code is placed on. Since this variable doesn't exist, Flash can't use it. The variable is undefined, so sidereal+0.38 always equals 0.38, because sidereal doesn't exist.

You must target the sidereal of the main timeline, _root.sidereal, and add .38 to that value, not to the local, non-exisiting sidereal:



onClipEvent(enterFrame){
_root.sidereal = _root.sidereal+0.38
}


or this, which is the same:



onClipEvent(enterFrame){
_root.sidereal += 0.38
}

Voetsjoeba
January 6th, 2004, 05:14 PM
Claudio, always fast as lightning ;)

Here's how to get rid of the 0 before 2 digit numbers:


onEnterFrame = function () {
_root.sidereal < 10 ? _root.sidereal = "0"+(Number(_root.sidereal)+0.38).toString() : _root.sidereal = (Number(_root.sidereal)+0.38).toString()
};

:)

jaelle
January 6th, 2004, 05:22 PM
Originally posted by Voetsjoeba
Claudio, always fast as lightning ;)

Here's how to get rid of the 0 before 2 digit numbers:


onEnterFrame = function () {
_root.sidereal < 10 ? _root.sidereal = "0"+(Number(_root.sidereal)+0.38).toString() : _root.sidereal = (Number(_root.sidereal)+0.38).toString()
};

:)

I take it that that is a fancy way of doing the equivilent of if-then statements? *makes note* :P I got it working mostly, thanks to all your most appreciated help. :D Only to realize my math was slighty flawed to begin with.. but meh. I will get it eventually. :) We hope...

claudio
January 6th, 2004, 05:24 PM
Actualy it should be:
Number(_root.sidereal)+0.38<10 ? _root.sidereal="0"+(Number(_root.sidereal)+0.38).toString() : _root.sidereal=(Number(_root.sidereal)+0.38).toStr ing();
:beam:

Voetsjoeba
January 6th, 2004, 05:29 PM
Originally posted by claudio
Actualy it should be:
Number(_root.sidereal)+0.38<10 ? _root.sidereal="0"+(Number(_root.sidereal)+0.38).toString() : _root.sidereal=(Number(_root.sidereal)+0.38).toStr ing();
:beam:

Hey, I had to do something to be faster than you ;)



I take it that that is a fancy way of doing the equivilent of if-then statements? *makes note*


Yep :) I just thought of it that this might not work in F5 ... it works like this:

condition ? actions if true : actions if false;

it's the same as:

if (condition){
actions if true
} else {
actions if false
}