![]() |
Change the frame rate of your movie
Using ActionScript 3, you can dynamically change the frame rate of your movie using the Stage class.
The Stage class (flash.display.Stage) is the class assigned to the stage object which is accessible from your main movie sprite/movie clip (or others within the same security sandbox) using the stage property. The stage object has a frameRate property which can contain any value between 0.01 and 1000 and determines the frame rate at which the Flash player plays back your movie. Changing this value lets you change the frame rate at runtime. ActionScript Code:
|
Nice to have an AS 3 thread. Maybe whole Flex/AS3 forum would be usefull as it seem to spred a lot.
To the stage thing... is "stage" and not "Stage" a typo or do you need to import and instance the Stage to change the framerate? Shouldnt flash.display.Stage.frameRate = 12 do the job then? |
Changing FPS with AS? Isnt this a dream come true?
|
Quote:
|
Quote:
Quote:
http://livedocs.macromedia.com/labs/...lay/Stage.html |
So that example at livedocs makes it pretty clear... "stage" is a global object allowing you to view and change its properties or to register events with it.
ActionScript Code:
@fabiopb: Why? Isnt this just a sign, that flash goes on pretty well? If your worried anout the Adobe Flash application itself, then you dont need to be... Flex is just for something else than Flash. It wont also disappear from the Internet as everybody has the plugin, people are used to use it and it still has great potential. |
one more step towards making AS to Java!! Great!!
|
Quote:
|
Class scope is now bound to class methods
ActionScript 3 is entirely class-based. When you create classes, you create variables and functions (methods) which relate to and work with that class and instances of that class. Unlike ActionScript 2, methods in ActionScript 3 now retain their class scope when called, even if assigned to another object and called from that object, or if used with Function.call and Function.apply. Example:
ActionScript Code:
|
this topic at least should be a sticky !
thx Senocular. |
Quote:
|
Nice one, sen :thumb:
|
A few questions...Whats AS 3.0 for? Is there a new flash coming out?
And PK...why do you believe flash will decline? |
Quote:
More details on available and upcoming products can be found on http://www.adobe.com/ and http://labs.adobe.com/ (where both the Flex 2 beta and Flash Player 9 beta are currently available for download) |
Hey sen, how much work do you think it'll take to convert an AS2 class to a fully optimized AS3? I'm assuming it would take just a few changes to make it work, but an entire rewrite to take advantage of all AS3+F9 features. Have you tried converting any of your classes to 3 yet?
Working with a project that has a few thousand lines of code, it seems like a daunting task. Because there's things like attachMovie that aren't even there anymore, so it's not like easy AS1 to AS2, correct? |
Quote:
It depends. Some conversions aren't that bad. However, with the new scoping behaviors of AS3, there can be some drastic changes. Also, another thing to consider is default values for some variable types. Declared but undefined Number variable instances, for example, are no longer undefined but NaN and to check to see if they have been defined you should use isNaN(myNum) and not myNum == undefined. There are also a lot of little things that have to be modified, such as adding the package {} block, using the override keyword, and changing all instances of Void to void (and not using void if methods have no arguments). I've only converted my Path class at this point, and I think that was only (if) a couple hundred lines. It wasn't too bad at all, but I know there are some other classes I think I would fear to convert. |
Graphics Object and the Drawing API
Like ActionScript 1 and 2, ActionScript 3 also has a drawing API that allows you to draw vector lines and shapes dynamically in movie clips and sprites. With ActionScript 3, however, the drawing API is now used off of an object within display objects (movie clips, sprites, etc.) defined as graphics (flash.display.Graphics). This graphics property represents the dynamic drawing layer where drawing API drawings exist. Like before, it is placed below all children of the target object. Also, in ActionScript 3, you have new methods that help you more easily create rectangles, circles, and even rounded rectangles. These include:
Example: ActionScript Code:
|
New Variable Types
ActionScript 3 supports a wide range of variable types including some which were not present in previous versions of ActionScript. Basic types for AS3 include:
Primitive:
Additional types also exist that relate to their classes; ex: Matrix (flash.geom.Matrix), Shape (flash.display.Shape), URLRequest (flash.net.URLRequest), etc. Things to note:
|
Display Objects
ActionScript 3 now has a new collection of "display objects" which includes those objects that can be seen on the screen or added to the "display list." This goes beyond the simple movie clip, button, and text field objects that ActionScript had access to before. AS3 display objects include
AVM1Movie represents a movie created with ActionScript 1 or 2. Those movies use ActionScript Virtual Maching 1 where AS3 movies use AVM2. AVM2 movies can play AVM1 movies, but cannot interact with them (their ActionScript) using AS3. Bitmaps are bitmap objects. You can specify their imagery with BitmapData objects or they can be bitmaps from files. Loader objects are display objects that load external content into them. This content can be images or other SWF movies. MorphShapes are shape tweens created in the timeline. Though you cannot create them in ActionScript, you can access those that exist on the timeline already using ActionScript and they are of the type MorphShape. MovieClips are the movie clips you know and love. Shapes are stripped down movie clips that essentially only contain a graphics object for drawing in with the vector drawing API. Using Shapes instead of MovieClips or Sprites can help conserve memory Sprite objects are essentially movie clips without timelines. This is your most common display object in AS3 and usually the one extended when creating your own display object subclasses. StaticText, like MorphShapes, cannot be created with ActionScript instead referening static text objects that are pre-existing on the Flash timeline. TextField objects include dynamic and input text. Video objects represent Flash video. |
New Import Directive
The import directive in ActionScript 3 is a little different than import in ActionScript 2. In AS2, import was used solely as a name shortener for classes defined in packages; it was not required to use a class. Instead of using import, you could just have used the class's full name. ex:
ActionScript Code:
In ActionScript 3, the import directive is required to access classes in their packages, even if you reference the class using its full name. You can still use the full name (or just the imported class name), but the import is required ActionScript Code:
As with AS2, you can also use the wildcard character (*) in AS3 to import all classes within a package. ActionScript Code:
|
How did I not see this thread until today? :-/
Why are the rules /suggestions for using uint so strict? Is it not as fast as an int variable? In other words, I see no reason not to use uint for every value that I know will be a positive integer, why isn't this the case? When using addChild and getChild-type methods, you are allowed to add objects that are subclasses of DisplayObject, but the 'get' functions all return DisplayObjects. I'm assuming that this is done so the method will have only one return type, but it is kind of inconvenient to have to construct subclass-typed objects out of that returned value, like "Bitmap(imageHolder.getChildAt(0))". Is there a better way of handling returns that return value types that are less specific than you would like? How do you make functions callable by events and non-events with more than just an event as an argument? In your CoilGallery source, I saw that you had something like "event:Event = null", which I'm assuming makes the argument optional. I've been running into trouble using functions for multiple purposes that need specific argument types. If I, for example, had a function that for some reason needed to have the x property of a DisplayObject. If I wanted to be able to call this function with an event and by itself, would it be best to define the function so it can be run by itself and then have a separate function that serves as an event handler to get the desired property using info from the event.target and then passes that into the function with the x argument? |
1. Yes, uints are pretty slow. I don't know why, but they are. Typically they're just used for numbers that require values that high (like colors with alpha information). Using int for integers is generally a little faster than Number (but not much if at all in all situations). All in all, using only Number wouldn't kill you. They might get around to optimizing int (and maybe uint?) more in the future, though. So its good practice to use them where applicable. For more info see:
http://kuwamoto.org/2006/06/15/avoid...-actionscript/ 2. The getChild methods all return DisplayObject types because methods are only allowed to have one type associated with their return value. By using DisplayObject, its using a type that all objects must be or inherit from to even be added in the first place. So if you add something to the display list, you know it's at least going to be a DisplayObject and having the return value typed as that means it works for whatever is returned. Note: You can also use the 'as' operator to change the type of variables (and I think this is preferred over ClassCast() conversions). ActionScript Code:
3. In using event:Event = null as the parameter for a method, I was able to use it both as an event (where the event object is passed) and as a normal method call anywhere else in my code using methodName(); since having a default value for parameters makes them optional. Required parameters have no default value. You can have both as well, but optional have to come after all required. If you want to have a method that acts as an even handler and a normal method, if its "normal" use doesn't use an event parameter as the first parameter, then you'll most likely just have to use a separate method for the event handler that calls your normal event. There is also a new ...(rest) parameter that lets you specify an undetermined number of untyped arguments. Arguments that map themselves to the ...(rest) parameter are assigned to an array with the name of (rest) which you can name anything you want. I'm pretty sure I used this for the trace method in the Output class (com.senocular.utils.Output). That should use it by itself as one parameter, but you can also have required parameters come before it. You can find more information in the docs under Language Elements > Statements, Keywords & Directives > [Definition keyword summary] ...(rest) |
Thanks senocular! :beam:
Quote:
That's just what I was looking for. I had seen the 'as' operator before (in your CoilGallery, I think), but it makes much more sense when applied to a situation that I'm familiar with. |
Type Casting and the as Operator
ActionScript lets you change the assigned type of an object to other compatible types when needed. This is called casting. Both ActionScript 2 and 3 support casting using type(object) syntax. For example, if your custom class object was assigned to a variable typed as an object, you can reassign typing for that object by casting it to have a type of your custom class thereby letting Flash know what methods and properties exactly are available to that object
ActionScript Code:
ActionScript 3 introduces a new operator for casting, the as operator. The as operator replaces casting using type(object) in ActionScript 2 with a syntax of object as type. ActionScript Code:
The as operator works much like casting in ActionScript 2. If the conversion can't be made, the result of the cast is null. Otherwise, the object being cast is returned and assigned a type of the type used. ActionScript 3 still supports type(object) casting, but the behavior is a little different now. Instead of returning null for failed casting, a TypeError is thrown. Failure occurs when you try to cast an object into an incompatible type such as trying to cast one object into a type it is not associated with or inherits from. Note: ActionScript also has global conversion functions in that work in the style of Class(object) which have precedence over type(object) casting. These include String(), Number(), and Array(), etc. These don't cast so much as actually convert one object into another (where applicable). Because these have precedence over type(object) casting, its preferred that the as operator be used when casting objects to different data types. |
Unique Class Variables
In ActionScript 2, variables defined within a class's body were defined in the class's prototype object. This was an efficient means to handle variable definitions but it also meant that complex data types (such as Arrays) had a tendency to be "shared" among instances if not explicitly defined for instances within the class constructor. This is no longer a problem with ActionScript 3. Class variables defined in the class body are now unique to each class instance so this problem no longer occurs.
ActionScript Code:
|
New MouseMove Behavior
The mouseMove event has changed a little in ActionScript 3. Before, mouseMove events were global meaning that no matter where the mouse was in the Flash movie, if it moved, any listener to Mouse or any MovieClip (which automatically receive mouse events without being made listeners of Mouse) would recognise that it moved and recieve the event.
With ActionScript 3, the Mouse object is no longer used for dispatching mouse events. For mouseMove events, you would listen to InteractiveObject instances such as Sprites, MovieClips, and the Stage. So instead of adding listeners to Mouse, you would use some InteractiveObject. For each InteractiveObject instance, though, the mouseMove event is only called when the mouse moves from one location on that object to another location on that object. For a mouseMove event anywhere in the movie, you would want to add your listener(s) to the Stage instance. Given this behavior, when using mouseMove to drag objects, you would always want to use Stage as your dispatcher in the case that when you move the mouse, the location it moves is off the object your dragging. If that were to happen, the mouseMove event wouldnt not fire and your drag would stop. ActionScript Code:
|
Quote:
Doesn't it exist, just not in the way it did before? For example, ActionScript Code:
As far as dragging goes, has startDrag (and stopDrag) been improved at all? I always avoided it in Flash <9, but I tried to use it again for the AS Battle and ended up falling back on hand coding it with if constraints. The built-in function seems to be more trouble than it's worth, so is there any case where it would be better to use startDrag? (Perhaps when I have a predefined rectangle that might be changed dynamically?) |
Yeah, I meant to say its no longer an object that you add listeners too - lemme edit that ;) Mouse does exist, but for a different reason - mainly just to control whether or not the mouse cursor is seen (I think that actually may be it too)
AS for startDrag, I dont think it has changed much. |
ActionScript 1.0 Tip of the day: Upgrade
:D |
I have a suggestion for today's hint: Describe how to use AS 3.0 classes with the Flash 9 preview. I'm trying to get my gallery to work with it, though I haven't had any luck so far. (I've only had it for 10 minutes, though)
ActionScript Code:
Generates: Quote:
It does run the first line of the constructor function, though, as it traces "KrilGallery". The next line tries to access the stage, so perhaps that is part of the issue. |
Krilnon, all you do is type your class in the document class field in the property inspector then publish.
Your application class is setup to represent the actual movie being published (the document class). Just making a new instance of it on the timeline only creates an instance in a default document class and your movie won't function as expected. :) |
Oh, that makes things much more simple. Thanks :)
|
The delete Keyword and Class Members
The delete keyword in Flash is used to remove variable definitions. It doesn't delete objects from memory (this happens behind the scenes using something called the "Garbage Collector"), it just takes a variable you've created and gets rid of it so that it is no longer accessible and is no longer present through iteration (for..in loops, etc.).
Internally, the Garbage Collector, or GC for short, knows when to physicially delete objects in memory when they no longer have any variables that reference them. So, for example, if you have two variables A and B and they both reference ObjectX, deleting variable A will not cause ObjectX to be removed from memory by the GC because variable B still references it. However, if you delete both variables A and B, there will be no more references to ObjectX and the GC will recognize that it needs to be removed from memory ActionScript Code:
This works practically the same way for Flash 8 and Flash 9 (ActionScript 1, 2, and 3), though some changes were made in 8 to improve the GC. (Note: GC deletion from memory is not immediate.) Though the GC has not really changed much with ActionScript 3 and the new virtual machine that runs it, what has changed is the behavior of the delete keyword. Now, the delete keyword only works for dynamic properties of a class instance and not declared class memebers (variables or methods). With ActionScript 1 and 2, delete could be used for anything. ActionScript 3 only lets you delete dynamic variables and locks those which are not. ActionScript Code:
ActionScript Code:
Because myVar in the above example was declared as part of the class definition, it cannot be deleted using delete in ActionScript 3. Since you cannot delete class members in ActionScript 3, if you want to cause a variable to no longer reference an object or value in memory you should set your variable's value to null instead of deleting it. ActionScript Code:
If all variable references to an object are null, the GC will mark it for deletion and it will eventually be cleared from memory. |
*sigh* I wish I had your brain. :(
|
The Dictionary Class
The Dictionary class (flash.utils.Dictionary) in ActionScript 3 is a new addition to ActionScript. Dictionary objects exactly like generic Object objects aside from one thing: Dictionary objects can use any value as a property name or key as opposed to a string.
Generic objects in ActionScript use string keys (names) for property definitions. If a non-string value is used as a key, the key interpretation is the string representation of that value. Example: ActionScript Code:
If you attempt to use different objects as keys in generic objects, what you'll get iare string conversions that match each other. That means that though you have used separate objects as keys, to the object container, its the same key and they will reference the same value. ActionScript Code:
The Dictionary class is not restricted to this limitation. You can have any value as a key and that value will fully represent that key as opposed to the object using its string representation. So in the above example, if a Dictionary instance is used, you would have two separate keys, one for each object. ActionScript Code:
Though you still get [object Object] in the trace, this is a result of the string conversion in the trace command; it is a unique object key in the Dictionary instance. Note that prop here is typed as *. This is important as the keys in the dict object can be of any type. If you used String for prop's type, it would cast the a and b objects as Strings when finding them in the loop making prop "[object Object]" instead of actual references to a and b which they would need to be to correctly obtain the values 1 and 2 through dict. For generic objects, regardless of the type used for prop, you will always get a String. |
What is the point, can't you just use a regular object? I guess I'm confused as to what it would be used for.
|
Quote:
You can look at the examples and see where they are different. Objects use string keys where Dictionaries use the objects themselves. This lets you essentially use objects as variable names which can be very useful in the right circumstances. For example, consider having a bunch of "male" objects and a bunch of "female" objects, and eventually you have your objects get hitched and all get married. However, male and female objects dont have any internal property that lets you specify who their partner is, so instead you set up a reference table to let you access any female from a male or male from a female so reference[male] = female. Given any male, access it from the reference object and it returns the respective female and vise versa. Here's the thing, using normal objects, this won't work because any male or female you use as a key will be resolved as "[object Object]" and after assigning them all to your reference object, it only ends up with one value, "[object Object]" which has the value of the last male of female you assigned to it. Dictionaries, on the other hand, don't make that string conversion and would work for this. I even used a dictionary in my coil gallery to allow me to reference sprites added to the coil display. Sprites added using addItem were used with another object with coil-specific properties which was used in how the sprite was displayed in the coil display itself. Using a dictionary object, I could quickly access that wrapper by using a reference to the sprite without the need to modify anything within the sprite itself. |
Regarding a post you wrote earlier, I am a little curious about one thing... Why would anyone need * as a type? Isnt
ActionScript Code:
ActionScript Code:
|
I don't think Strings or Numbers etc are objects.
|
| All times are GMT -7. The time now is 10:17 AM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2006, Jelsoft Enterprises Ltd.
Copyright 2004 - kirupa.com