I'm developing a video game using JavaScript and html Canvas and I'm trying to make it so that depending on what value:
PlayerIcon = 0;
is, the computer will draw that specific number from an array:
PlayerI = [];
PlayerI[0] = new Sprite("");
PlayerI[1] = new Sprite("");
PlayerI[2] = new Sprite("");
I don't exactly understand how undefined values work (eg .this [i]) But it should be possible with them right? I already know how to do this in JavaScript however I would like to be able to make this into only a few lines of coding (no matter how many variables I add to the array):
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var timer = setInterval(gameLoop,10);
var PlayerIcon = 0;
var PlayerI = [];
function gameLoop()
{
if(PlayerIcon == 0)
{
PlayerI[0].draw(ctx);
}
if(PlayerIcon == 1)
{
PlayerI[1].draw(ctx);
}
if(PlayerIcon == 2)
{
PlayerI[2].draw(ctx);
}
}
then I will assign a picture to each PlayerI variable and switch which one is being displayed with the PlayerIcon variable.