2017-03-04 29 views
-4

我用javascript创建了一个游戏,但游戏无法正常工作。它总是说它不是Chrome日志中的功能。请检查我的代码,它不起作用。并总是说它不是控制台中的功能

所以我想知道那有什么问题。像liubei.fight()等。这是一个游戏,你打对caocao。我创造了3个角色。如果liubei不存在,则游戏结束。如果caocao没有活着​​,那么你赢了比赛。

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() 
+3

5秒答复中提到的最后一个支架缺失 - 缺少'}''之前.. this.fight'如果 –

+1

你格式化你的代码,你会看到 - 你使用Firefox吗?用“漂亮打印”按钮使用暂存器:p jsfiddle有一个整齐的选项 - 看看你的代码[这里](https://jsfiddle.net/x0uvnvu3/) - 现在很明显,不是吗? –

回答

0
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() 
     } 
    } 
} 

由Jaromanda

相关问题