0
我做了這個作爲HTML二維數組的Javascript
<!doctype html>
<head>
<meta charset="utf-8">
<script src="jQuery 2.1.js"></script>
<title>2d array</title>
</head>
<body>
<div id="stage"></div>
<p id="output"></p>
<script src="2D_ARRAY ch color.js"></script>
</body>
<style>
#stage {
position: relative;
}
.cell {
display: block;
position: absolute;
width: 40px;
height: 40px;
border: 1px solid black;
background-color: white;
}
.player {
display: block;
position: absolute;
width: 40px;
height: 40px;
border: 1px solid black;
background-color: white;
}
p {
position: relative;
top: 240px;
width: 400px;
}
</style>
和JS文件:
(function(){
var stage = document.querySelector("#stage");
var output = document.querySelector("#output");
window.addEventListener("keydown" , keydownHandler , false);
var map =
[
[0, 1, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 3]
];
var gameObjects =
[
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 2]
];
var UP = 38;
var DOWN = 40;
var RIGHT = 39;
var LEFT = 37;
var SHIP = 2;
var PERETE = 1;
var ROWS = map.length;
var COLUMNS = map[0].length;
var playerRow;
var playerColumn;
var SIZE = 40;
render();
function keydownHandler(event) {
var player = document.querySelector(".player");
switch (event.keyCode) {
case UP:
if (playerRow > 0) {
gameObjects[playerRow][playerColumn] = 0;
playerRow --;
gameObjects[playerRow][playerColumn] = SHIP;
}
break;
case DOWN:
if (playerRow < 7) {
gameObjects[playerRow][playerColumn] = 0;
playerRow ++;
gameObjects[playerRow][playerColumn] = SHIP;
}
break;
case LEFT:
if (playerColumn > 0) {
gameObjects[playerRow][playerColumn] = 0;
playerColumn --;
gameObjects[playerRow][playerColumn] = SHIP;
}
break;
case RIGHT:
if (playerColumn < 7) {
gameObjects[playerRow][playerColumn] = 0;
playerColumn ++;
gameObjects[playerRow][playerColumn] = SHIP;
}
break;
}
if (map[playerRow][playerColumn] === PERETE) {
player.style.backgroundColor = "red";
}
render();
}
function render() {
for (rows = 0; rows < ROWS; rows++) {
for (columns = 0; columns < COLUMNS; columns++) {
var cell = document.createElement("div");
cell.setAttribute("class" , "cell");
stage.appendChild(cell);
pereteRow = rows;
pereteColumn = columns;
cell.style.top = (rows * SIZE) + "px";
cell.style.left = (columns * SIZE) + "px";
if (map[rows][columns] === 1) {
cell.style.backgroundColor = "black";
}
if (gameObjects[rows][columns] === SHIP) {
var player = document.createElement("div");
player.setAttribute("class" , "player");
stage.appendChild(player);
playerRow = rows;
playerColumn = columns;
player.style.top = (rows * SIZE) + "px";
player.style.left = (columns * SIZE) + "px";
player.style.backgroundColor = "green";
}
}
}
}
}());
我的目的是綠化廣場變成紅色了黑色方塊。我的條件,看看綠色方塊是否是黑色的,但我不明白爲什麼var「player
」的backgroundColor屬性不能在聽衆中調用
謝謝!
什麼是你的問題?請具體說明。 – dgvid