无法访问函数内创建的类

时间:2018-01-08 20:34:25

标签: javascript

我正在尝试为html5 javascript制作一个简单的棋盘游戏引擎,但我似乎无法访问其他类中的函数或函数内的任何类。我只能在所有函数之外创建一个新类:

<canvas id = "canvas" width = "640" height = "480" style = "border: 1px solid gray; width: 640px; height: 480px;" >
</canvas>

<script>
    
    var canvas = document.getElementById("canvas")
    var ctx = canvas.getContext("2d");
    
    class Board {
        construtor(height, width, cellSize, spaceing) {
            this.height = height;
            this.width = width;
            this.cellSize = cellSize;
            this.spaceing = spaceing;
            this.board = new Array(width).fill(new Array(height).fill(new Cell("black",1)));
        }
        
        draw() {
            var x = 0;
            this.board.forEach(function (cell) {
                ctx.fillStyle = cell.color;w
                ctx.fillRect(
                    this.spaceing + (x * (this.cellsize + this.spaceing)),
                    this.spaceing,
                    this.cellSize,
                    this.cellSize,
                );
                x++;
            });
        }
    }
    
    
    

    class Cell {
        construtor(color, value) {
            var color = color;
            var value = value;
        }
        
        
    }
    
    
    setup();

    function setup() {
        var board = new Board(3,3,10,1);
        board.draw();
    }
   
    
</script>

请帮忙!我很乐意取得进步。 唯一的解决方法是在开始时制作所有变量,但这样做效率非常低并且存在很多问题

0 个答案:

没有答案