randomize Nested symbols for coin blocks

94 Views Asked by At

I have a MovieClip object called coinblock1.

Problem:
Coinblock1 has 6 frames and I want to choose a random frame number from this MC object.
I want to use this number to later update some other variables by using an IF statement.
Variables to update include the changing of amount of coins earned by the player.

My frame numbers are arranged like this:

frame 1 = 0 coins
frame 2 = 1 coins 
frame 3 = 5 coins
frame 4 = 10 coins
frame 5 = 50 coins
frame 6 = 100 coins 

How to randomize the range for following frames in the coin block?
I have used the if...else statement and gotoAndStop in a currentFrame.

My Code:

if(coinblock1.hitTestObject(blowfishPong) == true)
{
    if (ballSpeedXCoinGame > 0) 
    {
        ballSpeedXCoinGame *= -1;
        ballSpeedYCoinGame = calculateBallAngleCoinGame(character.y, blowfishPong.y);
        var random1: uint = 0;
        random1 = Math.random() * coinblock1.totalFrames;       
        if (random == 1){
                coinblock1.gotoAndStop(1);
                Bump.play();
            }
            else{
                if(random == 2){
                    Coins += 1;
                    coinblock1.gotoAndStop(2);
                }
                if(random == 3){
                    Coins += 5;
                    coinblock1.gotoAndStop(3);
                }
                if(random == 4){
                    Coins += 10;
                    coinblock1.gotoAndStop(4);
                }
                if(random == 5){
                    Coins += 50;
                    coinblock1.gotoAndStop(5);
                    }
                if(random == 6){
                    Coins += 100;
                    coinblock1.gotoAndStop(6);
                    }
                updateTextFieldsCoinGame();
                CoinSFX.play();
            }
    }
}
1

There are 1 best solutions below

7
VC.One On

My understanding is you want to get a random number and then use it in an IF statement to control which frame the coinblock1 goes to and also to update the coins amount.

For that, try something like this logic below:

if(coinblock1.hitTestObject(blowfishPong) == true)
{
    if (ballSpeedXCoinGame > 0) 
    {
        ballSpeedXCoinGame *= -1;
        ballSpeedYCoinGame = calculateBallAngleCoinGame(character.y, blowfishPong.y);
        
        //# ::: Step 1 ::: first you define a variable (it has value of zero)
        var random1: uint = 0;
        
        //# then you update the variable (with some random value)
        random1 = ( Math.random() * coinblock1.totalFrames ); 
        
        //# finally you check what the new updated value is now
        trace( "random1 is : " + random1 );
        
        //# ::: Step 2 ::: update coins amount and then change coinblock's frame number
       
        if( random1 == 1 ) //# will do this part if random1 is 1..
        {
            coinblock1.gotoAndStop(1);
            Bump.play();
        }
        
        else //# will do this part if random1 is NOT 1..
        {
            if( random1 == 2 )
            {
                Coins += 1;
                coinblock1.gotoAndStop(2);  
            }

            if( random1 == 3 )
            {
                Coins += 5;
                coinblock1.gotoAndStop(3);
            }

            if( random1 == 4 )
            {
                Coins += 10;
                coinblock1.gotoAndStop(4);
            }

            if( random1 == 5 )
            {
                Coins += 50;
                coinblock1.gotoAndStop(5);
            }

            if( random1 == 6 )
            {
                Coins += 100;
                coinblock1.gotoAndStop(6);  
            }
            
            //# after the IF's... now update the in-game text and also play a sound
            updateTextFieldsCoinGame();
            CoinSFX.play();
        
        } //# end of ELSE part

    }
}