Javascript:这个代码片段有什么作用和错误代码Uncatch TypeError:allEnemies.forE
Javascript: What does this code snippet do and error code Uncaught TypeError: allEnemies.forEach is not a function
我正在努力完成一个简单的画布游戏。 我收到此错误:
Uncatch TypeError: allEnemies.forEach 不是函数
为什么我会收到此错误?
我在下面展示我是如何宣布所有敌人的。 根据说明,所有敌人都应该是一个对象数组。
是我对所有敌人的声明是错误的,还是对象敌人的声明是错误的?
这是敌人的声明,我认为他们正在尝试声明一个对象(我从未见过这样的对象声明)我认为这是一个 IIFE 正确吗?
var Enemy = function() {
// Variables applied to each of our instances go here,
// we've provided one for you to get started
// The image/sprite for our enemies, this uses
// a helper we've provided to easily load images
this.sprite = 'images/enemy-bug.
这是整个应用程序.js文件:
// Enemies our player must avoid
var Enemy = function() {
// Variables applied to each of our instances go here,
// we've provided one for you to get started
// The image/sprite for our enemies, this uses
// a helper we've provided to easily load images
this.sprite = 'images/enemy-bug.png';
}
// Update the enemy's position, required method for game
// Parameter: dt, a time delta between ticks
Enemy.prototype.update = function(dt) {
// You should multiply any movement by the dt parameter
// which will ensure the game runs at the same speed for
// all computers.
}
// Draw the enemy on the screen, required method for game
Enemy.prototype.render = function() {
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
}
// Now write your own player class
// This class requires an update(), render() and
// a handleInput() method.
var Player = function() {
// Variables applied to each of our instances go here,
// we've provided one for you to get started
this.sprite = 'images/char-pink-girl.png';
}
// Now instantiate your objects.
// Place all enemy objects in an array called allEnemies
// Place the player object in a variable called player
var allEnemies = [];
allEnemies = Enemy;
var player = Player;
// This listens for key presses and sends the keys to your
// Player.handleInput() method. You don't need to modify this.
document.addEventListener('keyup', function(e) {
var allowedKeys = {
37: 'left',
38: 'up',
39: 'right',
40: 'down'
};
player.handleInput(allowedKeys[e.keyCode]);
});
这是给我错误类型的代码行:
allEnemies.forEach(function(enemy) {
下面是函数声明的其余部分:
function updateEntities(dt) {
allEnemies.forEach(function(enemy) {
enemy.update(dt);
});
player.update();
}
在第 1 行中,allEnemies
是一个数组。
下一行它变成Enemy
.
var allEnemies = [];
allEnemies = Enemy;
Enemy
是一个函数。
allEnemies
现在是一个函数。
Function.prototype.forEach 是undefined
问题就在这里
allEnemies = Enemy;
您将 allEnemies 声明为一个数组,但您用上述语句覆盖了它。现在 allEnemies 是一个简单的函数,而不是一个数组。因此,forEach 是未定义的。
要创建敌人对象的数组,您应该执行以下操作:
allEnemies.push(Enemy);
相关文章:
- 咕噜声.js无法读取文件(错误代码:EISDIR)
- 科尔多瓦指南针 API 给出错误代码 3
- 如何在使用JavascriptCallbackBehavior时引发带有HTTP错误代码的WebFaultExcepti
- 解析错误代码 121
- Ajax get jsonp给出错误代码500“;请求被阻止”;来自MVC应用程序
- ionic http获取到本地文件会给出http错误代码0
- Imacros/jQuery TypeError:无法访问死对象,第 816 行(错误代码:-991)
- 电话差距/网络数据库错误代码说明
- NaN 错误代码有什么问题
- 异常:“远程主机关闭了连接.错误代码为 0x80070057”
- 咕噜声/巴别塔错误:无法写入“dist”文件(错误代码:EISDIR)
- 科尔多瓦的文件传输写入错误(代码 1)
- 角度$asyncValidators将错误代码传输到模板
- 接收错误代码 3,http_status空,正文空,异常状态 2
- 运行 grunt.file.copy 返回错误代码:ENOENT
- Parse.com 找不到错误代码:141 函数
- 尝试运行作业时分析错误代码 1 内部错误
- 节点 js 退出状态 8,错误代码 0 在 Mac 上运行
- 如何使用mootools确定损坏图像上的错误代码
- Javascript:这个代码片段有什么作用和错误代码Uncatch TypeError:allEnemies.forE