Using AS3 in Flash, loading dynamic text in the TextField is a common task and usually this text is large enough and requires scrollbars to show it properly. Unfortunately, TextField in Flash, even in the latest CS5 version, available via AS3 does not have built-in support of Scrollbars.
I recently had to do the same thing and following are the steps invloved to achieve this:
Create a new Flash document (ActionScript 3.0). Drag the UIScrollBar onto the stage and then delete it. Add following code to the Actions Panel on frame 1.
import fl.controls.UIScrollBar; var nameTxt:TextField = new TextField(); nameTxt.multiline = true; nameTxt.height=100; nameTxt.border = true; nameTxt.width = 400; nameTxt.x = 50; nameTxt.y = 50; var mySb:UIScrollBar = new UIScrollBar(); mySb.direction = "vertical"; // Size it to match the text field. mySb.setSize(nameTxt.width, nameTxt.height); // Move it immediately below the text field. mySb.move(nameTxt.width + nameTxt.x, nameTxt.height); //puut it on stage addChild(mySb); // Set nameTxt as target for scroll bar. mySb.scrollTarget = nameTxt; nameTxt.htmlText = "<li>Sed ut perspiciatis unde omnis iste</li> <li>Tnatus error sit voluptatem accusantium</li> <li>Gdoloremque laudantium, totam rem aperiam</li> <li>Deaque ipsa quae ab illo inventore veritatis</li> <li>Suasi architecto beatae vitae dicta sunt explicabo.</li>";
Hope this helps.
Thank you Ali for your great and clear tutorial , very useful man 🙂
I also got a news here for you that I thought would be so useful for all of us as Flash devs and thought to share it with all!
Now that we are talking about TextField, let me introduce a class named TextArea that is so powerful and is extended from the Adobe TextField class itself.
It allows you every possible tags even your own custom tag and has much more abilities too.
Check out http://doitflash.com/ for more information.
It not only allows you to load different SWF files by calling different tags in line of your text but also you have much more control over your Text blocks and its contents… such as calling your custom functions right from your text blocks and passing multiple and different arguments through them; loading talking avatars, video players, buttons, slideshows and more… by calling their own tags and having full interaction between all of the loaded SWF modules and your text block. Check out the site for more information, downloading the platform is also free of charge 🙂
Actually it helped a lot!
Exactly what I was looking for: using scrollbar component with AS3.
‘scrollTarget’ didn’t know this property before, but it’s critical if you change frames with text 🙂
Good Luck, and thanks for sharing the knowledge.
I’m glad it helped you.