Javascript 请检查我的代码,它不工作。并且总是说它不是控制台中的函数
我用javascript创建了一个游戏,但是这个游戏不起作用。它总是说它不是Chrome日志中的函数 所以我想知道这有什么问题。像刘备。战斗等等。这是一个游戏,你打曹操。我创造了3个角色。如果刘备还活着,游戏就结束了。如果曹操不在,你就赢了Javascript 请检查我的代码,它不工作。并且总是说它不是控制台中的函数,javascript,Javascript,我用javascript创建了一个游戏,但是这个游戏不起作用。它总是说它不是Chrome日志中的函数 所以我想知道这有什么问题。像刘备。战斗等等。这是一个游戏,你打曹操。我创造了3个角色。如果刘备还活着,游戏就结束了。如果曹操不在,你就赢了 var character = function(name,power,hp){ this.name=name; this.power=power; this.hp= hp; this.alive=true; this
var character = function(name,power,hp){
this.name=name;
this.power=power;
this.hp= hp;
this.alive=true;
this.checkalive = function(){
if(this.hp <=0){
this.alive==false;
}
this.fight=function(){
var attack = Math.random()
if(attack < 0.5){
caocao.hp-=this.power;
caocao.checkalive();
checkwin()
}
else{
console.log("miss attack")
}
}
this.fullattack=function(){
var attack = Math.random()
if(attack < 0.5){
caocao.hp-= 3;
caocao.checkalive();
checkwin()
}
else {
this.hp-=2
this.checkalive()
this.checklose()
}
}
}
}
//*create the character//
var liubei = new character("liubei",1,5);
var guanyu = new character("guanyu",1,5);
var zhangfei = new character("zhangfei",1,5);
var caocao={
name:"caocao",
power: 2,
hp :8,
alive : true,
checkalive: function(){
if(caocao.hp<=0){
caocao.alive===false;
};
},
fight: function(){
var caocaoattack = Math.random()
if(caocaoattack<0.33){
liubei.hp-=2;
liubei.checkalive();
checklose();
}
else if(caocaoattack>0.66){
zhangfei.hp-=2;
zhangfei.checkalive()
}
else{
guanyu.hp-=2
guanyu.checkalive();
}
}
}
//*define the action//
var gameover = false;
var gameOver = function(){
gameover==true;
confirm("gameover");
return;
}
var checkwin = function(){
if(caocao.alive == false ){
confirm("you win the game");
gameOver();
}
}
var checklose = function(){
if(liubei.alive == false)
confirm("you loose the game");
gameOver();
}
//*start game//
var gamestart = function(){
while(gameover==false){
if(liubei.alive==true){
liubei.fight();
}
if(guanyu.alive==true){
guanyu.fight();
}
if(zhangfei.alive==true){
zhangfei.fight();
}
if(caocao.alive==true){
caocao.fight();
}
}
}
gamestart()
最后一个括号丢失了,正如Jaromanda在this.fight之前提到的那样5秒答案-丢失}。如果你格式化了你的代码,你会看到-你使用firefox吗?使用带有漂亮打印按钮的scratchpad:PJSFIDLE有一个整洁的选项——看看你的代码——现在很明显,不是吗
var character = function(name,power,hp){
this.name=name;
this.power=power;
this.hp= hp;
this.alive=true;
this.checkalive = function(){
if(this.hp <=0){
this.alive==false;
}
}
this.fight=function(){
var attack = Math.random()
if(attack < 0.5){
caocao.hp-=this.power;
caocao.checkalive();
checkwin()
}
else{
console.log("miss attack")
}
}
this.fullattack=function(){
var attack = Math.random()
if(attack < 0.5){
caocao.hp-= 3;
caocao.checkalive();
checkwin()
}
else {
this.hp-=2
this.checkalive()
this.checklose()
}
}
}