Skip navigation
Currently Being Moderated

Randomize Buttons on stage

Jul 16, 2012 11:34 AM

Tags: #problem #flash #script #text #swf #button #action #code_snippets #actionscript #event #actionscript3

I'm new to flash and coding, I'm attempting to setup a quiz for students who are in elementary school. I have tried quiz creators but they don't do what i want them to do. I've tried captivate but that also doesn't support buttons that have images in them.

 

My project consists of the following.

                                                            the red _____

 

apple.jpg               banana.jpg          orange.jpg

 

The images are buttons which I made with roll over effects, and sound. They also move to the next question when the correct answer is chosen.

What I want is for the Buttons to be randomized when the slide loads, so they are not always in the same spot.

I created a layer called actions for the actionscript so in that layer, I have tried to create an array but I cant seem to make it work properly, the buttons never move. After extensive Google searches on randomization I have the following code.

 

function Main() {

var button:Array = [];
button
.push("choice1");
button
.push("choice2");
button
.push("choice3");
ShuffleArray(button);
trace
(button);

}
function ShuffleArray(button:Array)
{

for (var i:int = button.length-1; i >=0; i--)
{

var randomIndex:int = Math.floor(Math.random()*(i+1));
var itemAtIndex:Object = button[randomIndex];
button
[randomIndex] = button[i];
button
[i] = itemAtIndex;

 

 

Needless to say I have no idea why its not randomizing. I'm sure I'm missing something, or not coded correctly.

 

If someone could please point me in the right direction, or assist with the code I would really appreciate that.

 

Thanks in advance.

 
Replies
  • kglad
    72,220 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 16, 2012 11:39 AM   in reply to jdmrent1

    use:

     

     

    function Main() {

    var button:Array = [];
    button
    .push("choice1");
    button
    .push("choice2");
    button
    .push("choice3");
    shuffle(button);
    for(var i:int=0;i<button.length;i++){

    button[i].x = 50+button[i].width*i;

    }



    }

    function shuffle(a:Array) {

        var p:int;

        var t:*;

        var ivar:int;

        for (ivar = a.length-1; ivar>=0; ivar--) {

            p=Math.floor((ivar+1)*Math.random());

            t = a[ivar];

            a[ivar] = a[p];

            a[p] = t;

        }

    }

     
    |
    Mark as:
  • kglad
    72,220 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 16, 2012 9:39 PM   in reply to jdmrent1

    you should be seeing error messages.  if not, check your publish settings to make sure you're publishing for as3.

     

    then, if you have buttons with instance names "choice1" etc, use:

     

    function Main() {

    var button:Array = [];
    button
    .push("choice1");
    button
    .push("choice2");
    button
    .push("choice3");
    shuffle(button);
    for(var i:int=0;i<button.length;i++){

    getChildByName(button[i]).x = 50+getChildByName(button[i]).width*i;

    }



    }

    function shuffle(a:Array) {

        var p:int;

        var t:*;

        var ivar:int;

        for (ivar = a.length-1; ivar>=0; ivar--) {

            p=Math.floor((ivar+1)*Math.random());

            t = a[ivar];

            a[ivar] = a[p];

            a[p] = t;

        }

    }

     
    |
    Mark as:
  • kglad
    72,220 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 17, 2012 9:43 PM   in reply to jdmrent1

    what are the instance names of your buttons?

     
    |
    Mark as:
  • kglad
    72,220 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 18, 2012 10:56 PM   in reply to jdmrent1

    copy and paste the trace output from:

     

    function Main() {

    var button:Array = [];
    button
    .push("choice1");
    button
    .push("choice2");
    button
    .push("choice3");
    shuffle(button);
    for(var i:int=0;i<button.length;i++){

    getChildByName(button[i]).x = 50+getChildByName(button[i]).width*i;

    trace(i,button[i],getChildByName(button[i].name)

    }



    }

    function shuffle(a:Array) {

        var p:int;

        var t:*;

        var ivar:int;

        for (ivar = a.length-1; ivar>=0; ivar--) {

            p=Math.floor((ivar+1)*Math.random());

            t = a[ivar];

            a[ivar] = a[p];

            a[p] = t;

        }

    }

     
    |
    Mark as:
  • kglad
    72,220 posts
    Jul 21, 2002
    Currently Being Moderated
    Jul 19, 2012 9:07 PM   in reply to jdmrent1

    then you're not even calling Main

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 19, 2012 9:58 PM   in reply to jdmrent1

    um i'm new 2 this too,but maybe you could try something like

     

    trace("this is working");

     

    to make sure if the problem's in the code itself or if it's something else

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points