简体   繁体   English

JavaScript迭代

[英]JavaScript iteration

I want to use JavaScript to draw a series of images onto an HTML5 canvas. 我想使用JavaScript在HTML5画布上绘制一系列图像。 I have the following while loop, which I had hoped would draw all of the images to the canvas, however, it is currently only drawing the first one: 我有以下while循环,我曾希望将所有图像绘制到画布上,但是,它目前仅绘制第一个:

function drawLevelOneElements(){
            /*First, clear the canvas */
            context.clearRect(0, 0, myGameCanvas.width, myGameCanvas.height);
            /*This line clears all of the elements that were previously drawn on the canvas. */

            /*Then redraw the game elements */
            drawGameElements();

            /*Draw the elements needed for level 1 (26/04/2012) */
            var fileName = 1;
            var imagePositionX = 20;
            var imagePositionY = 30;
            while(fileName < 11){
                /*Create an array of images here, move to next element of array on each iteration */
                var numbers = new Array();
                numbers[0] = "1.png"
                numbers[1] = "2.png"
                numbers[3] = "3.png"
                numbers[4] = "4.png"
                numbers[5] = "5.png"
                image.src = fileName+".png";
                image.src = numbers[0];
                image.onload = function(){
                    context.drawImage(image, imagePositionX, imagePositionY, 50, 50);
                }
                fileName = fileName+1;
                imageY = imageY+20;
                console.dir(fileName); /* displays in the console- helpful for debugging */
            }

To talk through what I had hoped this function would do: 讨论一下我希望此功能可以完成的工作:

  1. Load each of the images into a different element of the array (so 1.png would be in numbers[0], 2.png in numbers[1], etc. ) 将每个图像加载到数组的不同元素中(因此1.png的数字为[0],2.png的数字为[1]等)

  2. It would then take the global variable 'image', and assign its source to the contents of numbers[0] 然后,它将使用全局变量“ image”,并将其来源分配给数字[0]的内容

  3. Then draw that image at the specified position on the canvas. 然后在画布上的指定位置绘制该图像。

  4. Then increment the value of the variable fileName by 1, giving it a value of '2' 然后将变量fileName的值增加1,为其赋予值'2'

  5. Next it would increment the value of the Y co-ordinate where it will draw the image on the canvas by 20- moving the position of the image to be drawn down by 20 pixels 接下来,它将通过在画布上绘制图像的Y坐标值增加20-将要绘制图像的位置向下移动20个像素

  6. After that it would go back to the start of the loop and draw the next image (2.png) on the canvas in a position that is 20 pixels below the position of the first image that was drawn. 之后,它将返回到循环的起点,并在画布上以比绘制的第一个图像的位置低20个像素的位置绘制下一个图像(2.png)。

  7. It should continue doing this while the value of the variable 'fileName' is less than 11, ie it should draw 10 images each new one below the last one that was drawn. 当变量'fileName'的值小于11时,它应继续执行此操作,即应在绘制的最后一个图像下方绘制每个新图像10张图像。

However, for some reason, my function only draws the first image. 但是,由于某种原因,我的函数仅绘制第一张图像。 Could someone point out what I'm doing wrong, and how I could correct this? 有人可以指出我做错了什么,我该如何纠正?

Thanks very much. 非常感谢。

If you take the drawImg stuff and shove it in its own function you can clean this up a bit :) Now we've yanked the async stuff out of the loop, so the image variable doesn't get over-written each time you loop. 如果您将drawImg东西放入它自己的函数中,则可以对其进行清理:)现在,我们将异步东西从循环中删除了,因此每次循环时image变量都不会被覆盖。 You're also using a for loop now, which to me is clearer to understand. 您现在也正在使用for循环,对我来说更容易理解。

function drawLevelOneElements() {
  // your stuff
  for (var i = 0; i > 5; i++) {
    drawImg(ctx, i, x, y);
    // update x or y and whatever else
  }
}

// put all your image drawing stuff here
function drawImg(ctx, i, x, y) {
  var img = new Image();
  img.src = i + ".png";
  img.onload = function(){
    ctx.drawImage(img,  x, y, 50, 50);
  }
}

Edited and commented some points of your code. 编辑并注释了代码的一些要点。 The most effective change was at imageY = imageY+20; 最有效的更改是imageY = imageY+20; that was edited to use imagePositionY variable. 被编辑为使用imagePositionY变量。

function drawLevelOneElements() {
        /*First, clear the canvas */
        context.clearRect(0, 0, myGameCanvas.width, myGameCanvas.height);
        /*This line clears all of the elements that were previously drawn on the canvas. */

        /*Then redraw the game elements */
        drawGameElements();

        /*Draw the elements needed for level 1 (26/04/2012) */
        var fileName = 1;
        var imagePositionX = 20;
        var imagePositionY = 30;
        while(fileName < 11){
            /*Create an array of images here, move to next element of array on each iteration */
            var numbers = new Array();
            /* what is not used is not necessary :)
            numbers[0] = "1.png"
            numbers[1] = "2.png"
            numbers[3] = "3.png"
            numbers[4] = "4.png"
            numbers[5] = "5.png"*/
            image.src = fileName+".png";
            // image.src = numbers[0];
            image.onload = function(){
                context.drawImage(image, imagePositionX, imagePositionY, 50, 50);
            }
            fileName = fileName+1;
            imagePositionY = imagePositionY+20; //before: imageY = imageY+20;
            console.dir(fileName); /* displays in the console- helpful for debugging */
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM