Debugging @MisterAtompunk’s monster issue.
@MisterAtompunk was adding a new monster when the old monster died. What happened was an invisible monster was created that hit the player every move. I suspected the issue would be an error with the monster’s position. I added some console.log()
statements and I saw this output:
5,4,undefined
The undefined value printed there tells me there is an issue. Two variable are printed: _x
and _y
and it looks like "5,4"
is being set to the first variable, and the second is undefined
.
If you look at the makeMonster(x, y)
function you can see it accepts two arguments, one for x and one for y, whereas the old code was doing this: makeMonster(Game.alienfort)
which can never work because it’s only passing in the one argument (a string with “x,y”).
The fix is to get the x and y values separately out. So I changed his code to this (line 850) and it fixed the issue:
const pos = posFromKey(Game.alienfort);
const m = makeMonster(pos[0], pos[1]);
Always check the “arity” of your functions to make sure the arguments going in match what is expected. Using console.log()
is a useful way to see if variables hold the value you expect.