首页 > 解决方案 > 调用未定义对象的原型函数

问题描述

我正在尝试为我的新平台游戏制作碰撞功能,但在制作碰撞原型功能时遇到了错误。

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 ;
    }
}

这给出了一个错误,上面写着“无法设置未定义的属性'碰撞'”有人知道如何解决这个问题吗?

标签: javascriptgame-physicscollisionplatform

解决方案


推荐阅读