0

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();
            }
    }
}
  • You use **randomCoin** as a numeric variable first, but then you act as if it is a **MovieClip**. So, if you indeed have a **randomCoin** as a **MovieClip** there, you should rename the local variable to something else. – Organis Dec 25 '22 at 13:58
  • How to do make randomCoin as a numeric variable as a MovieClip? – Isaac Yeap Jie Ling Dec 25 '22 at 15:13
  • Sorry, what you are asking makes no sense. A numeric variable holds a value like **int**, **uint** or **Number**, and it is not a **MovieClip**. A **MovieClip** variable is not numeric, it contains a reference to a **MovieClip** object. If your **randomCoin** is an actual **MovieClip** (designed on timeline, with the instance name of **randomCoin**), you should leave it as is, and make another variable to define a random frame: **var aFrame:int = Math.random() * coinblock.length + 1;** – Organis Dec 25 '22 at 17:58
  • The script is changed. Then, Randomize the frame than 0? Is it Math.random() * coinblock.length + 1? – Isaac Yeap Jie Ling Dec 26 '22 at 01:31
  • So, what is the problem now? Did the error go away? – Organis Dec 26 '22 at 22:54
  • No errors. I need to randomize the nested symbols to random the number of frames. E.g. coinblock1 is a MovieClip. There are 6 frames inside the nested symbol. – Isaac Yeap Jie Ling Dec 27 '22 at 07:45
  • 1
    Ah! The **MovieClip** class doesn't have a **length** property, you should put it as the following: **random1 = Math.random() * coinblock1.totalFrames + 1;** You don't need to **Math.floor(...)** it because the variable is **uint** and the result is auto-converted. – Organis Dec 27 '22 at 14:33
  • Ok. What the ".currentFrame" for the random? – Isaac Yeap Jie Ling Dec 28 '22 at 09:03
  • Not sure what are you asking. The rest of script is *logically* correct. You tell a **MovieClip** to **gotoAndPlay(...)** and that changes its **currentFrame**. – Organis Dec 28 '22 at 12:10
  • Ok. So, what about this? coinblock1.gotoAndStop(random1); if(coinblock1.currentFrame == 2){ Coins += 1; CoinSFX.play(); } else if(coinblock1.currentFrame == 3){ Coins += 5; CoinSFX.play(); } else if(coinblock1.currentFrame == 4){ Coins += 10; CoinSFX.play(); } else if(coinblock1.currentFrame == 5){ Coins += 50; CoinSFX.play(); } else if(coinblock1.currentFrame == 6){ Coins += 100; CoinSFX.play(); } else if (coinblock1.currentFrame == 1){ Bump.play(); } – Isaac Yeap Jie Ling Dec 29 '22 at 07:06
  • @IsaacYeapJieLing It doesn't make sense to ask _"what about this?"_... You did not say what you want to know, so what answer do you expect here? Test the code and if there's any problems, then [update your question](https://stackoverflow.com/posts/74913206/edithttps://stackoverflow.com/posts/74913206/edit) with the new code and new error so we can try to see a possible solution. – VC.One Dec 29 '22 at 11:22
  • @IsaacYeapJieLing _"What the `.currentFrame` for the random?"_ Are you asking what number is **random1**? If yes, use `trace ( "random1 is : " + random1);` then you can know which frame number the `coinblock1.gotoAndStop( random1 );` is going to... Where it stops is the new `coinblock1.currentFrame` (frame number is same number as **Random1**)... – VC.One Dec 29 '22 at 11:28
  • Yes. Each frame in a coin block nested symbol is 0, 1, 5, 10, 50 and 100 coins to randomize it in a current frame. – Isaac Yeap Jie Ling Dec 29 '22 at 13:27

1 Answers1

0

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

    }
}
VC.One
  • 14,790
  • 4
  • 25
  • 57
  • PS: I cannot test AS3 code so tell me if my code example is doing what you needed... – VC.One Dec 30 '22 at 13:10
  • I have tested. but the coin block makes different sounds: 1. get a coin with a bump sound and no coins added. 2. hit a block no coins with a coin sound. – Isaac Yeap Jie Ling Jan 01 '23 at 04:51
  • _"I have tested. but the coin block makes different sounds:"_ Yes that is what your code does. You can see you have `CoinSFX.play();` and `Bump.play()` for different sounds... I don't understand your point **1.**, how can it be **get a coin** but at same time is also **no coins added**? I cannot explain your point **2.** (`hit a block no coins with a coin sound`). You have `Coins +=` so the code should be adding coins. What is _"hit a block no coins"_ meaning? I ask again **tell me if my code example is doing what you needed.**. If not correct, then you must also tell **what should happen**. – VC.One Jan 01 '23 at 21:57
  • 1
    One of the coin blocks have hit and reached Frame 1 in a nested symbol but it makes a coin sound instead of a bump sound – Isaac Yeap Jie Ling Jan 02 '23 at 02:16
  • OK I now understand better what you are meaning. So the problem is: You touch `coinblock1`, and then the trace says `"random1 is : 1"`, and then you see `coinblock1` going to frame 1, but the problem now is that the Sound is `CoinSFX.play();` instead of being `Bump.play()`? .. Things you can try to fix: **(1)** Make sure there is no code on Frame 1 of `coinblock1` to play other Sound. **(2)** Check in Library that your Bump and CoinSFX are using correct sound files. Maybe you got wrong Var names to the Sounds (like saying `Bump = coin_sound.mp3` so later your `bump.play()` gives a coin sound?) – VC.One Jan 02 '23 at 09:25
  • var CoinSFX: Coin = new Coin(); var Bump: brickbump = new brickbump(); They are already matched the correct sounds and they are in a library. – Isaac Yeap Jie Ling Jan 03 '23 at 07:35
  • Okay... **(1)** I don't know what is `new Coin` in your `var CoinSFX: Coin = new Coin();` I mean is that a Sound (MP3) object? or maybe it is a MovieClip object with frames?... Also I don't understand about the command `CoinSFX.play` is that one telling a **Sound** to play or is that one telling a **MovieClip** to play? **(2)** Your question is about **Randomize Nested symbols for coin block** and that is what **must be fixed first**. If **random frame** is working then this question is Answered correctly. If there is a new problem about sound then ask a new question about Sound. – VC.One Jan 03 '23 at 10:17