Javascript 用于碰撞检测的阵列目标元素
更新:我想让你知道,我已经解决了这个问题,如果不一定回答我自己的问题。我没有尝试以推送数组的元素为目标,而是这样做: 在 我补充说Javascript 用于碰撞检测的阵列目标元素,javascript,html,canvas,Javascript,Html,Canvas,更新:我想让你知道,我已经解决了这个问题,如果不一定回答我自己的问题。我没有尝试以推送数组的元素为目标,而是这样做: 在 我补充说 tokens=[]; 到最后 然后我编写了一个新函数updateTokens: function updateTokens() { for (var i = 0; i < tokens.length; i++) { tokens[i].update(); tokens[i].draw(); if (p
tokens=[];
到最后
然后我编写了一个新函数updateTokens:
function updateTokens() {
for (var i = 0; i < tokens.length; i++) {
tokens[i].update();
tokens[i].draw();
if (player.minDist(tokens[i]) <= player.width - platformWidth / 2) {
gameOver();
}
}
if (tokens[0] && tokens[0].x < - platformWidth) {
tokens.splice(0, 1);
}
}
我在这里完全不懂。我一直在按照教程制作HTML5画布游戏。这很好,我学到了很多东西,但我完全被卡住了。
我有一个精灵的向量
function Sprite(x, y, type) {
this.x = x;
this.y = y;
this.width = platformWidth;
this.height = platformWidth;
this.type = type;
Vector.call(this, x, y, 0, 0);
this.update = function () {
this.dx = -player.speed;
this.advance();
};
this.draw = function () {
ctx.save();
ctx.translate(0.5, 0.5);
ctx.drawImage(assetLoader.imgs[this.type], this.x, this.y);
ctx.restore();
};
}
Sprite.prototype = Object.create(Vector.prototype);
所有精灵都从中继承。要生成“令牌”精灵,我使用以下函数:
function spawnEnvironmentSprites() {
if (score > 0 && rand(0, 20) === 0 && platformHeight < 3) {
if (Math.random() > 0.5) {
environment.push(new Sprite(
canvas.width + platformWidth % player.speed,
platformBase - platformHeight * 2 * platformSpacer - platformWidth,
'tokens'
));
}
else if (platformLength > 2) {
environment.push(new Sprite(
canvas.width + platformWidth % player.speed,
platformBase - platformHeight * 1.5 * platformSpacer - platformWidth,
'tokens'
));
}
}
}
谢谢
更新:好的,我仍然没有解决这个问题,但我已经做了一个改变,我认为这会让事情变得更容易,你可以帮助我(!)
我已经从'spawneEnvironmentSprites()'函数中删除了'tokens',并在'spawneMySprites'中添加了一个推送'tokens'的条件。这意味着它们现在具有与我的其他“敌人”相同的行为,即在碰撞检测时运行gameOver()。我现在的问题是,如何在这一行代码中定位“令牌”,以便为冲突提供不同的行为?我希望“令牌”精灵消失,分数增加,但我不知道如何只瞄准阵列中的“令牌”部分。处理碰撞的行是:
if (player.minDist(enemies[i]) <= player.width - platformWidth/2) {
gameOver();
}
}
if(玩家、心智主义者(敌人[i])
function Sprite(x, y, type) {
this.x = x;
this.y = y;
this.width = platformWidth;
this.height = platformWidth;
this.type = type;
Vector.call(this, x, y, 0, 0);
this.update = function () {
this.dx = -player.speed;
this.advance();
};
this.draw = function () {
ctx.save();
ctx.translate(0.5, 0.5);
ctx.drawImage(assetLoader.imgs[this.type], this.x, this.y);
ctx.restore();
};
}
Sprite.prototype = Object.create(Vector.prototype);
function spawnEnvironmentSprites() {
if (score > 0 && rand(0, 20) === 0 && platformHeight < 3) {
if (Math.random() > 0.5) {
environment.push(new Sprite(
canvas.width + platformWidth % player.speed,
platformBase - platformHeight * 2 * platformSpacer - platformWidth,
'tokens'
));
}
else if (platformLength > 2) {
environment.push(new Sprite(
canvas.width + platformWidth % player.speed,
platformBase - platformHeight * 1.5 * platformSpacer - platformWidth,
'tokens'
));
}
}
}
if (player.minDist(enemies[i]) <= player.width - platformWidth/2) {
gameOver();
}
}
var ground = [], water = [], enemies = [], environment = [];
if (player.minDist(enemies[i]) <= player.width - platformWidth/2) {
gameOver();
}
}