我尝试为我的宝石对象做一个碰撞原型,每次我的玩家与它交互时,它都不会重置。开发工具也不会出现任何错误。
var checkCollisions = function(targets){
var target;
if (Array.isArray(targets)){
for (var i =0; i < targets.length; i++){
target = targets[i];
if (targets === allEnemies){
target.width = 50;
target.height = 40;
}
if (player.x < target.x + target.width && player.x + player.width > target.x && player.y < target.y + target.height && player.y + player.height > target.y){
console.count('collision');
player.reset();
return true;
}
}
}
return false;
};
var Gem = function(x,y){
this.x = [2,275,300];
this.y = [200,150,80];
this.width = 50;
this.height = 40;
this.sprite = 'images/gem-blue.png';
}
Gem.prototype.update = function(){
this.x = 200
this.y = 200
};
Gem.prototype.render = function() {
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
};
Gem.prototype.reset = function(){
this.x = 0;
this.y = 0;
};
Gem.prototype.checkCollision = function(player) {
for(var i = 0; i player.length; i++){
if(player.x < this.x + this.width &&
player.x + player.width > this.x &&
player.y < this.y + this.height &&
player.height + player.y > this.y);
}
console.log("got a gem!");
gem.reset();
};
var gem = new Gem();