| By Guy Watson | Article Rating: |
|
| October 25, 2005 06:45 AM EDT | Reads: |
88,970 |
The Rectangle Class
The Flash Player now contains a lot of new feature exposed to ActionScript that require Rectangles to be specified so Macromedia also added a new ActionScript class for us in this release that makes it really easy for developers to define and manipulate Rectangles. This class is therefore conveniently named the Rectangle class and we can use it to specify the scrollRect of a movie clip as opposed to using a simple object, like this:
squareMask = new flash.geom.Rectangle(0,0,100,100)
//define the viewing area and scroll offset
scrollingMovieclip.scrollRect=squareMask
//add the square mask
I recommend that you use an instance of the Rectangle class to specify the scrollRect of a movie clip as opposed to a simple object, simply because it allows a lot more control over the rectangle once you have created it.
If you are going to be creating a lot of instances of the Rectangle class in your ActionScript, then you should import the Rectangle class namespace using the import statement, it will make your life a lot easier because you wont have to constantly type the full path to the Rectangle class, which is located in the flash.geom package:
import flash.geom.Rectangle
squareMask = new Rectangle(0,0,100,100)
scrollingMovieclip.scrollRect=squareMask
The Rectangle class constructor accepts four parameters; x, y, width and height.
flash.geom.Rectangle(x,y,width,height)
The first two parameters (x and y) define the position of the top left corner. Then the last two parameters (width and height) define the actual size of the rectangle. When you use an instance of the Rectangle class to set the scrollRect property of a movie clip, the x and y parameters are used as the scrolling offsets for each axis.
Once you have created an instance of the Rectangle class, you then have yourself an object that contains more than just the four properties required of an object passed to the scrollRect property. There are numerous other properties and methods that you can use to find out about or manipulate that rectangle object, for example to move the top left hand corner of the rectangle you can use the Rectangle.offset method:
import flash.geom.Rectangle
squareMask = new Rectangle(0,0,100,100)
squareMask.offset(10,10)
trace(squareMask)
scrollingMovieclip.scrollRect=squareMask
Or to change the width of the rectangle you can just set the value of the width property:
import flash.geom.Rectangle
squareMask = new Rectangle(0,0,100,100)
squareMask.width+=100
//add 100 pixels to the width of the rectangle
scrollingMovieclip.scrollRect=squareMask
Also, you can for example, find out the position of the bottom right hand corner of the rectangle using the bottomRight property:
import flash.geom.Rectangle
squareMask = new Rectangle(0,0,100,100)
bottomRight=squareMask.bottomRight
trace(bottomRight)
Scrolling Large Amounts of Text
So far, I have only shown you how to use the scrollRect property to crop a rectangular region of a movie clip and hide the rest, however, the real power of this feature comes into play when we actually need to scroll the movie clip's content around.
I am now going to show you a fairly simple example of how to use the scrollRect property to scroll some text, loaded from a text file. For added effect we will add some blur to the text while it is scrolling and a little inertia, just because we can.
- First and foremost create a new Flash Document.
- Set the movie's width and height to something reasonable (370 * 200) works for me.
- Rename the first and only layer on the timeline to actions.
- Select the first frame on the actions layer and open the Actions panel (F9).
- Add the following code:
import flash.geom.Rectangle
import flash.filters.BlurFilter
/*Ensure that the Flash Movie can only be resized along the x and yaxis
*/
Stage.addListener(this)
Stage.align="TL"
Stage.scaleMode="noScale"
blur=new BlurFilter(0,0,3)
//create a blur filter and store a reference to it
sRect=new Rectangle(0,0,w,h)
//create a rectangle with no scrolling offset and store a reference to it onResize()
//resize the textfield and scrollRect to the size of the Stage
initialize()
//create a textfield inside of a movie clip
*/
load the contents of the specified text file into the textfield
*/
loadTextFile("desiderata.txt") - Create a new text file and put some content inside it, the more the merrier.
- Save the text file in a new folder and remember the text files name.
- Select the first frame of the actions layer again.
- Open the Actions panel, if it is not already open. (F9).
- Change the last line of code that looks like this:
loadTextFile("desiderata.txt")
- Replace "desiderata.txt" with the name of your text file.
- Now add a new layer above the layer named actions.
- Rename the new layer to functions.
- Select the first frame of the functions layer.
- Open the Actions panel, if it is not already open. (F9).
- Add the following code:
import mx.utils.Delegate
//create a textfield inside a movie clip
function initialize()
{
//create a container movie clip because you cant use scrollRect on a textfield
c=this.createEmptyMovieClip("container_mc",this.getNextHighestDepth())
//create a textfield inside the container movieclip
c.createTextField("contents_txt",c.getNextHighestDepth(),0,0,w,h)
//store a short reference to the textfield
tf=c.contents_txt
//set the initial textfield properties
with(tf)
{
multiline=true
wordWrap=true
selectable=false
html=true
condenseWhite=true
autoSize=true
}
//specify the default scrollRect
c.scrollRect=sRect
}
//called each frame to scroll the text into view with some intertia
function scroll()
{
//how far away is the current scroll away from the target scroll
var diff=Math.round(tysRect.y)
//if the difference is negative, make it positive
if(diff<1)
diff*=1
//if the difference is too big, limit it to 10
if(diff>10) diff=10
//update the blur on the yaxis
blur.blurY=diff
//add a percentage of the distance we need to go to the yaxis
scroll offset
sRect.y+=(tysRect.y)/8
//if the text has scrolled to where it is supposed to be, stop calling this function
if(Math.round(tysRect.y) == 0) delete c.onEnterFrame
//reapply the changed scrollRect and blur filter
c.scrollRect=sRect
c.filters=[blur]
}
//loads a textfiles contents into the textfield
function loadTextFile(url)
{
//show a message while we wait for the file to load
tf.htmlText="Loading... please wait"
var txt=new LoadVars()
//hotwire
the onData event to set the contents of the textfield
txt.onData=function(d)
{
tf.htmlText=d
}
//load the specified file
txt.load(url)
}
//called when the mouse moves
function onMouseMove()
{
//set the target position
ty=(this._ymouse)/200*(tf._heighth)
//call the 'scroll' function every frame
c.onEnterFrame=Delegate.create(this,scroll)
}
//called when the Stage resizes
function onResize()
{
//resize the textfield and the scrollRect
tf._width=sRect.width=Stage.width
tf._height=sRect.height=Stage.height
w=Stage.width
h=Stage.height
} - Now save the Flash Document (CTRL + S) in the same directory as the text file you created. 18. Test your Flash Movie (CTRL + ENTER). You should see a text field that is the same size as the Stage, containing the text from your text file. When you move your mouse up, the text scrolls down, when you move your mouse down, the text scrolls up. Try resizing the window, the text field should resize.
Although this feature can be used independently of Bitmap Caching, it is necessary to turn on Bitmap Caching for movie clips that have a scrollRect property defined, to achieve maximum performance when scrolling. This is because it is much faster for the player to shift around pixels in the bitmap equivalent of the movie clip that was generated by Bitmap Caching, than it is to copy around the vector data of a movie clip.
Using scrollRect to overcome size limits Previously I mentioned that bitmaps inside the player cannot be large than 2880 pixels in either width or height. This can become a problem when you want to apply a filter to a movie clip that is larger than thesedimensions. When you apply a filter to a movie clip, Bitmap Caching is turned on for that movie clip so that abitmap representation of it is generated internally. The filter is then applied to the bitmap representation that is generated.
Let's say for example that you have one large movie clip that is 5000 pixels in width and you want toscroll it across the background of your Flash Movie with a blur filter applied. Unfortunately you cannot simplyapply the filter to the movie clip because a bitmap cannot be generated internally to represent the movie clip,due to the fact that its dimensions are larger than the size limits. However you can work around this problem using the scrollRect property. I think it would be safe to say that your actual Flash Movie will not be 2880 pixels or more in width. So, all you need to do is set the scrollRect property of the scrolling movie clip to crop it to the size of the Flash Movie which makes the visual size of the movie clip smaller than the maximum size limit. Filters are applied last, so the filters you specified either in the authoring environment or using ActionScript will be applied to the movie clip's cropped state:
largeMovieclip.scrollRect=new flash.geom.Rectangle
(0,0,Stage.width,Stage.height)
blur=new flash.filters.BlurFilter(3,0,3)
// a Gaussian blur on the x axis
largeMovieclip.filters=[blur]
Published October 25, 2005 Reads 88,970
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Guy Watson
Guy Watson (aka FlashGuru) has been a well-recognized figure in the Flash community for around four years, supporting the community with tutorials and source files, moderating the large Flash community forums, and running his own Flash resource Web site - FlashGuru's MX 101. Guy was one of the two developers that created the award-winning zoom interface for Relevare and now works for Endemol UK, the creative force behind reality television, producing programs such as Big Brother and The Salon. Guy now spends most of his time developing Flash games and applications for high-profile clients such as Channel 5 Television, Ladbrookes, and UK Style.
![]() |
Conference Call 07/07/06 04:45:58 PM EDT | |||
Trackback Added: Conference Call; Conference Call |
||||
![]() |
Drug Rehab 07/07/06 09:19:22 AM EDT | |||
Trackback Added: Drug Rehab; Drug Rehab |
||||
![]() |
Payday Loan 07/05/06 10:59:50 AM EDT | |||
Trackback Added: Payday Loan; Payday Loan |
||||
![]() |
Dental Insurance 07/05/06 10:33:31 AM EDT | |||
Trackback Added: Dental Insurance; Dental Insurance |
||||
![]() |
Data Recovery 07/04/06 08:38:25 AM EDT | |||
Trackback Added: Data Recovery; Data Recovery |
||||
![]() |
Printers 07/02/06 12:24:47 PM EDT | |||
Trackback Added: Printers; Printers |
||||
![]() |
1031 Exchange 07/01/06 02:08:26 PM EDT | |||
Trackback Added: 1031 Exchange; 1031 Exchange |
||||
![]() |
First Aid Kits 07/01/06 03:49:12 AM EDT | |||
Trackback Added: First Aid Kits; First Aid Kits |
||||
![]() |
Car Accident Lawyer 06/30/06 02:25:19 PM EDT | |||
Trackback Added: Car Accident Lawyer; Car Accident Lawyer |
||||
![]() |
Acne 06/25/06 02:22:46 PM EDT | |||
Trackback Added: Acne; Acne |
||||
![]() |
Equity Line Of Credit 06/25/06 07:38:23 AM EDT | |||
Trackback Added: Equity Line Of Credit; Equity Line Of Credit |
||||
![]() |
Forklift 06/25/06 07:22:27 AM EDT | |||
Trackback Added: Forklift; Forklift |
||||
![]() |
Term Life Insurance 06/24/06 04:21:24 AM EDT | |||
Trackback Added: Term Life Insurance; Term Life Insurance |
||||
![]() |
Eye Surgery 06/23/06 06:58:00 PM EDT | |||
Trackback Added: Eye Surgery; Eye Surgery |
||||
![]() |
Relocation 06/23/06 06:35:57 PM EDT | |||
Trackback Added: Relocation; Relocation |
||||
![]() |
Payday Loan 06/23/06 06:33:04 PM EDT | |||
Trackback Added: Payday Loan; Payday Loan |
||||
![]() |
Payday Loan 06/23/06 03:15:54 PM EDT | |||
Trackback Added: Payday Loan; Payday Loan |
||||
![]() |
Relocation 06/23/06 03:10:34 PM EDT | |||
Trackback Added: Relocation; Relocation |
||||
![]() |
Distance Education 06/23/06 01:29:59 AM EDT | |||
Trackback Added: Distance Education; Distance Education |
||||
![]() |
Relocation 06/22/06 09:36:41 PM EDT | |||
Trackback Added: Relocation; Relocation |
||||
![]() |
Vasectomy 06/22/06 12:31:50 PM EDT | |||
Trackback Added: Vasectomy; Vasectomy |
||||
![]() |
Aeon 05/27/06 03:21:41 AM EDT | |||
This tutorial is full of syntax errors. |
||||
![]() |
eddyC 02/19/06 07:05:15 PM EST | |||
Hi "noname", I was searching for a solution to this problem and found out you've had the same tysRect-problem... Have you solved it by now? :-) TIA! |
||||
![]() |
noname 01/05/06 06:00:11 PM EST | |||
hey, i just took a shot at the example in "Cover Story: How to Increase the Frame Rates of Your Flash Movies": "Scrolling Large Amounts of Text". Unless i'm doing something incredibly wrong, it's not close to complete the way you have it in the article. Anyway, i think i've narrowed it down and just need the value for one last property: "tysRect", in the scroll() function. what is this supposed to be set to??? is there a working fla anywhere? thanks! |
||||
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Unveils LiveCycle Enterprise Suite 2 for Deployment in the Cloud
- Adobe Flex Developer Earns $100K in New York City
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Ph.D. in Twitter Anyone?
- Eolas Sues the Internet
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Betas Target RIAs and Cloud Computing
- Special Report on the Emerging Cloud Computing Trend
- Adobe Cans Another 9% of its Workforce
- My Thoughts on Ulitzer
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Ulitzer Live! New Media Conference & Expo
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Cloud Executives Feature on Cloud Computing Expo Power Panel
- Software Flexibility in the Cloud - Part 4 of 5
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Is Microsoft as Free as Open Source?
- Adobe Reader Sued
- Adobe Unveils LiveCycle Enterprise Suite 2 for Deployment in the Cloud
- Where Are RIA Technologies Headed in 2008?
- Cover Story: How to Increase the Frame Rates of Your Flash Movies
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Your First Adobe Flex Application with a ColdFusion Backend
- Adobe Flex 2: Advanced DataGrid
- i-Technology Blog: Death-Knell For "Rich Media? Hardly!
- Adobe/Macromedia - Microsoft, Look Out!
- How To Create a Photo Slide Show ...
- Adobe Flex Interface Customization - Themes, Styles, Skins
- Personal Branding Checklist
- Has the Technology Bounceback Begun?
- "Real-World Flex" by Adobe's Christophe Coenraets





































