2014-10-29 42 views
0

我一直在嘗試在AS3中創建的遊戲中實現一個分數。到目前爲止,我已經設法創建一個分數系統,根據遊戲中的動作增加/減少分數。我已經決定讓評分系統只是加點而不是減去它們並達到一定數量就結束遊戲會更簡單。ActionScript 3得分/數學

我遇到的問題是,一方面,遊戲執行檢查以查看棋子是否在正確的位置。如果是,則玩家獲勝。另一方面,在決定玩家失敗之前,計數器需要計數並達到一定數量(10)。此刻有一些奇怪的行爲正在發生,我可以拖動這些碎片而不會將它們放在正確的位置,並且計數器仍然超過10.我嘗試了一些更改數學的變體,以便總計不同,但功能是一樣的。我需要改變什麼,才能像第一次描述的那樣行事?

stop(); 

//Create the score counter 

import flash.text.TextField; 

var score = 0; 
scorecounter.text = score; 
function init(): void 
{ 
    score = 0; 
    scorecounter.text = "SCORE:" + score.toString(); 
} 

function updateScore(): void 
{ 
    scorecounter.text = ++score; 
} 

function evaluateScore(): void //this is meant to stop the score going below 0 
{ 
    scorecounter.text = --score; 
    if(score < 0) { 
     score -= score; 
    } 
} 

/*Omitted some functions and var's for object positions and events*/ 

function stopDragging(e:MouseEvent):void { 
    e.currentTarget.stopDrag(); 
    switch (e.currentTarget){ 
     case apple: 
      if (apple.x < appleEndX - offset || apple.x > appleEndX + offset || 
       apple.y < appleEndY - offset || apple.y > appleEndY + offset) { 

       apple.x = appleStartX; 
       apple.y = appleStartY; 
       soundOne(); 
       updateScore(); 
      } else { 
       apple.x = appleEndX; 
       apple.y = appleEndY; 
       soundTwo(); 
       updateScore(); 
       checkGame(); 
      } 
      break; 
      //Lots of other cases, using the same method 

      //The end of the game - here, I've been trying to set it to 
      //check whether the player will win or lose 
    } 
} 

function checkGame(): void { 

    if (apple.x == appleEndX && apple.y == appleEndY && pear.x == pearEndX && 
     pear.y == pearEndY && guava.x == guavaEndX && guava.y == guavaEndY && 
     pineapple.x == pineappleEndX && pineapple.y == pineappleEndY && 
     plum.x == plumEndX && plum.y == plumEndY && 
     purple.x == purpleEndX && purple.y == purpleEndY) 
    { 
     trace("You win!"); 
     gotoAndStop(149); 
     soundFive(); 
    } else if (score == 10) { 
     gotoAndStop(150); 
     soundSix(); 
     trace("You lose."); 
    } 
} 
+0

user3293367調用updateScore()兩次:在'if'以及'else'。這就好像你每次都在調用它一樣。 – helloflash 2014-10-29 17:11:12

回答

0

我認爲邏輯是有點混亂,但我從你的代碼的理解,想法是一拖山牆項移動到正確的x,y位置,與一忍「的偏移「?目標是以儘可能低的「得分」(或者移動次數)來實現這個目標,如果移動次數(得分)大於10,那麼你會失去這個遊戲?

目前唯一檢查是否進行了10次移動的地方是「checkGame」,只有當您的「apple」位置正確時纔會調用此方法。如果位置不正確,則移動次數增加,但不檢查分數。因此,當你最終進入「checkGame」但正確定位「蘋果」時,分數可能已經大於10.因此,你的「分數== 10」檢查也會失敗。

所以,你需要的是檢查的東西的一舉一動像這樣的遊戲:

function stopDragging(e:MouseEvent):void { 
    ... 
    switch (e.currentTarget){ 
     case apple: 
      if (apple.x < appleEndX - offset || apple.x > appleEndX + offset || 
       apple.y < appleEndY - offset || apple.y > appleEndY + offset) { 

       apple.x = appleStartX; 
       apple.y = appleStartY; 

       soundOne(); 
      } else { 
       apple.x = appleEndX; 
       apple.y = appleEndY; 

       soundTwo(); 
      } 
     break; 
    ... 
    } 
    //Check the game on every turn. 
    checkGame(); 
} 


function checkGame(){ 

    //Update the score 
    score++; 

    if (apple.x == appleEndX && apple.y == appleEndY && pear.x == pearEndX && 
     pear.y == pearEndY && guava.x == guavaEndX && guava.y == guavaEndY && 
     pineapple.x == pineappleEndX && pineapple.y == pineappleEndY && 
     plum.x == plumEndX && plum.y == plumEndY && 
     purple.x == purpleEndX && purple.y == purpleEndY) 
    { 
      //Do the game win. 
    } 
    else if (score>=10) 
    { 
     //Else if you have a score greater then or equal to 10 do the game lose. 
    } 
} 
+0

這很好用 - 謝謝!我注意到,雖然分數正確更新,但視覺分數不再更新。我是否會將checkGame插入scorecounter.text中以再次使用它? – user3293367 2014-10-30 11:53:39

+0

是的,所以正確的分數++,你可以只添加scorecounter.text =分數; – 2014-10-30 11:58:07