As a long time JavaScript hobbyist I have made my fair share of games. I am sure that game development is a major reason why a lot of people get into javaScript to begin with, and as such I am no exception. One thing that comes up often in game development is dealing with some kind of in game currency.
As of late I have been making some new games that involve game money, so I thought I would put together a quick something to touch on the ways I go about handling this.
Just incrementing
In some games I will have a situation in which you start with a certain amount of money, and money will just simply be added to that sum on each frame tick, or by way of some kind of condition.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// the money
var money = 0.75,
income = 0.25,
// something to by
cansOfBeans = {
cost : 0.65,
count : 0
},
buybeans = function () {
// yep just subtract
if (money >= cansOfBeans.cost) {
money -= cansOfBeans.cost
cansOfBeans.count += 1;
}
},
// what to do on each tick
tick = function () {
// yep just add
money += income;
render();
},
// a basic view, and interface would be nice, yes?
button.value = 'buy beans for $' + cansOfBeans.cost;
button.addEventListener('click', buybeans);
document.body.appendChild(disp);
document.body.appendChild(button);
render();
setInterval(tick, 1000);
};
start();
Working with game currency this way may be crude, but it is often simple, and effective. Sometimes it is called for to just get the job done, and not put to much thought into things.
Setting money by way of an expression
Another approach I have been using for game money, is to always set money to it’s current balance by way of an expression. I like this because I can play around with the values in the expression to quickly jump around in time, and with different expenses.