javascript - 在 JavaScript 俄罗斯方 block 中绘制新的俄罗斯方 block

标签 javascript arrays canvas tetris

我正在尝试用 JavaScript 绘制四联骨牌,但遇到了一些麻烦。我认为我的代码 this.blocks = i.blocks[b][c] 不正确,但可能更多。现在我的眼睛开始痛了,我决定寻求帮助。 this.blocks = i.blocks[b][c] 是否因为 this.blocks 不能存储数组而不起作用?或者还有其他问题。感谢您的帮助。

这是 jsfiddle 链接:http://jsfiddle.net/8aS9E/

代码如下:

var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");
        canvas.height = window.innerHeight;
        canvas.width = window.innerWidth;
        var h = canvas.height;
        var w = canvas.width;
        var gridWidth = w / 4;
        var gridHeight = h;
        var cols = gridWidth / 10; //width, columns
        var rows = cols; //height, rows
        window.addEventListener("keydown", test, true); 
        var b = 0;
        var c = 0;

    var gravity = 0;

    var id;
    var type = 2;
    var color; 
    var x = gridWidth * 2;
    var y = -30;
    var position;
    var velocityY;
    var tetrominoId = 0;
  var i = { id: 'i', size: 4, 
                blocks: [[0, 1, 0, 0],
                [0, 1, 0, 0],
                [0, 1, 0, 0],
                [0, 1, 0, 0]], 
                color: 'cyan'   };

function tetromino(id, type, color, position, y, velocityY){
    this.id = i.id;
    this.blocks = i.blocks[b][c];
    this.color = i.color;
    this.x = x;
    this.y = y;
    this.velocityY = 1;
}

var tetrominoList = [];

function addTetromino(type, color, position, x, y, velocityY){
tetrominoList[tetrominoId] = new tetromino(tetrominoId, type, color, x, y, velocityY);
tetrominoId++;

}

function tetrominoDraw(){

        tetrominoList.forEach(function(tetromino){

        for(b = 0; b < 4; b++){

            for(c = 0; c < 4; c++){



                    ctx.fillStyle = tetromino.color;
                    ctx.fillRect(
                        gridWidth * 2 + tetromino.blocks[b][c] * 
                        b * cols, tetromino.blocks[b][c] * c * rows + gravity + y,
                        cols, rows
                        );
                    ctx.fill();
                }   
            }
        }   
        });

        }

谢谢!

最佳答案

tetromino.blocks 不是数组而是整数,因为它等于 i.blocks 的第一个数组的第一个元素的值(i.blocks[0][0] 因为 b 和 c 变量都已定义作为初始化中的零)。

您所要做的就是去掉 tetromino 声明中的数组地址:

function tetromino(id, type, color, position, y, velocityY) {
    this.id = i.id;
    this.blocks = i.blocks;
    this.color = i.color;
    this.x = x;
    this.y = y;
    this.velocityY = 1;
}

我已经更新了你的 fiddle : http://jsfiddle.net/8aS9E/1/

关于javascript - 在 JavaScript 俄罗斯方 block 中绘制新的俄罗斯方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22628652/

相关文章:

javascript - collection.fetch() 的选项

javascript - 在 html 中显示页面

c# - while循环用数组填充datagridview

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

javascript - 多平台 HTML5 游戏 DOM+CSS vs CANVAS vs 两者

javascript - 由 eventlisteners() 创建的 Canvas 上的绘图对象

javascript - 如何在单击按钮时使输入字段可见,或在单击按钮时为 ruby​​ on Rails 创建相同的字段

javascript - ASP.NET MVC 包括 Javascript 中的 ASP

php - 如何在 php mysql 中将搜索选项中的数据分配为 json 格式

javascript - 对象动画 - 位置无法更新