我遇到过一个我以前从未见过的问题,因为我对javascript很新。目前正试图让HUD参与我已经下载的无尽赛跑游戏(链接:http://www.html5quintus.com/quintus/examples/runner/)。复制了所有图像,JSON,javascript和索引文件,并设法让它与屏幕顶部的一些gui标签一起使用,以指示生命和健康剩余的数量。当玩家与箱子发生碰撞时,我从玩家的整体健康状况中扣除25(存储在名为hitAmount的变量中)(从100开始),但是在玩家和箱子之间的第一次冲击时,我看到' NAN'而不是简单的减法计算的结果。有谁知道为什么会这样?
window.addEventListener("load",function() {
var Q = window.Q = Quintus()
.include("Sprites, Scenes, Input, 2D, Anim, Touch, UI")
.setup({ maximize: true })
.controls().touch()
var SPRITE_BOX = 1;
Q.gravityY = 2000;
var lives = 3;
var damageAmount = 25;
Q.Sprite.extend("Player",{
init: function(p) {
this._super(p,{
sheet: "player",
sprite: "player",
collisionMask: SPRITE_BOX,
x: 40,
y: 555,
standingPoints: [ [ -16, 44], [ -23, 35 ], [-23,-48], [23,-48], [23, 35 ], [ 16, 44 ]],
duckingPoints : [ [ -16, 44], [ -23, 35 ], [-23,-10], [23,-10], [23, 35 ], [ 16, 44 ]],
speed: 500,
health: 100,
jump: -700
});
this.p.points = this.p.standingPoints;
this.add("2d, animation");
},
step: function(dt) {
this.p.vx += (this.p.speed - this.p.vx)/4;
if(this.p.y > 555) {
this.p.y = 555;
this.p.landed = 1;
this.p.vy = 0;
} else {
this.p.landed = 0;
}
if(Q.inputs['up'] && this.p.landed > 0) {
this.p.vy = this.p.jump;
}
this.p.points = this.p.standingPoints;
if(this.p.landed) {
if(Q.inputs['down']) {
this.play("duck_right");
this.p.points = this.p.duckingPoints;
} else {
this.play("walk_right");
}
} else {
this.play("jump_right");
}
this.stage.viewport.centerOn(this.p.x + 300, 400 );
}
});
Q.Sprite.extend("Box",{
init: function() {
var levels = [ 565, 540, 500, 450 ];
var player = Q("Player").first();
this._super({
x: player.p.x + Q.width + 50,
y: levels[Math.floor(Math.random() * 3)],
frame: Math.random() < 0.5 ? 1 : 0,
scale: 2,
type: SPRITE_BOX,
sheet: "crates",
vx: -600 + 200 * Math.random(),
vy: 0,
ay: 0,
theta: (300 * Math.random() + 200) * (Math.random() < 0.5 ? 1 : -1)
});
this.on("hit");
},
step: function(dt) {
this.p.x += this.p.vx * dt;
this.p.vy += this.p.ay * dt;
this.p.y += this.p.vy * dt;
if(this.p.y != 565) {
this.p.angle += this.p.theta * dt;
}
if(this.p.y > 800) { this.destroy(); }
},
hit: function() {
this.p.type = 0;
this.p.collisionMask = Q.SPRITE_NONE;
this.p.vx = 200;
this.p.ay = 400;
this.p.vy = -300;
this.p.opacity = 0.5;
//change this so crate hitting player reduces his health
parseInt(this.p.health -= damageAmount);
Q.stageScene('hud', 3, this.p);
}
});
Q.GameObject.extend("BoxThrower",{
init: function() {
this.p = {
launchDelay: 0.75,
launchRandom: 1,
launch: 2
}
},
update: function(dt) {
this.p.launch -= dt;
if(this.p.launch < 0) {
this.stage.insert(new Q.Box());
this.p.launch = this.p.launchDelay + this.p.launchRandom * Math.random();
}
}
});
Q.scene("level1",function(stage) {
stage.insert(new Q.Repeater({ asset: "background-wall.png",
speedX: 0.5 }));
stage.insert(new Q.Repeater({ asset: "background-floor.png",
repeatY: false,
speedX: 1.0,
y: 300 }));
stage.insert(new Q.BoxThrower());
stage.insert(new Q.Player());
stage.add("viewport");
});
Q.scene('hud',function(stage) {
var container = stage.insert(new Q.UI.Container({
x: 50, y: 0
}));
var label = container.insert(new Q.UI.Text({x:200, y: 20,
label: "health: " + stage.options.health, color: "Fuchsia" }));
var strength = container.insert(new Q.UI.Text({x:50, y: 20,
label: "Lives: " + lives, color: "Fuchsia" }));
container.fit(20);
});
Q.load("player.json, player.png, background-wall.png, background-floor.png, crates.png, crates.json", function() {
Q.compileSheets("player.png","player.json");
Q.compileSheets("crates.png","crates.json");
Q.animations("player", {
walk_right: { frames: [0,1,2,3,4,5,6,7,8,9,10], rate: 1/15, flip: false, loop: true },
jump_right: { frames: [13], rate: 1/10, flip: false },
stand_right: { frames:[14], rate: 1/10, flip: false },
duck_right: { frames: [15], rate: 1/10, flip: false },
});
Q.stageScene("level1");
Q.stageScene('hud', 3, Q('Player').first().p);
});
});
答案 0 :(得分:0)
想出来 - 非常感谢Alex K提出的建议。创建另一个变量以在初始化时存储健康值,然后将其输入到减法计算中。现在更新它应该。