我正在做一个小型的太空飞船游戏,太空飞船必须四处走动,以免被流星击中。当流星击中飞船时,玩家本应在健康上损失,但是在我的例子中,玩家总是输掉一个大约12的数字,我不知道是什么原因造成的。
我尝试添加起始生命值,而不是减去它在流星上会添加的生命值,但是即使那样,生命值也增加了10,而不是我想要的一个。我尝试使用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。
答案 0 :(得分:1)
问题是,流星击中了船矿一次。一枚流星在从船顶到船底的途中不断撞击船。
解决此问题的最简单方法是,迫使游戏产生新的流星。在方法fall
中,产生新流星的条件是this.y > height
。如果meteor.y
是height+1
,则撞上飞船的流星将消失,并且在窗口顶部设置一个新位置:
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();
}
}
// [...]
}
答案 1 :(得分:0)
本质上,绘制循环的“滴答速度为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
}
希望这会有所帮助。
答案 2 :(得分:0)
我相信问题是流星在多个帧上撞击飞船。因此,要解决此问题,您必须使流星停止多次损坏。
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);
希望这会有所帮助! :-)