I am trying to make a point and click adventure video game on HTML Canvas for G4C. I have an array full of images. I want to convert it into a single parameter so that I can do something like image[1] and when that happens, the selected image array element will appear as the background on the canvas. The parameter is inside a function call.
So how do I convert my array into a single parameter, more specifically the image parameter, when calling a particular function that has multiple parameters in it?
The code must be in Vanilla JavaScript.
CSS
#gameCanvas {
width: 960px;
height: 640px;
border: 5px solid rgba(52,52,52,1.00);
background-color: rgba(241,213,179,1.00);
}
Vanilla JavaScript
/**Array of Images**/
var imgArray = [
"gameImages/titleScreen.png",
"gameImages/titleScreen1.png",
"gameImages/titleScreenBW.png",
"gameImages/startScreen.png",
"gameImages/startScreenBW.png",
"gameImages/forestTrail.png",
"gameImages/forestTrailBW.png",
"gameImages/jewleryOnLog.png",
"gameImages/jewleryOnLogBW.png",
"gameImages/sunlitWaterFall.png",
"gameImages/sunlitWaterFalBW.png",
"gameImages/sunlitWaterFall.png",
"gameImages/sunsetStarySky.png",
"gameImages/sunsetStarySkyBW.pn",
"gameImages/campSunset.png",
"gameImages/campSunsetBW.png",
"gameImages/LoveHeart.png",
"gameImages/LoveHeartBW.png",
"gameImages/nightSky2BW.png",
"gameImages/nightSky2.png",
"gameImages/nightSkyAnimate.gif"
];
function drawImageProp(ctx, image, x, y, w, h, offsetX, offsetY) {
//Function Code for resizing images to fit canvas proportions...
ctx.drawImage(image, cx, cy, cw, ch, x, y, w, h);
}
drawImageProp(ctx, image, 0, 0, width, height); //I want to change image to get the array image element
/**Right now, the image parameter on Adobe Dreamweaver displays the error "'image' is not defined. [no-undef]"
**/
I tried drawImageProp.apply(null, imgArray); and other apply methods although I am unable to find use for that since I am not making all array elements into parameters.
I tried a for (){} loop with:
for (var i = 0; i < imgArray.length; i++) {
imgArray.element = image;
}
but I am unable to determine if it works due to a lack of knowledge.
If there is an improvement for this code or better ways to insert images as backgrounds into HTML Canvas to use as a part of drawing or displaying scenes/events in a point and click adventure video game, please give a well explained answer. It would be much appreciated.
var image = new Image();? Also, do you have any references or sources for your critique?