A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Problem with showing and hiding buttons

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    2

    Unhappy Problem with showing and hiding buttons

    Hi there,

    I'm using CS5.5 and I want to set up a flash file so that a button will only show up when two other buttons have been clicked. I thought that I could do this with scoring (as I possibly want to scale up to lots of buttons) so I tried this code ('one' and 'two' are the buttons and 'answer' is the button that should show only once both one and two have been clicked).

    import flash.events.MouseEvent;

    var score:Number = 0;

    one.addEventListener(MouseEvent.CLICK, clickone);
    two.addEventListener(MouseEvent.CLICK, clicktwo);

    function clickone(e:MouseEvent):void {
    score += 1;
    }

    function clicktwo(e:MouseEvent):void {
    score += 2;
    }

    if (score == 3) {
    answer.visible = true;
    }
    else {
    answer.visible = false;
    }

    The problem is, I can't get the answer button to ever appear (or disappear if I swap the visible false and true statements). Does anyone know what I'm doing wrong? Or could suggest a simpler way of doing this?

    Thanks!

  2. #2
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    The if statement needs a trigger:

    Code:
     function clickone(e:MouseEvent):void { 
    score += 1;
    checkIt(); 
    } 
    
    function clicktwo(e:MouseEvent):void { 
    score += 2; 
    checkIt();
    } 
    
    function checkIt():void{
    if (score == 3) {
    answer.visible = true;
    }
    else {
    answer.visible = false;
    }
    }
    [SIGPIC][/SIGPIC]

  3. #3
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    EDIT: sorry, rynoe, had this tab open for a couple of hours
    --------

    The answer is simple, your if conditional code is static, it's only executed once, since it's just written standalone in your Frame, without any event listeners or such. So Flash only checks if score is equals to 3, once, and then is when you enter that Frame. What you can do, is to turn that if statement into a function, and call that function for each click, because Flash doesn't keep checking whether score is equals to 3!

    A better way would be to make 2 boolean variables, and set them to true when both buttons have been pressed, and then check if both are equals to true, because with your current code, score will keep increasing, and if you press the first button twice, and then the second button, score will be equals to 4, so even if you have clicked both of the buttons, the third button will not show up, and if you press those two buttons, making the third one visible, pressing them again would result in the third button disappearing.

    Actionscript Code:
    import flash.events.MouseEvent;

    var btn1:Boolean = false;
    var btn2:Boolean = false;
    checkBtn3();

    one.addEventListener(MouseEvent.CLICK, clickone);
    two.addEventListener(MouseEvent.CLICK, clicktwo);

    function clickone(e:MouseEvent):void {
        btn1 = true;
        checkBtn3();
    }

    function clicktwo(e:MouseEvent):void {
        btn2 = true;
        checkBtn3();
    }

    function checkBtn3(){
        if (btn1 == true && btn2 == true) {
            answer.visible = true;
        }
        else {
            answer.visible = false;
        }
    }
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  4. #4
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    Actually, Nig, your code reflects coding standards much better
    [SIGPIC][/SIGPIC]

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    oh, thanks
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    2
    Thank you so much for your help, and for explaining the problem. This is exactly what I wanted!

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