Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to randomize movieclips?

Guest
Dec 02, 2017 Dec 02, 2017

Hi, I am new with Actionscript 3. I'm creating a Trading Card Game, but I want to generate a random card (from a specific deck).

We suppose we have four places, and these places will be filled with the four random cards. And I don't know if the cards should be in a movieclip but in differents frames or in differents movieclips.

I do not know how to use Math Random or Array. That's why I need a detailed explanation.

And, could you tell me how to do an action depending what card you got?

I would be very grateful with you if you can help me.

TOPICS
ActionScript
791
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 02, 2017 Dec 02, 2017

Hi.

I tried to keep things simple, but dynamic at the same time.

As you getting started with AS3, I think using frames is the easiest way. No special properties or classes.

FLA download: Here.

Code:

/*

1 - The function 'getRandomFaces' return an array with the amount of numbers that we set. In this case,

each card has 10 frames, so it will fill an array with numbers from 1 to 10.

2 - The 'randomSort' function is used as a parameter to the default array.sort() function, so we can get a random order from the original

array. See more at: https://help.adobe.com/pt_BR/FlashPlatform/reference/actionscript/3/Array.html#sort().

3 - The 'setFaces' function gets a randomized array and a deck Movie Clip and set the currentFrame property of each card

to the current value of the array inside the loop.

4 - The 'callActions' function call a corresponding function in this frame based on the currentFrame property of the sorted cards.

*/

function getRandomFaces(total:uint):Array

{

    var array:Array = [];

   

    for (var i:uint = 0; i < total; i++)

        array = i + 1;   

   

    return array.sort(randomSort);

}

function randomSort(a:*, b:*):Number

{

    if (Math.random() < 0.5)

        return -1;

    else

        return 1;

}

function setFaces(array:Array, deck:MovieClip):void

{

    for (var i:uint = 0;i < deck.numChildren; i++)

        MovieClip(deck.getChildAt(i)).gotoAndStop(array);

       

}

function callActions(deck:MovieClip):void

{

    for (var i:uint = 0; i < deck.numChildren; i++)

        this["action" + MovieClip(deck.getChildAt(i)).currentFrame]();

}

function action1():void {trace("action 1 executed");}

function action2():void {trace("action 2 executed");}

function action3():void {trace("action 3 executed");}

function action4():void {trace("action 4 executed");}

function action5():void {trace("action 5 executed");}

function action6():void {trace("action 6 executed");}

function action7():void {trace("action 7 executed");}

function action8():void {trace("action 8 executed");}

function action9():void {trace("action 9 executed");}

function action10():void {trace("action 10 executed");}

setFaces(getRandomFaces(deck.card0.totalFrames), deck);

callActions(deck);

trace(""); // set a space between each execution in the Output panel.

I hope it helps.

Regards,

JC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 02, 2017 Dec 02, 2017

Thank you very much. I'd like to open that .fla file to understand better, but I use Flash CS3. Can you save it for previous version? Sorry for the disturbances.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 03, 2017 Dec 03, 2017
LATEST

Hi again.

No problem.

It seems that Animate CC won't give me legacy options for FLA files. The only option is to save as a .xfl (Animate Uncompressed File). I've updated the previous link with this .xfl.

If you still cannot open the file, don't worry, because the FLA has a very simple setup.

- There is a main Movie Clip on the stage called 'deck' in a layer called 'cards'. The code above is located on a layer above called 'actions'.

- There are four Movie Clips named 'card0' to 'card3' inside this 'deck' Movie Clip.

- Each of this 'cardn' Movie Clips has 10 frames with text fields from 1 to 10, a actions layer with a stop() function and a single shape on a layer on the bottom.

Like this:

animate_cc_random_cards_01.png

animate_cc_random_cards_02.png

animate_cc_random_cards_03.png

animate_cc_random_cards_04.png

animate_cc_random_cards_05.png

animate_cc_random_cards_06.png

Please tell me if you still have any questions.

Regards,

JC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines