我正在尝试使用HTML5制作游戏,但由于我是编程新手,因此我有许多错误,我不明白。 Cannot read property 'appendChild' of undefined
就是其中之一。我不使用jQuery,我使用dom添加/删除类
如何修复以下代码,以便不再出现该错误?
var dom = start.dom,
$ = dom.$,
canvas, ctx,
pUruchomienie = true;
function createBackground() {
var background = document.createElement("canvas"),
bgctx = background.getContext("2d");
dom.addClass(background, "background");
background.width = 640;
background.height = 480;
bgctx.fillStyle = "rgba(225,235,255,0.15)";
for (var x=0;x<10;x++) {
for (var y=0;y<10;y++) {
if ((x+y) % 2) {
bgctx.fillRect(
x * 2, y * 2,
2, 2
);
}
}
}
dom.addClass(background, "board-bg");
return background;
}
function setup(){
var boardElement = $("#ekran-gry .ekran-planszy")[0];
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
dom.dodKlase(canvas, "plansza");
canvas.width = 640;
canvas.height = 480;
// boardElement.appendChild(createBackground()); <- is where error pop up
boardElement.appendChild(canvas); <- is where error pop up
CSS代码&amp; HTML
#ekran-gry .ekran-planszy {
position : relative;
width : 640px;
height : 480px;
}
#ekran-gry .ekran-planszy .board-bg,
#ekran-gry .ekran-planszy .plansza {
position : absolute;
width : 100%;
height : 100%;
}
</div>
<div class = "ekran" id = "ekran-gry">
<div class = "ekran-planszy"></div>
</div>
</div>
这是我的代码jsfiddle:http://jsfiddle.net/ukjh45Lr/