2014-01-25 47 views
0

我正在使用Flash動作腳本創建一個「骰子微調」遊戲3,規則是如果玩家在第一次擲出7或11時先擲出然後他們獲勝,但是如果用戶擲出2,3或第一次扔12,然後他們輸了。如果第一次投擲是4,5,6,8,9或10,則該總和成爲點,並且該用戶必須通過再次滾動直到獲得點(他們獲勝)或他們投擲7(他們輸了)在AS3中重置得分

我已經成功地創建了它的旋轉函數,在每個「骰子」上產生一個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; 
} 
+0

如果不是第一回合,我不會得到失敗/勝利條件。玩家應該贏得什麼?總和應該是多少? – Vesper

+0

如果是第一次「投擲」,總和爲7或11,則用戶獲勝。 如果是第一次投擲並且總數是2,3或12,則用戶輸掉。 如果在第一次投擲時,總和爲4,5,6,8,9或10,那麼該總和就成爲「點」,然後用戶必須在下一次投擲時「製造」該點。爲了說明這一點,用戶必須投入相同的金額。如果沒有,用戶繼續投擲,直到他們獲得分數(他們贏了),或者他們擲出7(他們輸了) – user3235027

回答

0

您需要檢查點和addPoints之前你加分的球員,而你的代碼檢查pointsif (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編譯器不嚴格要求這一點,它也可以幫助您避免一些非常奇怪的錯誤。