- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力完成一个简单的 Canvas 游戏。我收到此错误:
未捕获类型错误:allEnemies.forEach 不是函数
为什么我会收到此错误?
我在下面展示了我如何声明所有敌人。根据说明,allEnemies 应该是一个对象数组。
是我对 allEnemies 的声明错误,还是对象 Enemy 声明错误?
这是 Enemy 的声明,我认为他们正在尝试声明一个对象(我从未见过这样的对象声明)我认为这是 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.
这是整个 app.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
关于Javascript:此代码片段的作用和错误代码 Uncaught TypeError: allEnemies.forEach 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35103037/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!