Search code examples
actionscript-3flash-cs5

ActionScript 3 Scoring/Math


I've been attempting to implement a score inside of a game that I'm creating in AS3. So far, I've managed to create a score system that adds/subtracts points based on actions in-game. I've decided that it'd be simpler to have the scoring system just add points instead of subtract them and on reaching a certain number, end the game.

The problem I'm having is that on the one hand, the game is performing checks to see if the pieces are in the right place. If they are, the player wins. On the other, the counter needs to count and reach a certain number (10) before deciding the player loses. At the moment there's some weird behaviour going on where I'm able to drag the pieces around without putting them in their right place and the counter still goes over 10. I've tried a few variations of changing the math so that it totals differently, but the functionality is the same. What would I have to change so that it would behave as first described?

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.");
    }
}

Solution

  • I think that the logic is a little confusing, but as I understand it from your code, the idea is to move a drag-gable item to the correct x,y position, with a tolerance of "offset"? The aim is to to this with the lowest possible "score" (or number of moves) and if the number of moves (score) is greater than 10 then you lose the game?

    Currently the only place that checks to see if you have made 10 moves is "checkGame" and this method is only called if your "apple" is correctly positioned. If it is incorrectly positioned then the number of moves is incremented, but the score is not checked. So when you finally get to "checkGame" but correctly positioning the "apple" the score could already be greater than 10. So your "score == 10" check will fail also.

    So what you need is to check the game on every move with something like this:

    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.
         }
    }