简体   繁体   English

我的 JavaScript 代码在运行时没有显示任何内容是否有原因?

[英]Is there a reason my JavaScript code isn't displaying anything when I run it?

I am new to the coding scene and I'm trying to to finish my friends incomplete code for a board game in p5.js can anyone explain why nothing is showing up when I run it?我是编码领域的新手,我正在尝试在 p5.js 中为我的朋友完成一个棋盘游戏的不完整代码,谁能解释为什么我运行它时什么都没有出现? I defined the createBoard() function and call it in the draw() function so it should be working but it doesn't.我定义了createBoard() function 并在draw() function 中调用它,所以它应该可以工作,但它没有。

I have the setup and draw functions at the bottom.我在底部有setupdraw功能。

Any help is appreciated.任何帮助表示赞赏。

 var CANVAS_WIDTH = 1080; var CANVAS_HEIGHT = 700; var dataBoard = [ [], [], [], [], [], [] //infirmaries ]; function board(x, y) { return dataBoard[y][x]; } //2D array with the terrain/design of the board. var boardSquaresTemplate = [ [2, 2, 1, 1, 1, 1, 1, 1, 5, 5], [2, 2, 1, 1, 1, 1, 1, 1, 5, 5], [2, 2, 1, 1, 0, 0, 0, 0, 0, 0], [4, 4, 1, 1, 0, 0, 0, 0, 0, 0], [4, 4, 1, 1, 0, 0, 0, 0, 0, 0] ]; //takes gridX and gridY params and returns terrain type function getTerrain(gridX, gridY) { return boardSquaresTemplate[gridY][gridX]; } //measured in board squares, the height of the board var boardHeight = boardSquaresTemplate.length; //measured in board squares, the length of the board var boardWidth = boardSquaresTemplate[0].length; //measured in board squares, the area of the board var boardArea = boardHeight * boardWidth; //console.log(boardArea); //grid square width var GRID_SQUARE_WIDTH = 96; //rgb values for terrain types var terrainTextures = [ {r: 100, g: 180, b: 255}, // 0 = water {r: 200, g: 160, b: 050}, // 1 = land {r: 100, g: 080, b: 010}, // 2 = mountain {r: 230, g: 230, b: 230}, // 3 = infirmary {r: 100, g: 080, b: 255}, // 4 = blue spawn {r: 255, g: 080, b: 025}, // 5 = red spawn ]; //takes one board square (center) grid coord and converts to pixel coord function gridToPixel(gridX) { var pixelX = 100 * gridX + 90; return pixelX; } //inverse of above function, may return a FLOAT function pixelToGrid(pixelX) { var gridX = (pixelX - 90) / 100 return gridX } //Constructor function, constructs BoardSquare objects to populate dataBoard class BoardSquare { constructor (gridX, gridY, type) { this.gridX = gridX; this.gridY = gridY; this.terrainType = type; this.crease = {e: false, n: false, w: false, s: false}; this.occupant = null; this.isHoveredOver = false; this.isClicked = false; this.isClicked2 = false; //x coord of center of square this.pixelX = gridToPixel(this.gridX); //y coord of center of square this.pixelY = gridToPixel(this.gridY); this.appear = function() { push(); rectMode(CENTER); //draws the square with correct color using the terrainTextures array fill(terrainTextures[this.terrainType].r, terrainTextures[this.terrainType].g, terrainTextures[this.terrainType].b); rect(this.pixelX, this.pixelY, GRID_SQUARE_WIDTH, GRID_SQUARE_WIDTH); //Hover highlight feature: if(this.isHoveredOver) { fill(255, 255, 210, 85); rect(this.pixelX, this.pixelY, GRID_SQUARE_WIDTH, GRID_SQUARE_WIDTH); } if(this.isClicked) { fill(210, 255, 210, 100); rect(this.pixelX, this.pixelY, GRID_SQUARE_WIDTH, GRID_SQUARE_WIDTH); } pop(); //crease stuff push(); noStroke(); rectMode(CORNERS); //THIS IS NOT THE SAME AS "CORNER" translate(this.pixelX, this.pixelY); fill(51); if(this.crease.e){ rect(34, -GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2); } if(this.crease.n){ rotate(octogree(2)); rect(34, -GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2); rotate(octogree(-2)); } if(this.crease.w){ rotate(octogree(4)); rect(34, -GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2); rotate(octogree(-4)); } if(this.crease.s){ rotate(octogree(6)); rect(34, -GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2); rotate(octogree(-6)); } pop(); } } } function createBoard() { for(var y = 0; y < boardHeight; y++){ for(var x = 0; x < boardWidth; x++){ var terrainType = getTerrain(x, y); //checks if loaded terrain type is invalid if(terrainType < 0 || terrainType >= terrainTextures.length || terrainType % 1.= 0){ console,log("Invalid terrain type at grid space " + x + "; " + y), } dataBoard[y][x] = new BoardSquare(x, y; terrainType); } } } function octogree(x) { var radians = - x / 8 * TAU; return radians; } function radToOctogree(x) { var octogrees = (-8 * x / TAU + 24) % 8; return octogrees, } function octoAdd(a; b) { var result = a + b; result = (result + 24) % 8; return result. } console;log(dataBoard), function setup() { createCanvas(CANVAS_WIDTH; CANVAS_HEIGHT); background(51); createBoard(); } function draw() { createBoard(); }

You miss drawing on the board by invoking appear() on the cells of the board.通过在板上的单元格上调用appear() ,您错过了在板上的绘图。 Use 2 nested loops to traverse the cells of the board and call the method appear() for each cell:使用2个嵌套循环遍历棋盘的单元格,并为每个单元格调用方法appear()

function draw() {
    background(51);
    for(var y = 0; y < dataBoard.length; y++){
        for(var x = 0; x < dataBoard[y].length; x++){
            dataBoard[y][x].appear();
        }
    }
}

Example:例子:

 var CANVAS_WIDTH = 1080; var CANVAS_HEIGHT = 700; var dataBoard = [ [], [], [], [], [], [] //infirmaries ]; function board(x, y) { return dataBoard[y][x]; } //2D array with the terrain/design of the board. var boardSquaresTemplate = [ [2, 2, 1, 1, 1, 1, 1, 1, 5, 5], [2, 2, 1, 1, 1, 1, 1, 1, 5, 5], [2, 2, 1, 1, 0, 0, 0, 0, 0, 0], [4, 4, 1, 1, 0, 0, 0, 0, 0, 0], [4, 4, 1, 1, 0, 0, 0, 0, 0, 0] ]; //takes gridX and gridY params and returns terrain type function getTerrain(gridX, gridY) { return boardSquaresTemplate[gridY][gridX]; } //measured in board squares, the height of the board var boardHeight = boardSquaresTemplate.length; //measured in board squares, the length of the board var boardWidth = boardSquaresTemplate[0].length; //measured in board squares, the area of the board var boardArea = boardHeight * boardWidth; //console.log(boardArea); //grid square width var GRID_SQUARE_WIDTH = 96; //rgb values for terrain types var terrainTextures = [ {r: 100, g: 180, b: 255}, // 0 = water {r: 200, g: 160, b: 050}, // 1 = land {r: 100, g: 080, b: 010}, // 2 = mountain {r: 230, g: 230, b: 230}, // 3 = infirmary {r: 100, g: 080, b: 255}, // 4 = blue spawn {r: 255, g: 080, b: 025}, // 5 = red spawn ]; //takes one board square (center) grid coord and converts to pixel coord function gridToPixel(gridX) { var pixelX = 100 * gridX + 90; return pixelX; } //inverse of above function, may return a FLOAT function pixelToGrid(pixelX) { var gridX = (pixelX - 90) / 100 return gridX } //Constructor function, constructs BoardSquare objects to populate dataBoard class BoardSquare { constructor (gridX, gridY, type) { this.gridX = gridX; this.gridY = gridY; this.terrainType = type; this.crease = {e: false, n: false, w: false, s: false}; this.occupant = null; this.isHoveredOver = false; this.isClicked = false; this.isClicked2 = false; //x coord of center of square this.pixelX = gridToPixel(this.gridX); //y coord of center of square this.pixelY = gridToPixel(this.gridY); this.appear = function() { push(); rectMode(CENTER); //draws the square with correct color using the terrainTextures array fill(terrainTextures[this.terrainType].r, terrainTextures[this.terrainType].g, terrainTextures[this.terrainType].b); rect(this.pixelX, this.pixelY, GRID_SQUARE_WIDTH, GRID_SQUARE_WIDTH); //Hover highlight feature: if(this.isHoveredOver) { fill(255, 255, 210, 85); rect(this.pixelX, this.pixelY, GRID_SQUARE_WIDTH, GRID_SQUARE_WIDTH); } if(this.isClicked) { fill(210, 255, 210, 100); rect(this.pixelX, this.pixelY, GRID_SQUARE_WIDTH, GRID_SQUARE_WIDTH); } pop(); //crease stuff push(); noStroke(); rectMode(CORNERS); //THIS IS NOT THE SAME AS "CORNER" translate(this.pixelX, this.pixelY); fill(51); if(this.crease.e){ rect(34, -GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2); } if(this.crease.n){ rotate(octogree(2)); rect(34, -GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2); rotate(octogree(-2)); } if(this.crease.w){ rotate(octogree(4)); rect(34, -GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2); rotate(octogree(-4)); } if(this.crease.s){ rotate(octogree(6)); rect(34, -GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2, GRID_SQUARE_WIDTH / 2); rotate(octogree(-6)); } pop(); } } } function createBoard() { for(var y = 0; y < boardHeight; y++){ for(var x = 0; x < boardWidth; x++){ var terrainType = getTerrain(x, y); //checks if loaded terrain type is invalid if(terrainType < 0 || terrainType >= terrainTextures.length || terrainType % 1.= 0){ console,log("Invalid terrain type at grid space " + x + "; " + y), } dataBoard[y][x] = new BoardSquare(x, y; terrainType); } } } function octogree(x) { var radians = - x / 8 * TAU; return radians; } function radToOctogree(x) { var octogrees = (-8 * x / TAU + 24) % 8; return octogrees, } function octoAdd(a; b) { var result = a + b; result = (result + 24) % 8; return result. } console;log(dataBoard), function setup() { createCanvas(CANVAS_WIDTH; CANVAS_HEIGHT); createBoard(); } function draw() { background(51); for(var y = 0. y < dataBoard;length; y++){ for(var x = 0. x < dataBoard[y];length. x++){ dataBoard[y][x];appear(); } } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.min.js"></script>

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

相关问题 我的Javascript没有打印任何内容 - My Javascript isn't printing anything 当我使用 javascript 构造函数方法创建对象时,我的对象没有显示 - My object isn't displaying when i use the javascript constructor method to create an object 为什么我的即时验证没有显示任何内容? - Why isn't my instantaneous validation displaying anything? 为什么我的作用域插槽不显示任何内容? - Why isn't my scoped slot displaying anything? 当我将 QuillJS setContent 设置为 JSON 对象时,为什么它不显示任何内容? - Why isn't the QuillJS setContent displaying anything when I set it to a JSON Object? 帆布屋。 将代码复制到编辑器并在浏览器中运行时,为什么代码不起作用? - Canvas- house. Why isn`t the code working, when I copy it to an editor and run it in my browser? 我的代码没有任何作用。 可能是什么原因? - My code doesn't do anything. What could be the reason? 当我似乎找不到任何错误时,此代码为什么不起作用? - How come this code isn't working when I can't seem to find anything wrong? 我在我的代码中找不到我的错误。 旁边的 Total Estimae 未显示 - I can't find my error in my code. The aside Total Estimae isn't displaying 为什么我的JavaScript倒数功能没有做任何事情? - Why isn't my JavaScript countdown function doing anything?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM