Checkbox answer needs to trigger visibility of movie clip
A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Checkbox answer needs to trigger visibility of movie clip

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6

    Checkbox answer needs to trigger visibility of movie clip

    Hi, I am using AS2 and I am trying to create a script for a flag based guessing game. The situation is this:

    I have the Spanish flag with 4 multiple choice options of: Spain, Norway, Macedonia and Italy. These are all checkboxes. Ofcourse the answer is Spain and so when Spain is checked and a "Submit" button is pressed. I want This to trigger the visibility of one of the tick/crosses. The tick/crosses are movie clips with translucent white shapes over them that are also set as movie clips. Basically, either "mcCoverYes" (the cover for the tick) or "mcCoverNo" (the cover for the cross) will disappear depdning on whether the user gets the right answer.

    I am pretty new to Flash and everything I have attempted isn't working. I tried this action script on the button but it didn't work:

    Code:
    on (release) {
    }
    if (cbSpain == true) {}
    {Yes_cover_mc._visible = false;
    }else{
    	No_cover_mc._visible = false;
    }
    cbSpain is the checkbox labelled spain. Basically I am trying to say that when the button is released, if Spain is selected then the visibility of the "tick" graphic will disappear so that the tick appears in it's full colour. If cbSpain isn't selected, then the cover of the cross will be hidden to declare it as a wrong answer.

    Thanks!

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Hello, I havent looked much at your logic yet, but Immediately, I see that your are having issues with your syntax. A function should have a opening and closing curly brace {} and the content of the function should be going inside of your brace. Same thing for if(){} statements. When you nest statements, you need to have a "sandwich" mentality. This means that you need to close the first brace you open last. See the diagram below for an explanation:

    on (release) {
    if (cbSpain == true) {
    Yes_cover_mc._visible = false;
    }else{ No_cover_mc._visible = false;
    }}

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    105
    After looking at your logic, you will need to add a property after cbSpain for it to yeild a "true" result.

    You need to say if(cbSpain.value==true){
    Yes_cover_mc._visible = false;
    }else{ No_cover_mc._visible = false;
    }

    The rest of your code looks like it should work as long as you have given everything the correct instance names.

    Good luck!

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Hi jkell, thanks for your help. I tried that and whilst I was now not getting any errors, the button isn't hiding the graphic or anything. It just does nothing?

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    I had to change the code actually as I am now using radio buttons. Here is the code I am using:

    Code:
    btnSubmit.onPress = function() { 
    	if(radioButtonPressed==1){
    		Yes_cover_mc._visible = false;
    	} 	if(radioButtonPressed==2){ 
    		No_cover_mc._visible = false;
    	} 	if(radioButtonPressed==3){ 
    		No_cover_mc._visible = false;
    	} 	if(radioButtonPressed==4){ 
    		No_cover_mc._visible = false;
    	}
    	
    	cbSpain.onPress = function() {
    		radioButtonPressed=1;
    	}
    	cbNorway.onPress = function() {
    		radioButtonPressed=2;
    	}
    	cbMacedonia.onPress = function() {
    		radioButtonPressed=3;
    	}
    	cbItaly.onPress = function() {
    		radioButtonPressed=4;
    }
    }
    Unfortunately this still doesn't work even though it doesn't provide any errors. Any ideas?

  6. #6
    Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Hello again.
    Your Syntax is wrong again. You have nested functions for your radio buttons inside the submit button function. This make is so your variable "radioButtonPressed" is never triggered. I am assuming that all the elements on your stage are properly named. I added a trace statement so you can see what Flash thinks radioButtonPressed should be. Your code should look like this:

    btnSubmit.onPress = function()
    {
    trace(radioButtonPressed);
    if (radioButtonPressed == 1)
    {
    Yes_cover_mc._visible = false;
    }
    if (radioButtonPressed == 2)
    {
    No_cover_mc._visible = false;
    }
    if (radioButtonPressed == 3)
    {
    No_cover_mc._visible = false;
    }
    if (radioButtonPressed == 4)
    {
    No_cover_mc._visible = false;
    }
    };

    cbSpain.onPress = function()
    {
    radioButtonPressed = 1;
    };
    cbNorway.onPress = function()
    {
    radioButtonPressed = 2;
    };
    cbMacedonia.onPress = function()
    {
    radioButtonPressed = 3;
    };
    cbItaly.onPress = function()
    {
    radioButtonPressed = 4;
    };

  7. #7
    Senior Member
    Join Date
    Nov 2012
    Posts
    105

    Sample working file

    Here is a working version of what you were trying to do. radioButton.fla

    It is done in CS6, so hopefully you can open this.

  8. #8
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Hi jkell I cannot thank you enough for your help. I managed to get it all working thanks to your example. One other last quick question... say a user was to get the answer right can I ALSO trigger a sound to play as well as revealing "YES"?

    Thanks again, I really appreciate it.

    EDIT: Sorry to keep asking questions but is there a way to affect another scene if the user were to correctly get the answer (Spain) so that when they go back to the previous scene (a scene with multiple flag choices to guess), the Spain flag has a tick over? Or is this too complicated?
    Last edited by SR46; 02-27-2013 at 09:07 AM.

  9. #9
    Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Adding a sound would not be difficult.
    You would just need to select the sound in your library and edit its Linkage property so that you can call it with actionscript. The following article explains a little about loading sound from the library into flash. Once you follow the first part of the article of declaring your sound object, you would just put the sound feature inside of your submit button fuction...

    http://planetoftheweb.com/components/promos.php?id=211

    Code:
    mySound=new Sound(); 
    mySound.attachSound("NAME_OF_SOUND_IN_LIBRARY"); 
    
    btnSubmit.onPress = function()
    {
    if (radioButtonPressed == 1)
    {
    Yes_cover_mc._visible = false;
    mySound.start(); 
    
    
    }
    if (radioButtonPressed == 2)
    {
    No_cover_mc._visible = false;
    }
    if (radioButtonPressed == 3)
    {
    No_cover_mc._visible = false;
    }
    if (radioButtonPressed == 4)
    {
    No_cover_mc._visible = false;
    }
    };

  10. #10
    Senior Member
    Join Date
    Nov 2012
    Posts
    105

    Adding Checkmarks between Senes AS2

    Sorry to keep asking questions but is there a way to affect another scene if the user were to correctly get the answer (Spain) so that when they go back to the previous scene (a scene with multiple flag choices to guess), the Spain flag has a tick over? Or is this too complicated?
    Nothing is too complicated if you are willing to learn.

    1) Create all of the checkmarks on top of your flags so they are ready to be used with actionscript. This means you create one check mark and make it a MovieClip. Then use that same checkmark for all flags, but give each a unique instance name example: checkMark1 or checkMarkSpain.
    When you test the movie, all the check marks will be visible.

    2) In the code you turn off the visibility of all the check marks:
    checkMark1._visible=false;
    checkMark2._visible=false;
    checkMark3._visible=false;
    checkMark4._visible=false;
    When you test the movie now, the checkmarks should all NOT be visible.

    3) Create variables so that you can know if the checkmarks are visible. In this example I will pretend '0' means not visible and '1' means visible When cm1visible=1 then we change the visibility of the checkmark;

    4)When the user gets the question correct, then you would change the variable to a new value:
    btnSubmit.onPress = function()
    {
    if (radioButtonPressed == 1)
    {
    Yes_cover_mc._visible = false;
    mySound.start();
    cm1visible=1;
    }

    5) then you would have a conditional statement in actions window that turns on the visibility of checkmark "on" based on the variable being changed or not.
    if (_root.cm1visible == 1){ _root.cm1._alpha = 100;}
    if (_root.cm2visible == 1){ _root.cm2._alpha = 100;}
    if (_root.cm3visible == 1){ _root.cm3._alpha = 100;}
    Attached Files Attached Files

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Your help is extremely helpful and I really really appreciate it. Thanks to your easy to understand instructions and example files I managed to do it. Thanks for all the help!

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

Poll by Flashkit.com