在AS3中重置分数

时间:2014-01-25 12:21:05

标签: actionscript-3 reset points

我正在使用Flash动作脚本3创建一个“骰子微调器”游戏,规则是如果玩家在第一次投掷时掷出7或11然后他们赢了但是如果用户在第一次掷出2,3或12扔然后他们输了。如果第一次投掷是4,5,6,8,9或10,则该总和成为该点,并且该用户必须再次滚动直到他们获得该点(他们获胜)或者他们投掷7(他们输了)

我已成功创建了旋转的功能,在每个“骰子”之间生成1-6之间的随机数,并从总数中添加点数,但我的问题是用户是赢还是输。当用户从上次尝试重新滚动

时,我无法弄清楚如何将点重置为零

代码很糟糕。

import flash.events.MouseEvent;

spinner_mc.stop();
spinner2_mc.stop();

roll_btn.addEventListener(MouseEvent.CLICK, startspin);
stop_btn.addEventListener(MouseEvent.CLICK, stopspin);
spinner_mc.addEventListener(Event.ENTER_FRAME, spin);
spinner2_mc.addEventListener(Event.ENTER_FRAME, spin)

var spinning = false

function spin(evt){
    if(spinning){
        spinner_mc.rotation += 5
        spinner2_mc.rotation += 5
    }
} //end spin function

function startspin(mEvt:MouseEvent){
    spinning = true

} //end startspin function

var points:int = 0;
function stopspin (mEvt){
    if (spinning){
        spinning = false
        var nextVal = newVal()
        var nextVal2 = newVal2()
        var addPoints = nextVal + nextVal2
        points += nextVal + nextVal2
        spinner_mc.gotoAndStop(nextVal)
        spinner_mc.rotation = 0
        spinner2_mc.gotoAndStop(nextVal2)
        spinner2_mc.rotation = 0
        value_txt.text = "point = " + points + ", new value " + addPoints
    }
    if ((points == 0) && (addPoints == 7) || (addPoints == 11))
    {
        info_txt.text = "You win!! You rolled a 7 or 11 on first throw!"

    }
    if ((points == 0) && (addPoints == 2) || (addPoints == 3) || (addPoints == 12))
    {
        info_txt.text ="You lose! You rolled a 2, 3 or 12 on first throw!"
    }

} // end stopspin function

function newVal(){
    var temp:int;
    temp = Math.random() * 6 + 1;
    return temp;
}

function newVal2(){
    var temp:int
    temp = Math.random() * 6 + 1;
    return temp;
}

1 个答案:

答案 0 :(得分:0)

您需要在向玩家添加积分之前检查点数和addPoints ,而您的代码已在points块之外检查if (spinning)。即使points尚未归零,这也会使您的检查失败。因此,首先获取旋转值,然后检查,然后分配值。

function stopspin(evt:MouseEvent):void {
    if (!spinning) return; // this is shorter than wrapping the entire function
    // body into a single "if (spinning)" block. If we don't spin, get out
    spinning=false;
    var nextVal1:int=newVal();
    var nextVal2:int=newVal(); // no need of two functions doing the same :)
    var addPoints:int=nextVal1+nextVal2;
    spinner_mc.gotoAndStop(nextVal1);
    spinner_mc.rotation = 0;
    spinner2_mc.gotoAndStop(nextVal2);
    spinner2_mc.rotation = 0; // give values to spinners
    // now check phase
    if (points==0) {
        // this means first throw.
        if ((addPoints==7) || (addPoints==11)) {
            // player wins, insert win code
        } else if((addPoints==2)||(addPoints==3)||(addPoints==12)) {
            // player loses, insert lose code
        } else {
            points=addPoints; // otherwise making the player do second throw
            // add other code, say inform the player that he has to throw again
        }
    } else {
        // not a first throw. We have the "point" in "points" variable right now
        if (addPoints==points) {
            // player wins with second throw, add win code
            points=0; // okay, back to first throw
        } else if (addPoints==7) {
            // player loses second throw, add relevant code
            points=0; // again, back to first throw
        } else {
            // else no match, oh well.
        }
    }
}

此外,如果您希望使用单个函数(在您的情况下为spin)监听多个对象,您应该更好地了解如何使用事件侦听器。现在,您可以使用spin功能更新两个微调器,并且每帧调用两次,因此您的旋转器以所需速度的两倍旋转。您可以使用传递给侦听器的Event类对象获取主动侦听影片剪辑,您需要获取其target属性。所以,你的听众可以像这样重写:

function spin(evt:Event):void {
    var mc:DisplayObject=evt.target; // the object that's listening
    mc.rotation+=5; // now turn it.
}

最后,请在每行的末尾放置分号。即使AS3编译器没有严格要求这一点,它也可以避免一些非常奇怪的错误。