我正在尝试使用 EnchantJS 制作一个 HTML5/JavaScript 游戏,其中包含一个 6x6 的彩色方块网格。我有 1 张图像要显示,但不是有 36x4 行代码来显示我想将其全部移动到函数中的所有图像。代码在第 62 行中断this.addChild(block);
。我似乎无法弄清楚为什么这不起作用。过去几天我一直在阅读什么是函数以及如何在函数之间调用信息。我无法弄清楚自己做错了什么,也无法通过搜索找到答案。我来自纯粹的 Java 和 C++ 背景,所以我认为我弄乱了语法并且不理解某些东西。
enchant();
window.onload = function() {
/*
* Game width and height.
* Note: Game scale is half so actual width=width/2 and
* actual heigh it height/2.
*/
var game = new Game(1280,768);
/*
* Game Preload Vars
*/
game.preload('res/bg2x.png',
'res/block.png');
game.fps = 30;
game.scale = 0.5;
game.onload = function() {
console.log("Game Loaded");
var scene = new SceneGame;
game.pushScene(scene);
}
game.start();
window.scrollTo(0, 0);
var SceneGame = Class.create(Scene, {
initialize: function() {
var game, bg;
Scene.apply(this);
game = Game.instance;
// Background
bg = new Sprite(1280,768);
bg.image = game.assets['res/bg2x.png'];
// Populate Screen
this.addChild(bg);
gridBuilder(500,75);
}
});
var gridBuilder = function(x,y) {
this.x=x;
this.y=y;
block = new Block;
block.x = x;
block.y = y;
this.block = block;
this.addChild(block); // THIS IS WHERE THE ERROR IS OCCURING.
};
var Block = Class.create(Sprite, {
// The player character.
initialize: function() {
// 1 - Call superclass constructor
Sprite.apply(this,[100, 100]);
this.image = Game.instance.assets['res/block.png'];
// 2 - Animate
this.animationDuration = 0;
this.addEventListener(Event.ENTER_FRAME, this.updateAnimation);
},
updateAnimation: function (evt) {
this.animationDuration += evt.elapsed * 0.001;
if (this.animationDuration >= 0.25) {
this.frame = (this.frame + 1) % 4;
this.animationDuration -= 0.25;
}
},
});
}