简体   繁体   English

html5 canvas游戏中的更新功能无法正常工作

[英]Update function in html5 canvas game isn't working properly

Jsfiddle : http://jsfiddle.net/seekpunk/6eeKH/15/ Jsfiddle: http : //jsfiddle.net/seekpunk/6eeKH/15/

the code : 编码 :

if (this.collection.length > 0) {
                            if (((this.collection[0].blockX + this.collection[0].blockW) >= cw) || (this.collection[0].blockX <= 0)) {
                                this.collection[0].blockSpeed *= -1;
                            }
                            this.collection[0].blockX += this.collection[0].blockSpeed;

                        }

the first block from top is drawn twin and i can't figure out why i guess the problem is in drawing the levels can someone please help me figure out what i did wrong in my code 从顶部开始的第一个图块是双胞胎绘制的,我不知道为什么我猜问题出在绘制关卡时有人可以帮助我找出我在代码中做错了什么

Now I think you're clearing the platforms from the collection array before any coordinates get chance to be applied and redrawn to the canvas. 现在,我认为您是在从集合数组中清除平台之前,没有任何机会应用坐标并将其重新绘制到画布上。 If you remove the call to 'Blocks.clear()', the platform moves. 如果删除对“ Blocks.clear()”的调用,平台将移动。

I am trying to explain this in detail, please bear with me. 我正在尝试详细解释,请耐心等待。

Again in your current fiddle, collection array is increasing in its size when Draw method is called. 同样,在您当前的小提琴中,调用Draw方法时,集合数组的大小正在增加。 You can debug your code, no. 您可以调试代码,否。 of blocks increases to thousands in few seconds. 的块数在几秒钟内增加到数千。

So now this is how your code works in loop: 因此,这就是您的代码在循环中的工作方式:

  1. Your code only modifying position of 1st block ie this.collection[0].blockX 您的代码仅修改第一个块的位置,即this.collection[0].blockX
  2. Adds 5 more blocks in collection array with same position. 在位置相同的集合数组中再添加5个块。 for ex: While Update method is called for the second time, below line will get execute 例如:第二次调用Update方法时,下面的行将被执行

     addBlocks(4, "Red", 50, 70, 50, 5); 

    So one more block will get added in 6th position of array. 因此,将在数组的第6个位置添加一个块。

  3. While rendering, it draws first block with updated coordinates because of point 1. and draws that block again because of point 2. 渲染时,由于点1而绘制具有更新坐标的第一个块,并由于点2而再次绘制该块。

This is the reason it is appearing twice. 这就是它出现两次的原因。

You need to modify this logic a little bit to solve the problem. 您需要稍微修改此逻辑以解决该问题。

Hope this makes sense. 希望这是有道理的。

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

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