- Home /
Have readout of score at certain increments (JavaScript)
I am making a game where the score is incremented gradually and not incrementally. The score goes up depending on how long you live, so it is constantly going up the whole time. The new score gets refreshed every second and when I print the score to the console I get numbers such as, 0.05037879, 0.07613637, 0.1018939, etc.
I have recorded voices saying a single number, and by combining multiple sound files, i can get a readout of the number I want. I want when the score reaches 0.2, for it to read out "0.2". I then when it reaches 0.5, it should readout "0.5". Then when it reaches 1.0 it read out "1.0". From then on it should loop and readout the correct number at every increment of 0.5 (1.5,2.0,2.5 etc). Technically this should go on forever, but no one will reach to a score of 10, so the last number will be 9.5. (so all of the read out numbers are 0.2, 0.5, 1.0, 1.5, 2.0, 2.5 .......... 8.5, 9.0, 9.5) what I have, as far as I can tell, should work. Here is what it is,
............................................................................................
var zero: AudioClip;
var one: AudioClip;
var two: AudioClip;
var three: AudioClip;
var four: AudioClip;
var five: AudioClip;
var six: AudioClip;
var seven: AudioClip;
var eight: AudioClip;
var nine: AudioClip;
var point: AudioClip;
var miles: AudioClip;
var youTraveled: AudioClip;
var score1: int;
var firstNum: int;
var secondNum: int;
var thirdNum: int;
var pointTwo = true;
var pointFive = true;
var pointZero = false;
function Update(){
score1 = Mathf.Round(Score.distanceScore * 100);
firstNum = score1/100;
secondNum = (score1 - firstNum)/10;
thirdNum = score1 - firstNum - secondNum;
if(firstNum == 0 && secondNum >= 2 && pointTwo == true)
{
pointTwo = false;
Debug.Log("play 0.2");
sound(zero,two);
}
else if(secondNum == 5 && pointFive == true)
{
pointFive = false;
if (firstNum == 0){
sound(zero,five);
}
else if (firstNum == 1){
sound(one,five);
}
else if (firstNum == 2){
sound(two,five);
}
else if (firstNum == 3){
sound(three,five);
}
else if (firstNum == 4){
sound(four,five);
}
else if (firstNum == 5){
sound(five,five);
}
else if (firstNum == 6){
sound(six,five);
}
else if (firstNum == 7){
sound(seven,five);
}
else if (firstNum == 8){
sound(eight,five);
}
else if (firstNum == 9){
sound(nine,five);
}
pointZero = true;
}
else if(pointZero == true && secondNum == 0)
{
pointZero = false;
if (firstNum == 0){
sound(zero,zero);
}
else if (firstNum == 1){
sound(one,zero);
}
else if (firstNum == 2){
sound(two,zero);
}
else if (firstNum == 3){
sound(three,zero);
}
else if (firstNum == 4){
sound(four,zero);
}
else if (firstNum == 5){
sound(five,zero);
}
else if (firstNum == 6){
sound(six,zero);
}
else if (firstNum == 7){
sound(seven,zero);
}
else if (firstNum == 8){
sound(eight,zero);
}
else if (firstNum == 9){
sound(nine,zero);
}
pointFive = true;
}
}
function sound(x,y){
audio.clip = x;
audio.Play();
yield WaitForSeconds(.7);
audio.clip = point;
audio.Play();
yield WaitForSeconds(.7);
audio.clip = y;
audio.Play();
yield WaitForSeconds(.7);
audio.clip = miles;
audio.Play();
}
...........................................................................................
And in the script "Score", the score is found with
................................................................................
function score() {
distanceScore += (Variables.actualFps + 88) / 5280;
}
InvokeRepeating("score",1,1);
......................................................................................
This code works up to when it needs to say "1.0". So it correctly says 0.2 and 0.5.
You can round the numbers using float. Also, use an array of audioclips, not several variables: var Sounds : audioclip[];
I am a C# coder, bit that should work.
Answer by BillK88 · May 12, 2014 at 02:46 PM
Yes, you definitely should use an array for sound clips.
You could then refer to its elements by index.
Assuming you have audio clips in Sounds array (Sounds = [zero,one,two,...]), you could convert this large part of code to something smaller:
if(firstNum == 0 && secondNum >= 2 && pointTwo == true)
{
pointTwo = false;
Debug.Log("play 0.2");
sound(zero,two);
}
else if(secondNum == 5 && pointFive == true)
{
pointFive = false;
sound(Sounds[firstNum],five);
pointZero = true;
}
else if(pointZero == true && secondNum == 0)
{
pointZero = false;
sound(Sounds[firstNum], zero);
pointFive = true;
}
And it could be compacted a little bit more I suppose.
Best Regards
Frontend programmer for http://www.pneumatig.eu