A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Hide/Unhide in AC3 - Quick help!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8

    Hide/Unhide in AC3 - Quick help!

    Hi, I'm fairly new to flash and action script 3 so this could be something basic but i have no idea how to do it.

    All i want to do is have a button display a text box when clicked, and then if the button is clicked again - the text box will go away etc.

    I have the button displaying the text fine but i don't know how to set it so it hides it when its clicked on again. text_btn = button and info_txt = text box

    Thanks for your time!

    heres that part of the code:

    Code:
    text_btn.addEventListener(MouseEvent.CLICK, textClicked);
    
    function textClicked(evt:MouseEvent):void
    {
    	loadText();
    }
    
    
    function textLoaded(e:Event):void {
    
    info_txt.text = textLoader.data;
    }
    
    function loadText():void
    {
              textLoader.load(new URLRequest("FILE LOCATION"));
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    AC3? A for Action, I presume. The 3 is self-explanatory. What do you think the C stands for?

    That code does not show the text box when clicked. It populates the text box. info_txt must already be visible (but probably empty) for that to work. If you really want to hide and show it, you will have to either add and remove it from the display, or set its visible property.
    Also, the way you're doing it now, you attempt to load the file each time you press the button. Is that desired, or should it only load once? Is the file going to change?

    Either way, you will need to keep track of the current state so that you can change to the other state on the click. Let's say you're using the visible property to track the state, and you do want to reload the file every time you set the text visible.

    Code:
    function textLoaded(e:Event):void {
      info_txt.visible = true;
      info_txt.text = textLoader.data;
    }
    
    function loadText():void{
      if (info_txt.visible){ //it's already showing
        info_txt.visible = false;
      }else{
        textLoader.load(new URLRequest("FILE LOCATION"));
      }
    }
    
    info_txt.visible = false;  //hide it at the beginning so that the first click will show it.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    8

    Thanks but...

    been resolved
    Last edited by radi0; 02-20-2012 at 06:04 PM. Reason: resolved

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I didn't change anything with the textLoader. Your code already had that in there. Are you sure you got the capitalization correct? You put "textloader" in your description, but it's "textLoader" everywhere in the code. Which line is throwing that error, and where do you declare and instantiate textLoader?

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    8

    reply

    Quote Originally Posted by 5TonsOfFlax View Post
    where do you declare and instantiate textLoader?
    in my code
    Last edited by radi0; 02-20-2012 at 06:03 PM. Reason: wrong code

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes. textLoader is never declared or instantiated. You just start referring to it, and of course flash has no idea what you're talking about.

    What confuses me is that you implied that this part was already working. Was it? If it was, then textLoader must be defined somewhere. But we didn't change that, so it should still work.

    Whatever. Somewhere, in a scope visible to this code, and run before this code, you must have code like this:
    Code:
    var textLoader:URLLoader = new URLLoader();
    If you can't find this somewhere, just stick it in right below stop().

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    8

    update

    Quote Originally Posted by 5TonsOfFlax View Post
    Code:
    var textLoader:URLLoader = new URLLoader();
    If you can't find this somewhere, just stick it in right below stop().
    Yes the code was working before but perhaps whilst i was trying to add to it i missed that part off.

    Now when i run it, i get no error messages but nothing happens when the txt_btn is pressed - the text box (info_txt) isn't showing up at all!

    Thanks

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Oh, yes. You also need to add textLoaded as a complete listener on textLoader.
    Code:
    textLoader.addEventListener(Event.COMPLETE, textLoaded);
    Otherwise, nothing calls textLoaded. Also, if the load fails for any reason right now, you won't know. You might want to add error checking.

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    8

    Thankyou!!!!

    Quote Originally Posted by 5TonsOfFlax View Post
    Oh, yes. You also need to add textLoaded as a complete listener on textLoader.
    Code:
    textLoader.addEventListener(Event.COMPLETE, textLoaded);
    Otherwise, nothing calls textLoaded. Also, if the load fails for any reason right now, you won't know. You might want to add error checking.
    Yep that did the trick! Thanks very much. Just a quick question - i have a scroll bar next to the text box - is there anyway i can get that to appear/disappear at the same time?

    Thanks again - i really appreciate your time!

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't know. How'd you get it there in the first place? It's probably as simple as setting the visible property on that instance at the same time. Or adding/removing from the displaylist. But I don't really know since I don't know what sort of thing the scrollbar is, or how it got there.

  11. #11
    Junior Member
    Join Date
    Feb 2012
    Posts
    8

    scrollbar

    Quote Originally Posted by 5TonsOfFlax View Post
    I don't know. How'd you get it there in the first place? It's probably as simple as setting the visible property on that instance at the same time. Or adding/removing from the displaylist. But I don't really know since I don't know what sort of thing the scrollbar is, or how it got there.
    Its from the components menu - uiscrollbar, i just dragged it on the frame and had to add a bit of code to resize it etc. Its got the instance name uiScroller and it has this code thus far
    Last edited by radi0; 02-20-2012 at 06:04 PM. Reason: resolved

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Try setting the visibility of uiScroller at the same time you set the visibility of info_txt.

  13. #13
    Junior Member
    Join Date
    Feb 2012
    Posts
    8

    Excellent!

    Quote Originally Posted by 5TonsOfFlax View Post
    Try setting the visibility of uiScroller at the same time you set the visibility of info_txt.
    YUP! It now works after adding a bit of code!

    Again thank you very much for your time and have a great day!

    peace

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