Search code examples
actionscript-3kongregate

AS3 Check if movieclips inside array are all the same color


I've created a simple puzzle game and have uploaded it to kongregate, now I want to upload highscores (the least amount of moves = better) to it using their API. To make sure no one can cheat the system (submitting the score before puzzle is finished) I need to make sure that none of the pieces in the puzzle are black. All the pieces of the puzzle are movieclips and are inside a array called buttons.

I've currently got this:

    public function SumbitScore(e:MouseEvent)
    {
        for (var v:int = 0; v < buttons.length; v++)
        {
            if (buttons[v].transform.colorTransform.color != 0x000000)
            {
                _root.kongregateScores.submit(1000);
            }
        }
    }

but I think that will submit the score as soon as it checks a movieclip that is not black and it'll ignore the rest.


Solution

  • I think the way to go is to keep track of whether or not an 'empty button' is found in your for-loop. After the loop you could submit the score if no empty tiles were found or let the player know the puzzle has to be completed before submitting.

    I've added some comments in the code below:

    // (I changed the function name 'SumbitScore' to 'SubmitScore')
    public function SubmitScore(e:MouseEvent)
    {
        // use a boolean variable to store whether or not an empty button was found.
        var foundEmptyButton : Boolean = false;
        for (var v:int = 0; v < buttons.length; v++)
        {
            // check whether the current button is black
            if (buttons[v].transform.colorTransform.color == 0x000000)
            {
                // if the button is empty, the 'foundEmptyButton' variable is updated to true.
                foundEmptyButton = true;
                // break out of the for-loop as you probably don't need to check if there are any other buttons that are still empty.
                break;
            }
        }
        if(foundEmptyButton == false)
        {
            // send the score to the Kongregate API
            _root.kongregateScores.submit(1000);
        }
        else
        {
            // I'd suggest to let the player know they should first complete the puzzle
        }
    }
    

    Alternatively you could let the player know how many buttons he still has to finish:

    public function SubmitScore(e:MouseEvent)
    {
        // use an int variable to keep track of how many empty buttons were found
        var emptyButtons : uint = 0;
        for (var v:int = 0; v < buttons.length; v++)
        {
            // check whether the current button is black
            if (buttons[v].transform.colorTransform.color == 0x000000)
            {
                // if the button is empty increment the emptyButtons variable
                emptyButtons++;
                // and don't break out of your loop here as you'd want to keep counting
            }
        }
        if(emptyButtons == 0)
        {
            // send the score to the Kongregate API
            _root.kongregateScores.submit(1000);
        }
        else
        {
            // let the player know there are still 'emptyButtons' buttons to finish before he or she can submit the highscore
        }
    }
    

    Hope it's all clear.

    Good luck!