PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] radio button with array
A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: [RESOLVED] radio button with array

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    47

    resolved [RESOLVED] radio button with array

    i am making a random question quiz with 75 questions

    I'm using a random number int to get a number between 1 and 75 and use that number to find an image and 3 statements with it (images are called 1.jpg 2.jpg 3.jpg....... and i get the statements from XML files 1.xml 2.xml 3.xml.......)

    i get the right image and the right statements but when i want to check if someone answered correctly my script doesn't work.

    i have an array for the images, the xml files and the answers but only the answers array doesn't work correctly.

    here is the array:
    PHP Code:
    var answers:Array = [stelling_2,stelling_3,stelling_1,stelling_2,stelling_1,stelling_2,stelling_2,stelling_2,stelling_3,stelling_3,stelling_1,stelling_2,stelling_3,stelling_2,stelling_1,stelling_1,stelling_3,stelling_2,stelling_1,stelling_1,stelling_2,stelling_3,stelling_3,stelling_1,stelling_3,stelling_2,stelling_1,stelling_2,stelling_2,stelling_2,stelling_1,stelling_3,stelling_2,stelling_1,stelling_3,stelling_1,stelling_3,stelling_3,stelling_2,stelling_3,stelling_1,stelling_2,stelling_3,stelling_1,stelling_1,stelling_3,stelling_1,stelling_1,stelling_2,stelling_2,stelling_1,stelling_3,stelling_1,stelling_2,stelling_1,stelling_2,stelling_3,stelling_1,stelling_3,stelling_2,stelling_2,stelling_2,stelling_3,stelling_1,stelling_3,stelling_2,stelling_2,stelling_2,stelling_1,stelling_3,stelling_1,stelling_3,stelling_3,stelling_2,stelling_3]; 
    the radio buttons are called stelling_1, stelling_2 and stelling_3

    to get the image i use this:
    PHP Code:
    var request:URLRequest = new URLRequest(images[randomnumber]);

    loader.101;
    loader.190;
    addChild(loader);
    loader.load(request); 
    and for the XML files i use this:
    PHP Code:
    var XMLLoader:URLLoader = new URLLoader();
        
    XMLLoader.load(new URLRequest(stellingen[randomnumber]));

    var 
    embeddedFont:Font = new MyFont();
            var 
    textFormat:TextFormat = new TextFormat();
            var 
    myXML:XML = new XML(e.target.data);
            
    textFormat.font embeddedFont.fontName;
            
    textFormat.size 11;
            
    textFormat.color 0xFFFFFF;
            
    stelling_1_text.setStyle("textFormat"textFormat);
            
    stelling_1_text.setStyle("embedFonts"true);
            
    stelling_2_text.setStyle("textFormat"textFormat);
            
    stelling_2_text.setStyle("embedFonts"true);
            
    stelling_3_text.setStyle("textFormat"textFormat);
            
    stelling_3_text.setStyle("embedFonts"true);
            
    stelling_1_text.text myXML.stelling1.text;
            
    stelling_2_text.text myXML.stelling2.text;
            
    stelling_3_text.text myXML.stelling3.text
    and i get the right image with the right text

    to check if the right one is selected i use:
    PHP Code:
    if (answers[randomnumber].selected)
        { 
    but it isn't consistent for example if i get image number 1 i need to select stelling_2 and it says its correct but when i run it again and i get the same question i have a chance it will says its wrong!

    i also use splice to remove questions that are already answered from the arrays:
    PHP Code:
    images.splice(randomnumber1);
        
    stellingen.splice(randomnumber1);
        
    answers.splice(randomnumber1); 
    and this is the random number script:
    PHP Code:
    var randomnumber:int = (Math.floor(Math.random()*(1+high-low))+low); 
    and the vars high and low are this:
    PHP Code:
    var high:int answers.length;
        var 
    low:int 1
    if anyone needs more info just ask

    and thanks in advance for the help

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your random selection is incorrect. Pretend answers has one thing in it. Then high == low == 1. 1 + 1 - 1 = 1, so Math.random()*1 is going to give you something between 0 and 1, then you add 1 to it, getting something between 1 and 2. Then you floor that, getting 1. There is nothing at index 1 in your arrays, because they only have 1 item, and you get bad behavior.

    Remove the 1+ part, and make low 0. Since low is 0, you can simply remove it from the calculation.

    Or, since you foolishly started your indices at 1 for everything except answers, just check and splice answers[randomnumber -1] instead. Or even hackier, add an initial meaningless value as the first value of answers which will never be selected.

  3. #3
    Member
    Join Date
    Jan 2011
    Posts
    47
    it doesn't work i edited the random number calculation its now:
    PHP Code:
    var high:int answers.length;
    var 
    low:int 0;
    var 
    randomnumber:int = (Math.floor(Math.random()*(high-low))+low
    but still it doesn't work

    i also tried the [randomnumber -1] but still not working

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Those were alternatives. Do one or the other, not both.

    If you change low to 0 like that, then the random number won't work for selected your images and xmls, because they go from 1 to n. Your answers go from 0 to n-1. That's the crux of your problem.

  5. #5
    Member
    Join Date
    Jan 2011
    Posts
    47
    i meant i did the [randomnumber -1] separately

    at first i just changed
    PHP Code:
    var high:int answers.length;
    var 
    low:int 0;
    var 
    randomnumber:int = (Math.floor(Math.random()*(high-low))+low
    but changing only that had no results

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Change low back to 1, but keep the revised calculation without the "1+". Then, change your answers array to have the dummy first entry.
    Code:
    var answers:Array = [null, stelling_2,stelling_3,stelling_1,stelling_2,stelling_1,stelling_2,stelling_2,stelling_2,stelling_3,stelling_3,stelling_1,stelling_2,stelling_3,stelling_2,stelling_1,stelling_1,stelling_3,stelling_2,stelling_1,stelling_1,stelling_2,stelling_3,stelling_3,stelling_1,stelling_3,stelling_2,stelling_1,stelling_2,stelling_2,stelling_2,stelling_1,stelling_3,stelling_2,stelling_1,stelling_3,stelling_1,stelling_3,stelling_3,stelling_2,stelling_3,stelling_1,stelling_2,stelling_3,stelling_1,stelling_1,stelling_3,stelling_1,stelling_1,stelling_2,stelling_2,stelling_1,stelling_3,stelling_1,stelling_2,stelling_1,stelling_2,stelling_3,stelling_1,stelling_3,stelling_2,stelling_2,stelling_2,stelling_3,stelling_1,stelling_3,stelling_2,stelling_2,stelling_2,stelling_1,stelling_3,stelling_1,stelling_3,stelling_3,stelling_2,stelling_3];
    Now your images and xmls go from 1 to n, and your answers go from 0 to n. If you never select 0, you're okay.

  7. #7
    Member
    Join Date
    Jan 2011
    Posts
    47
    nope still not working and spice isn't working correctly now either ?

  8. #8
    Member
    Join Date
    Jan 2011
    Posts
    47
    PHP Code:
    var high:int answers.length;
        var 
    low:int 1;
        var 
    randomnumber:int = (Math.floor(Math.random()*(high-low))+low); 
    is now the random number
    and i copy/pasted the string

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If you describe a problem as simply "not working" again, I will not help you. Describe HOW it is not working.

    What string? Did you mean splice instead of spice?

    You need to simplify your problem and do some debugging on your own. I suggest temporarily changing your answers array to a single answer, then stepping through the code and watch the variable values. Figure out where things don't match up.

    And for Pete's sake, if you're getting error messages, post them.

  10. #10
    Member
    Join Date
    Jan 2011
    Posts
    47
    I'm sorry

    its not matching the correct answers with the images/text
    for example:
    i get an image of a tree with the correct answer is stelling_2
    it sometimes counts it as correct and sometimes as wrong.

    the string is just a mistake sorry i meant Array

    the splice/spice thing is my annoying spell correct

    i have been debugging for to many days, i have tried changing all entrys in the answers array with stelling_1 and then i have everything correct (if i always chose 1)
    and i have tried putting only 1 in the array but it gets buggy.

    and no I'm not getting any error messages if i had them i would have posted them.

    and I'm not a pro in AS3, so at some point when i have been debugging for so long and i can't find anything that works then i post it here.

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Go back to one answer in the array where it is "buggy". That is the same bugginess that you're dealing with with many answers in the array, but reproducible. Get it working there, and it'll work in the bigger case.

    Have you used the debugger to step through the code and see what the values are at each step?

  12. #12
    Member
    Join Date
    Jan 2011
    Posts
    47
    I've never used the debugger in flash :O i will give it a try

  13. #13
    Member
    Join Date
    Jan 2011
    Posts
    47
    i think i worked it out but the strange thing is now that randomnumber will always be 0.

    I'm using the code that you changed for me
    PHP Code:
    var high:int answers.length;
    var 
    low:int 0;
    var 
    randomnumber:int = (Math.floor(Math.random()*(high-low))+low); 
    but i will always end up with 0 if i trace it

  14. #14
    Member
    Join Date
    Jan 2011
    Posts
    47
    i found the problem but i don't know how to fix it

    if i put the script for checking the answer before it generates a new random number it becomes 0 for some reason but if i put it after when it generates a new random number it will check your answer for the new question and not one you just answered.

    both the script for checking the answer and generating a new random number are in the MouseEvent.

    i traced the random number before it generates a new one and after and the results are that the before trace will always get a 0 and thus will always use the first item in the answers array, and the trace after will get a random number but when i put the script for checking the answers there it will not work for it will be checking for the new number.

    i will post my entire event here maybe you can see what i did wrong. (some terms for tracing and instance names are in dutch)

    PHP Code:
    Next_btn.addEventListener(MouseEvent.CLICKrandomImageEvent);
    function 
    randomImageEvent(e:MouseEvent):void
    {
        
    counts++;
        
    trace("randomnumber is nu " randomnumber);
        
    trace("het goede antwoord is " answers2[randomnumber]);
        
    trace("Counts staat op " counts);
        if (
    answers[randomnumber].selected == true)
        {
            
    trace("antwoord is goed!");
            
    score++;
            
    trace("score is nu " score);
        }
        else
        {
            
    trace("antwoord is fout!");
            
    trace("score is nu " score);
        }
        var 
    high:int answers.length;
        var 
    low:int 0;
        var 
    randomnumber:int Math.floor(Math.random()* high);
        
    images.splice([randomnumber], 1);
        
    stellingen.splice([randomnumber], 1);
        
    answers.splice([randomnumber], 1);
        
    answers2.splice([randomnumber], 1);
        var 
    request:URLRequest = new URLRequest(images[randomnumber]);
        var 
    XMLLoader:URLLoader = new URLLoader();
        
    XMLLoader.load(new URLRequest(stellingen[randomnumber]));
        
    XMLLoader.addEventListener(Event.COMPLETEprocessXML);
        
    Trace_arrays();
        
    loader.101;
        
    loader.190;
        
    addChild(loader);
        
    loader.load(request);
        
    loader.addEventListener(Event.COMPLETEloadscore);
        
    addChild(Bamboo_frame);
        
    addChild(counter_txt);
        
    counter_txt.text counts "/10";
        if (
    counts == 11)
        {
            
    removeChild(first_mc);
            
    removeChild(second_mc);
            
    removeChild(third_mc);
            
    removeChild(loader);
            
    removeChild(stelling_1);
            
    removeChild(stelling_2);
            
    removeChild(stelling_3);
            
    removeChild(stelling_1_text);
            
    removeChild(stelling_2_text);
            
    removeChild(stelling_3_text);
            
    removeChild(BTN_TEXT_1);
            
    removeChild(BTN_TEXT_2);
            
    removeChild(BTN_TEXT_3);
            
    removeChild(counter_txt);
            
    removeChild(Bamboo_frame);
            
    addChild(Post_btn);
            
    addChild(scorescreen_txt);
            
    addChild(ScoreImages_1);
            
    addChild(ScoreImages_2);
            
    addChild(ScoreImages_3);
            
    addChild(ScoreImages_4);
            
    addChild(ScoreImages_5);
            
    addChild(ScoreImages_6);
            
    addChild(ScoreImages_7);
            
    addChild(ScoreImages_8);
            
    addChild(ScoreImages_9);
            
    addChild(ScoreImages_10);
            
    addChild(BambooFrame_score);
            
    addChild(Score_scorescreen_txt);
            
    Score_scorescreen_txt.text String(score);
            
    removeChild(Next_btn);
        }
        if (
    counts == 1)
        {
            
    removeChild(Start_Loader_1);
            
    removeChild(Start_Loader_2);
            
    removeChild(Start_Loader_3);
            
    removeChild(Start_txt);
            
    removeChild(Start_2_txt);
            
    addChild(stelling_1_text);
            
    addChild(stelling_2_text);
            
    addChild(stelling_3_text);
            
    addChild(BTN_TEXT_1);
            
    addChild(BTN_TEXT_2);
            
    addChild(BTN_TEXT_3);
            
    addChild(stelling_1);
            
    addChild(stelling_2);
            
    addChild(stelling_3);
            
    addChild(first_mc);
            
    addChild(second_mc);
            
    addChild(third_mc);
        } 
    and these are my Arrays:
    PHP Code:
    var answers:Array = [stelling_2,stelling_3,stelling_1,stelling_2,stelling_1,stelling_2,stelling_2,stelling_2,stelling_3,stelling_3,stelling_1,stelling_2,stelling_3,stelling_2,stelling_1,stelling_1,stelling_3,stelling_2,stelling_1,stelling_1,stelling_2,stelling_3,stelling_3,stelling_1,stelling_3,stelling_2,stelling_1,stelling_2,stelling_2,stelling_2,stelling_1,stelling_3,stelling_2,stelling_1,stelling_3,stelling_1,stelling_3,stelling_3,stelling_2,stelling_3,stelling_1,stelling_2,stelling_3,stelling_1,stelling_1,stelling_3,stelling_1,stelling_1,stelling_2,stelling_2,stelling_1,stelling_3,stelling_1,stelling_2,stelling_1,stelling_2,stelling_3,stelling_1,stelling_3,stelling_2,stelling_2,stelling_2,stelling_3,stelling_1,stelling_3,stelling_2,stelling_2,stelling_2,stelling_1,stelling_3,stelling_1,stelling_3,stelling_3,stelling_2,stelling_3];
    var 
    answers2:Array = ["stelling_2","stelling_3","stelling_1","stelling_2","stelling_1","stelling_2","stelling_2","stelling_2","stelling_3","stelling_3","stelling_1","stelling_2","stelling_3","stelling_2","stelling_1","stelling_1","stelling_3","stelling_2","stelling_1","stelling_1","stelling_2","stelling_3","stelling_3","stelling_1","stelling_3","stelling_2","stelling_1","stelling_2","stelling_2","stelling_2","stelling_1","stelling_3","stelling_2","stelling_1","stelling_3","stelling_1","stelling_3","stelling_3","stelling_2","stelling_3","stelling_1","stelling_2","stelling_3","stelling_1","stelling_1","stelling_3","stelling_1","stelling_1","stelling_2","stelling_2","stelling_1","stelling_3","stelling_1","stelling_2","stelling_1","stelling_2","stelling_3","stelling_1","stelling_3","stelling_2","stelling_2","stelling_2","stelling_3","stelling_1","stelling_3","stelling_2","stelling_2","stelling_2","stelling_1","stelling_3","stelling_1","stelling_3","stelling_3","stelling_2","stelling_3"];
    var 
    images:Array = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg","8.jpg","9.jpg","10.jpg","11.jpg","12.jpg","13.jpg","14.jpg","15.jpg","16.jpg","17.jpg","18.jpg","19.jpg","20.jpg","21.jpg","22.jpg","23.jpg","24.jpg","25.jpg","26.jpg","27.jpg","28.jpg","29.jpg","30.jpg","31.jpg","32.jpg","33.jpg","34.jpg","35.jpg","36.jpg","37.jpg","38.jpg","39.jpg","40.jpg","41.jpg","42.jpg","43.jpg","44.jpg","45.jpg","46.jpg","47.jpg","48.jpg","49.jpg","50.jpg","51.jpg","52.jpg","53.jpg","54.jpg","55.jpg","56.jpg","57.jpg","58.jpg","59.jpg","60.jpg","61.jpg","62.jpg","63.jpg","64.jpg","65.jpg","66.jpg","67.jpg","68.jpg","69.jpg","70.jpg","71.jpg","72.jpg","73.jpg","74.jpg","75.jpg"];
    var 
    stellingen:Array = ["1.xml","2.xml","3.xml","4.xml","5.xml","6.xml","7.xml","8.xml","9.xml","10.xml","11.xml","12.xml","13.xml","14.xml","15.xml","16.xml","17.xml","18.xml","19.xml","20.xml","21.xml","22.xml","23.xml","24.xml","25.xml","26.xml","27.xml","28.xml","29.xml","30.xml","31.xml","32.xml","33.xml","34.xml","35.xml","36.xml","37.xml","38.xml","39.xml","40.xml","41.xml","42.xml","43.xml","44.xml","45.xml","46.xml","47.xml","48.xml","49.xml","50.xml","51.xml","52.xml","53.xml","54.xml","55.xml","56.xml","57.xml","58.xml","59.xml","60.xml","61.xml","62.xml","63.xml","64.xml","65.xml","66.xml","67.xml","68.xml","69.xml","70.xml","71.xml","72.xml","73.xml","74.xml","75.xml"];
    var 
    counts:int 0;
    var 
    score:int 0
    i hope you can help me because i can't see anything that can fix this problem and i hope the problem itself is clear

  15. #15
    Senior Member guardiankitty's Avatar
    Join Date
    Dec 2006
    Location
    Here
    Posts
    215
    Hmm...

    Lets walk through this really quickly, we might be able to figure out if anything should be changed or updated.

    Actionscript Code:
    var high:int = answers.length;
        var low:int = 0;
        var randomnumber:int = Math.floor(Math.random()* high);
        images.splice([randomnumber], 1);
        stellingen.splice([randomnumber], 1);
        answers.splice([randomnumber], 1);
        answers2.splice([randomnumber], 1);
        var request:URLRequest = new URLRequest(images[randomnumber]);
        var XMLLoader:URLLoader = new URLLoader();
        XMLLoader.load(new URLRequest(stellingen[randomnumber]));

    So, lets start at the begining:

    The high variable is set to the length of the answers array. So lets say there is 10 (im too lazy to actually count and it will be variable anyway).

    The low variable is not in use.

    You generate a random number, 0-1, then times the result by the high variable (like 10), then lastly you floor that product. This will give you a range between 0 and 9.

    You Splice into the: images, stellingen, answers and answers2 arrays. You do not save the returned 'Spliced' value. The Splice starts at the 'index' of the randomly generated number (for this case lets say that it is 5). Now, these arrays have one less element in them, and everything after index 5 'shifts' over to the left by one (so 6 becomes 5, 7 becomes 6,... etc.)

    Next you use images[5] and stellingen[5]. However in these values 'were' images[6] and stellingen[6].


    Also the variable is set in the function scope; All Variables Initialized in the Function Scope are deleted when the function is completed. If you are trying to use "var randomnumber:int" the next time the function is run, then you will get a null value (and you should be throwing an error)- so this bit here:
    Actionscript Code:
    if (answers[randomnumber].selected == true)

    Will not work: randomnumber has not be initialized in this scope yet.



    HOW TO FIX
    1. Set the Variable Outside of the function, but accessiable from the function.
    2. Re-Roll your randomnumber AFTER you splice the old questions from the list, not before.


    Hope that helps, let me know if you have any other questions or if you don't understand any of my ramblings.

  16. #16
    Member
    Join Date
    Jan 2011
    Posts
    47

    Thumbs up

    THANKS IT WORKED!!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center