1. Uploading an image
Resources are for the players to be able to collect different resources in order to use or unlock certain items in the game. In order to create a resource we will start in the game.js file by uploading the image for our resource with the loadImage
function. This will be done in the preload
method.
preload() {
g.loadImage("trees", "tree.png");
}
2. Adding the resource
Now we need to add the resource to the room.js and game.js files. In the room.js file we need to put the setupResources
function in the onInit
method.
onInit() {
g.setupCharacters("trees");
}
Then, in the game.js file we need to put the addResources
function in the init
method.
init() {
g.addCharacters("trees");
}
And the getResources
function in the create
method.
create() {
g.getCharacters("trees");
}
3. Creating the Resources
Now you can use the createAResource
function in room.js file to create the new resource or the createResources
function to create many resources randomly across the map. To do this the createResources
or createAResource
functions will be placed anywhere in the room.js file. In this example the new resources will be created as soon as the game starts. in the onInit
method.
onInit() {
g.createResources("trees", 50);
}
This will create 50 trees randomly across your game board.
4. Using Resources
The main difference between resources and characters is you will be able to collect the resources and can use them for a store or as an item that they can place to build things. To do this you can follow the setup Item tutorial and the use Item tutorial for how to make items to go with your resources.
They can also be used in the store as resources you need to buy a certain item which can be seen in the add a Store tutorial.
In order for each player to keep track of its resources you will have to add or subtract from their resources which can be done at any time in the room.js file.
player['trees'] += 1;