1. Uploading an image
First, we need to upload a new image to use for the new character set. To do this we will go into the preload
method in game.js and add a new image named after the new character set.
preload() {
g.loadImage("zombies", "zombie.png");
}
2. Adding the Character
Now we need to add the character to the room.js and game.js files. In the room.js file we need to put the setupCharacters
function in the onInit
method.
onInit() {
g.setupCharacters("zombies");
}
Then, in the game.js file we need to put the addCharacters
function in the init
method. If you want to change the size of your character you can do this in the addCharacters
function by adding another parameter. ie. addCharacters('players', 0.5)
, the lower the number the smaller your character will be: 0.5 would be half size, 2 would be twice as big.
init() {
g.addCharacters("zombies");
}
And the getCharacters
function in the create
method.
create() {
g.getCharacters("zombies");
}
3. Creating the Character
Now you can use the createACharacter
function in room.js file to create the new character wherever it is needed. In this example the new character will be created near the top left of the screen and will be created as soon as the game starts. To do this the createACharacter
will be placed in the room.js file in the onInit
method.
onInit() {
g.createACharacter("zombies", g.nextCharacterId("zombies"), {x: 20, y: 20});
}
The X and Y values can be changed to position the character anywhere in the game.