2014-07-24 40 views
0

我正在做一个石头剪刀游戏。这是迄今为止我所拥有的。我遇到函数determineOutcome有问题。我试图让结果以石头,纸张或剪刀的形式返回计算机的结果。不是randomNumber(),1,2或3.我真的很感谢你的帮助。这是我的代码:岩石剪刀ActionScript 3.0

btnPlay.addEventListener(MouseEvent.CLICK, playGame); 
btnNewGame.addEventListener(MouseEvent.CLICK, newGame); 

var ties:int = 0; 
var wins:int = 0; 
var losses:int = 0; 

function playGame(e:MouseEvent):void 
{ 
    var userThrow:int; 
    var computerThrow:int; 
    var outcome:String; 

    userThrow = int(radRock.group.selectedData); 
    computerThrow = randomWholeNumber(3,1); 
    outcome = determineOutcome(userThrow, computerThrow); 

    lblOutcome.text = outcome; 
    lblScore.text = "Wins: " + wins.toString() + " Losses: " + losses.toString() + "Ties: " + ties.toString(); 
} 


function newGame(e:MouseEvent):void 
{ 
    // 
} 


function randomWholeNumber(highNumber:int,lowNumber:int):int 
{ 
    return Math.floor((highNumber - lowNumber + 1) * Math.random() + lowNumber); 
} 


function determineOutcome(u:int,c:int):String 
{ 
    if(u == 2 && c == 1){ 
     return "The computer threw a rock and your paper covered it. You Win!" 
    } 

    else if(u == 3 && c == 2){ 
     return "The computer threw paper and your scissors cut the paper.You Win!" 
    } 

    else if(u == 1 && c == 3){ 
     return "The computer threw scissors and your rock smashed it. You Win!" 
    } 

    else if(u == c){ 
     return "You tied with the computer" 
    } 

    else if(u == 1 && c == 2){ 
     return "you threw a rock and the Computer's paper covered it. You lose :(" 
    } 

    else if(u == 2 && c == 3){ 
     return "You threw paper and the Computer's scissors cut your paper.You lose :(" 
    } 

    else if(u == 3 && c == 1){ 
     return "You threw scissors and the Computer's rock smashed it. You lose :(" 
    } 
} 

回答

0

此函数会抛出一个编译器错误,因为它默认不会返回一个值。我的意思是,除了条件语句之外,没有返回语句。如果没有满足其中一个条件,您总是需要外部的退货声明。最后,只需添加返回“”;或返回null;

+0

我也遇到了newgame函数的麻烦。我希望玩家按下新的游戏按钮,并让标签分数清除所有附加分数,但分数并未保持在第一位。我将如何获得分数保存。 – user3870380

+0

使用此代码,您必须根据结果添加分数。在适当的括号内写入(在return语句之前)'loss ++'或'wins ++'等等。这会将变量加1。 如果您想重置它,只需将这些变量设置为零。 – MasterRoro

+0

工作。但我仍然对函数newGame()有点困惑。变量已经设置为0。 – user3870380