请检查我的代码,它不起作用。并且总是说它不是控制台中的功能

时间:2017-03-04 12:58:25

标签: javascript

我用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()

1 个答案:

答案 0 :(得分: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

提到的最后一个括号丢失了