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

Randomize Buttons on stage

New Here ,
Jul 16, 2012 Jul 16, 2012

Copy link to clipboard

Copied

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.

TOPICS
ActionScript

Views

2.0K

Translate

Translate

Report

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 ,
Jul 16, 2012 Jul 16, 2012

Copy link to clipboard

Copied

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.x = 50+button.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

;

        a

= t;

    }

}

Votes

Translate

Translate

Report

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
New Here ,
Jul 16, 2012 Jul 16, 2012

Copy link to clipboard

Copied

I tried your formula, no luck. i changed the "a" in your formula, to button the name of the array and still no success. So  here is what i have now.

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.x = 50+button.width*i;

}


}

function shuffle(button:Array) {

    var p:int;

    var t:*;

    var ivar:int;

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

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

        t = button[ivar];

        button[ivar] = button

;

        button

= t;

    }

}

again this is the actions layer, also the buttons were exported for actionscripting. I think im still doing something wrong or maybe, i inserted the wrong labels.

Votes

Translate

Translate

Report

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 ,
Jul 16, 2012 Jul 16, 2012

Copy link to clipboard

Copied

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).x = 50+getChildByName(button).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

;

        a

= t;

    }

}

Votes

Translate

Translate

Report

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
New Here ,
Jul 17, 2012 Jul 17, 2012

Copy link to clipboard

Copied

I have tried this also, there were no errors that showed up however there is no change. the buttons remain on the same place/position on the stage.is there anything perhaps i should be doing differently? 

I did check the publish settings its set to as3, the advanced settings had something about doc class but i have no idea what that is or how to use it.

Votes

Translate

Translate

Report

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 ,
Jul 17, 2012 Jul 17, 2012

Copy link to clipboard

Copied

what are the instance names of your buttons?

Votes

Translate

Translate

Report

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
New Here ,
Jul 18, 2012 Jul 18, 2012

Copy link to clipboard

Copied

my buttons are named choice1-3. i tried to make it as easy as possible for me once i start making the quiz i will change the name but i also know i have to change the code.

Votes

Translate

Translate

Report

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 ,
Jul 18, 2012 Jul 18, 2012

Copy link to clipboard

Copied

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).x = 50+getChildByName(button).width*i;

trace(i,button,getChildByName(button.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

;

        a

= t;

    }

}

Votes

Translate

Translate

Report

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
New Here ,
Jul 19, 2012 Jul 19, 2012

Copy link to clipboard

Copied

The code you have gave no out put and also generated no errors. Still the same result.

Votes

Translate

Translate

Report

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 ,
Jul 19, 2012 Jul 19, 2012

Copy link to clipboard

Copied

then you're not even calling Main

Votes

Translate

Translate

Report

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
New Here ,
Jul 19, 2012 Jul 19, 2012

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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