发生碰撞时如何修复玩家健康的随机减法?
How to fix random subtraction of players health when collision happens?
我正在制作一个小型 space 飞船游戏,其中 space 飞船必须四处移动才能不被流星击中。当流星击中船只时,玩家应该会失去生命值,但在我的示例中,玩家总是会失去大约 12 的数字,我不知道是什么原因造成的。
我尝试添加起始生命值而不是减去它撞击流星时添加的生命值,但即便如此,生命值还是增加了 10,而不是我想要的增加 1。我尝试使用 console.log 来查找错误,但没有任何效果
let x = 200;
let x1 = 258;
let score = 0;
let health = 5;
let meteors = [];
let ecllipseMeteors = [];
let meteor;
let ecllipseMeteor;
let spaceShipimg;
let meteorimg;
function Meteor() {
this.x = random(0,600);
this.y = random(-200,-190);
this.speed = random(3,10);
this.fall = function() {
this.y = this.y + this.speed;
if (this.y > height) {
this.y = random(-200,-100);
this.speed = random(3, 7);
}
};
this.show = function() { image(meteorimg,this.x,this.y, 40, 40) };
}
function meteormodel() {
this.x = random(0,600);
this.y = random(-200,-190);
this.speed = random(3,10);
this.fall = function() {
this.y = this.y + this.speed;
if (this.y > height) {
this.y = random(-200,-100);
this.speed = random(3, 7);
}
};
this.show = function() { ellipse(this.x,this.y, 20, 20) };
}
function setup() {
// creates canvas
createCanvas(600, 400);
timer = createP('');
meteor = new Meteor();
ecllipseMeteor = new meteormodel();
interval = setInterval(scoreCount, 500);
}
function gameOver() {
textSize(20);
text("GAME OVER YOUR SCORE: " + score, 200, 200);
fill(255);
}
function preload() {
//loads all teh necesarry material
spaceShipimg = loadImage('assets/spaceShip.png');
meteorimg = loadImage('assets/meteor.png');
}
function scoreCount() {
score++;
}
function draw() {
background(11, 72, 170);
hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
if(hit == true) {
health -= 1;
// console.log(health -= 1);
if (health == 0) {
gameOver();
noLoop();
}
}
if (keyIsDown(LEFT_ARROW) && x > -46) {
x -= 5;
}
if (keyIsDown(RIGHT_ARROW) && x < 508) {
x += 5;
}
if (keyIsDown(LEFT_ARROW) && x1 > 9) {
x1 -= 5;
}
if (keyIsDown(RIGHT_ARROW) && x1 < 565) {
x1 += 5;
}
rect(x1, 335, 20, 30)
image(spaceShipimg,x,260,120,120)
meteor.fall();
meteor.show();
textSize(20);
text("Health: " + health, 10, 20);
fill(255);
textSize(20);
text("Score: " + score, 10, 40);
fill(255);
}
预期的结果是,当流星撞击火箭时,玩家的生命值会下降一个。问题是它在 10 点左右。
本质上是在 "tick rate of 60" 处绘制循环,所以这 "true" 不止一次,因此您需要设置一个全局标志,在发生一次后停止它。
hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
if(hit == true) {
health -= 1;
解决方案:
var flag = true; //put this outside the draw scope
if(hit == true && flag) {
health -= 1;
flag = false
}
if(!hit){
flag = true
}
希望对您有所帮助。
我认为问题在于流星在多个帧上撞击宇宙飞船。所以要解决这个问题,你必须不止一次地阻止流星造成伤害。
hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
if(hit == true) {
// Your awsome code!
meteor.y = random(-200,-100);
meteor.speed = random(3, 7);
希望对您有所帮助! :-)
问题是,流星不止一次击中了船舶矿石。一颗流星在从船顶到船底的途中连续撞击船
解决这个问题最简单的方法是强制游戏生成新的流星。在方法 fall
中,产生新流星的条件是 this.y > height
。如果meteor.y
是height+1
,撞到船上的流星会消失,新的位置设置在window的顶部:
function draw() {
background(11, 72, 170);
hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
if(hit == true) {
health -= 1;
meteor.y = height+1; // <----------
if (health == 0) {
gameOver();
noLoop();
}
}
// [...]
}
我正在制作一个小型 space 飞船游戏,其中 space 飞船必须四处移动才能不被流星击中。当流星击中船只时,玩家应该会失去生命值,但在我的示例中,玩家总是会失去大约 12 的数字,我不知道是什么原因造成的。
我尝试添加起始生命值而不是减去它撞击流星时添加的生命值,但即便如此,生命值还是增加了 10,而不是我想要的增加 1。我尝试使用 console.log 来查找错误,但没有任何效果
let x = 200;
let x1 = 258;
let score = 0;
let health = 5;
let meteors = [];
let ecllipseMeteors = [];
let meteor;
let ecllipseMeteor;
let spaceShipimg;
let meteorimg;
function Meteor() {
this.x = random(0,600);
this.y = random(-200,-190);
this.speed = random(3,10);
this.fall = function() {
this.y = this.y + this.speed;
if (this.y > height) {
this.y = random(-200,-100);
this.speed = random(3, 7);
}
};
this.show = function() { image(meteorimg,this.x,this.y, 40, 40) };
}
function meteormodel() {
this.x = random(0,600);
this.y = random(-200,-190);
this.speed = random(3,10);
this.fall = function() {
this.y = this.y + this.speed;
if (this.y > height) {
this.y = random(-200,-100);
this.speed = random(3, 7);
}
};
this.show = function() { ellipse(this.x,this.y, 20, 20) };
}
function setup() {
// creates canvas
createCanvas(600, 400);
timer = createP('');
meteor = new Meteor();
ecllipseMeteor = new meteormodel();
interval = setInterval(scoreCount, 500);
}
function gameOver() {
textSize(20);
text("GAME OVER YOUR SCORE: " + score, 200, 200);
fill(255);
}
function preload() {
//loads all teh necesarry material
spaceShipimg = loadImage('assets/spaceShip.png');
meteorimg = loadImage('assets/meteor.png');
}
function scoreCount() {
score++;
}
function draw() {
background(11, 72, 170);
hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
if(hit == true) {
health -= 1;
// console.log(health -= 1);
if (health == 0) {
gameOver();
noLoop();
}
}
if (keyIsDown(LEFT_ARROW) && x > -46) {
x -= 5;
}
if (keyIsDown(RIGHT_ARROW) && x < 508) {
x += 5;
}
if (keyIsDown(LEFT_ARROW) && x1 > 9) {
x1 -= 5;
}
if (keyIsDown(RIGHT_ARROW) && x1 < 565) {
x1 += 5;
}
rect(x1, 335, 20, 30)
image(spaceShipimg,x,260,120,120)
meteor.fall();
meteor.show();
textSize(20);
text("Health: " + health, 10, 20);
fill(255);
textSize(20);
text("Score: " + score, 10, 40);
fill(255);
}
预期的结果是,当流星撞击火箭时,玩家的生命值会下降一个。问题是它在 10 点左右。
本质上是在 "tick rate of 60" 处绘制循环,所以这 "true" 不止一次,因此您需要设置一个全局标志,在发生一次后停止它。
hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
if(hit == true) {
health -= 1;
解决方案:
var flag = true; //put this outside the draw scope
if(hit == true && flag) {
health -= 1;
flag = false
}
if(!hit){
flag = true
}
希望对您有所帮助。
我认为问题在于流星在多个帧上撞击宇宙飞船。所以要解决这个问题,你必须不止一次地阻止流星造成伤害。
hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
if(hit == true) {
// Your awsome code!
meteor.y = random(-200,-100);
meteor.speed = random(3, 7);
希望对您有所帮助! :-)
问题是,流星不止一次击中了船舶矿石。一颗流星在从船顶到船底的途中连续撞击船
解决这个问题最简单的方法是强制游戏生成新的流星。在方法 fall
中,产生新流星的条件是 this.y > height
。如果meteor.y
是height+1
,撞到船上的流星会消失,新的位置设置在window的顶部:
function draw() {
background(11, 72, 170);
hit = collideRectCircle(x1, 335, 20, 30, meteor.x, meteor.y, 40);
if(hit == true) {
health -= 1;
meteor.y = height+1; // <----------
if (health == 0) {
gameOver();
noLoop();
}
}
// [...]
}