0
\$\begingroup\$

I am making a JavaScript game called "Spoop: The Clicker", and I have a variable shown by text:

stroke("blue");
fill("purple");
textSize(30);
text("Spoop Cash: §" + spoopCash, 130, 10);

This is inside the draw loop, as it is always shown.

The thing is, I need textSize to be set shrink as my other variable spoopCash increases. How could I do that? If you can, please include a way to stop it from going too small (such as size 10 being the smallest allowed.)

\$\endgroup\$
3
\$\begingroup\$

There are many ways to do things like this based entirely on context. One example would be to have 2 values have the same values (say 10 to 20) and doing it like a scale where +5 on one would result in -5 on the other. This is probably the easiest case to handle since all you would have to do is say:

var1 = X; // if x is 12
var2 = max - (var1 - min); //var2 = 20 - 2

Another, slightly more complex but more useful case would be for you to define the min and max values of each variable and then check how far from max the one value is and then set the current value of the other one to the opposite, both cases using relative values...

Example:

var obj1_min = 10;
var obj1_max = 50;
var obj2_min = 13;
var obj2_max = 47;
var obj1_cur = 10;
var obj2_cur = 47;

function SetObj1(var to) {
    if (to < obj1_min || to > obj1_max) return;
    obj1_cur = to;
    var range1 = obj1_max - obj1_min;
    var range2 = obj2_max - obj2_min;
    var percentage = (obj1_cur - obj1_min) / range1; 
    var percentage_other = 1 - percentage;
    var value_by_range = range2 * percentage_other;
    obj2_cur = obj2_min + value_by_range;
}

As you can see, implementation varies dramatically by how you intend the values to relate to one another but this larger function should be a decent all rounder.

Just in case it's not clear what's going on is that it is checking how much each value can change (i.e. 10 to 50 means it has a range of 40) and then it checks to see how far into that range you currently are. (i.e. at 25 you are 15 into the range). Once it knows you are 15 points into the max 40 it calculates that as a % (say 30%) and then says "In that case, make the other one 100% - that amount).

Now we repeat the process to find the range of the second object and then we ask "So, what value would be 70% of that range?" and then we add that to the min value. So if the min and max for the second object were 13 and 47 that would give us a range of 34, 70% of which would be something like 25-ish so we add that to the min value of 13 and there you go... 30% into the range of 10..20 means 70% into the range of 13..47 = object2's current value

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.