-
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"));
}
-
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.
-
Last edited by radi0; 02-20-2012 at 06:04 PM.
Reason: resolved
-
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?
-
reply
Originally Posted by 5TonsOfFlax
where do you declare and instantiate textLoader?
in my code
Last edited by radi0; 02-20-2012 at 06:03 PM.
Reason: wrong code
-
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().
-
update
Originally Posted by 5TonsOfFlax
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
-
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.
-
Thankyou!!!!
Originally Posted by 5TonsOfFlax
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!
-
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.
-
scrollbar
Originally Posted by 5TonsOfFlax
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
-
Try setting the visibility of uiScroller at the same time you set the visibility of info_txt.
-
Excellent!
Originally Posted by 5TonsOfFlax
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|