javascript - 调用未定义对象的原型函数
问题描述
我正在尝试为我的新平台游戏制作碰撞功能,但在制作碰撞原型功能时遇到了错误。
platforms = [];
var newPlatform = function(x, y) {
this.x = x,
this.y = y,
this.width = 50,
this.height
}
var platform1 = new newPlatform(10, 100);
var platform2 = new newPlatform(50, 50);
platforms.push(platform1);
platforms.push(platform2);
var player = {
x:100,
y:305,
width:80,
height:120,
velocity_y:4
}
platforms.prototype.collision = function(player) {
return (this.x < player.x + player.width && this.x + this.width > player.x && this.y
< player.y + player.height && this.y + this.height > player.y);
}
for (let i = 0; i < platforms.length; i++) {
if (platforms[i].collision(player)) {
player.velocity_y = 0 ;
}
}
这给出了一个错误,上面写着“无法设置未定义的属性'碰撞'”有人知道如何解决这个问题吗?
解决方案
推荐阅读
- json - 从反应js中的字典中提取数据
- .htaccess - 如何在没有其他 url 的情况下删除 index.php 并进行推荐 301 重定向
- html - 如何在 Safari 中的音频标签导致的 iOS 锁屏上隐藏播放器?
- oracle - Jhipster/Oracle 连接步骤?开发数据库?
- python - Pandas:如何组合 2 个表并为缺失值插入空白行?
- python - 跨非空列连接字符串
- kubernetes - Kubernetes nginx 的双 hsts 标头
- database-migration - 异步运行 Flyway 迁移
- unity3d - 如何减少统一的drawcall?
- xsl-fo - 页面方向取决于条件