整理岩纸剪刀游戏

时间:2015-10-21 08:28:49

标签: actionscript-3

虽然代码不会显示任何错误,但代码不起作用。我正在尝试用3个按钮和一个计分器构建一个石头剪刀游戏。我按下按钮但屏幕上没有任何变化。

播放器1是用户得分的文本框,播放器2是计算机得分的文本框。

var options: Array = [
"rock",
"paper",
"scissors"
];
var cp: String;
var res: String;
var score1: uint = 0;
var score2: uint = 0;
rockbtn.addEventListener(MouseEvent.CLICK, function (ev) {
computerturn("rock");
});

paperbtn.addEventListener(MouseEvent.CLICK, function (ev) {
computerturn("paper");
});

scissorsbtn.addEventListener(MouseEvent.CLICK, function (ev) {
computerturn("scissors");
});
function computerturn(player: String) {
cp = options[Math.floor(Math.random() * 3)];
computer.text = "Computer played " + cp;
if (cp == player) {
    res = "TIE";
    result.text = "TIE";
}
if ((cp == "rock") && (player == "paper")) {
    res = "Player wins: paper covers rock.";
    result.text = res;
    score1 += 1;
    Player1.text = score1.toString();
}
if ((cp == "rock") && (player == "scissors")) {
    res = "Computer wins: rock breaks scissors";
    result.text = res;
    score2 += 1;
    Player2.text = score2.toString();
}
if ((cp == "paper") && (player == "scissors")) {
    res = "Player wins: scissors cuts paper.";
    result.text = res;
    score1 += 1;
    Player1.text = score1.toString();
}
if ((cp == "paper") && (player == "rock")) {
    res = "Computer wins: paper covers rock.";
    result.text = res;
    score2 += 1;
    Player2.text = score2.toString();
}
if ((cp == "scissors") && (player == "rock")) {
    res = "Player wins: rock breaks scissors.";
    result.text = res;
    score1 += 1;
    Player1.text = score1.toString();
}
if ((cp == "scissors") && (player == "paper")) {
    res = "Computer wins: scissors cuts paper.";
    result.text = res;
    score2 += 1;
    Player2.text = score2.toString();
    }
}
result.text = res;
computer.text = "Computer played " + cp;

这是我基于它的代码:

var options:Array= [
"rock",
"paper",
"scissors"
];
var cp:String;
var res:String;
function computerturn(player:String) {
//trace("player played "+player);

cp = options[Math.floor(Math.random()*3)];
//trace("computer played "+cp);
computer.text = "Computer played "+cp;
if (cp==player) {
    res = "TIE";
    result.text = "TIE";
}
if ((cp=="rock")&&(player=="paper")) {
    res="Player wins: paper covers rock.";
    result.text = res;
}
if ((cp=="rock")&&(player=="scissors")) {
    res="Computer wins: rock breaks scissors";
    result.text = res;
    gotoAndPlay("rockbreaks");
}
if ((cp=="paper")&&(player=="scissors")) {
    res="Player wins: scissors cuts paper.";
    result.text = res;
}
if ((cp=="paper")&&(player=="rock")) {
    res="Computer wins: paper covers rock.";
    result.text = res;
}
if ((cp=="scissors")&&(player=="rock")) {
    res="Player wins: rock breaks scissors.";
    result.text = res;
    gotoAndPlay("rockbreaks");
}
if ((cp=="scissors")&&(player=="paper")) {
    res="Computer wins: scissors cuts paper.";
    result.text = res;
}
}

rockbtn.addEventListener(MouseEvent.CLICK, function (ev) {
computerturn("rock");}  );

paperbtn.addEventListener(MouseEvent.CLICK, function (ev) {
computerturn("paper");}  );

scissorsbtn.addEventListener(MouseEvent.CLICK, function (ev) {
computerturn("scissors");}  );
stop();

0 个答案:

没有答案