| Ultrashock Forums
• ActionScript Tips Thread |
|||
![]() |
||||
| Search this Thread | Thread Tools | Display Modes |
| 12> | Page 1 of 2 |
|
|
|||||||||||||||||||||||||
![]() |
Ultrashock Member Comments:
17 Creative Assets
|
2006-11-04
#2 |
||
|
001 - Delete all Movie Clips on a Timeline
If you ever need to remove all of the movie clips on a timeline you could use the following function. Keep in mind that this will only remove movie clips that have been added using attachMovie, createEmptyMovieClip, or duplicateMovieClip. ActionScript Code:
|
17 Creative Assets
|
2006-11-04
#3 |
||
|
002 - Aligning Movie Clips
The following function will allow you to align movie clips horizontally or vertically. You can also set the amount of spacing between each movie clip. ActionScript Code:
|
17 Creative Assets
|
2006-11-04
#4 |
||
|
003 - Add Classes to MovieClips at Runtime
Normally you would link a class to a library symbol if you wanted to add a class to a MovieClip. You can however take advantage of __proto__ if you want to add a class to a movie clip at runtime. ActionScript Code:
The only downside to this is that physical changes to the movie clip don't seem possible from the class constructor. For example, this._x = 0 in the following class has no effect on the movie clip. ActionScript Code:
|
17 Creative Assets
|
2006-11-04
#5 |
||
|
004 - Gathering FlashVars
If you send variables to your Flash movie using flashVars the following function could be used to gather all of the variables up into an object. Number values are converted to true numbers, and you can also name your flashVars so they are recreated as arrays. Here's the function: ActionScript Code:
01 - Basic example HTML Code:
<param name="flashVars" value="fv_foo=hello&fv_bob=123" /> ActionScript Code:
02 - Sending Arrays To send an array to Flash you simply add _0 _1 _2 etc to the end of your variable name. HTML Code:
<param name="flashVars" value="fv_items_0=apple&fv_items_1=banana" /> ActionScript Code:
|
17 Creative Assets
|
2006-11-05
#6 |
||
|
005 - Multiple onEnterFrame Functions
If you want to be able to quickly and easily assign multiple functions to a movie clip's onEnterFrame event then the following code should give you something to play with. ActionScript Code:
ActionScript Code:
-- function one -- function two Removing functions is done in the same way. ActionScript Code:
ActionScript Code:
|
17 Creative Assets
|
2006-11-05
#7 |
||
|
006 - Prototype Functions
Last edited by Codemonkey : 2006-11-11 at 03:00.
Prototype functions can be used if you want to add functions to Flash's core classes such as String, Array, Number, and so on. When you create a prototype function, all instances of the prototyped class will be able to access the new function. For example, let's say that you want to be able to reverse any of your strings at any time in your movie. To do that you would add a prototype function to the String class like this: ActionScript Code:
ActionScript Code:
ActionScript Code:
ActionScript Code:
|
|
2006-11-05
#8 |
||
|
007 - Measuring frames per second
Last edited by Codemonkey : 2006-11-07 at 13:11.
A lot of the time while debugging/profiling, you'll be interested in how many FPS your movie is actually pulling to see if there's something slowing it down. Here's a class that let's you specify the update interval and invokes a method each time. You can drop this class in a util folder and use it in any of your projects: ActionScript Code:
To use it: ActionScript Code:
|
|
2006-11-05
#9 |
||
|
008 - Converting coordinates
To quickly convert a coordination in one movieclip to another movieclip, you can use the following function: ActionScript Code:
|
|
2006-11-05
#10 |
||
|
009 - Array item lookup/removal
If you have an indexed array (array with 1, 2, 3, 4, ... indexes) and you want to know the index of a certain object, you can use the following function: ActionScript Code:
ActionScript Code:
ActionScript Code:
You could also add these function to the Array class with prototyping as Nutrox explained in Tip 006. Or, you can add these methods to a global util class as explained in Tip 010 |
|
2006-11-05
#11 |
||
|
010a - Creating a utility class
Last edited by Codemonkey : 2006-11-06 at 11:56.
If you have some functions you might use anywhere and possibly in different projects, you might want to create util classes from them. A util class is a class available from a global path with static methods only. Flash' Math class is a good example of this. To create a util class start with an empty class like this: ActionScript Code:
ActionScript Code:
If you want to prohibit people from instantiating a utility class - which would also mean such a people didn't 'get' your utility class - you can add a private constructor: ActionScript Code:
ActionScript Code:
ActionScript Code:
|
|
2006-11-05
#12 |
||
|
010b - Making a utility class global
Last edited by Codemonkey : 2007-05-06 at 03:24.
To actually make your util class visible across your projects, you need to add it to your flash' IDE. To do this, go to: Edit->Preferences->ActionScript->ActionScript Settings button. In there, click the button "Browse to Path" to select the folder your util class is in. For example, my global path is "D:\MyStuff\Flash\local". screenshot global paths To use the global util class in your project, you can now just use a simple import statement without having to worry about whether it finds it: ActionScript Code:
|
|
2006-11-05
#13 |
||
|
011 - Quick random number
If you quickly need a random number in a certain range, then you can use this simple (util) function: ActionScript Code:
|
|
2006-11-05
#14 |
||
|
012 - Clipping a number
Last edited by Codemonkey : 2006-11-05 at 09:35.
If you want to make sure a number is within a certain range or clip it otherwise, here's a simple way to do this: ActionScript Code:
|
17 Creative Assets
|
2006-11-05
#15 |
||
|
013 - Loading, Bytes Per Second and ETA
The following code is an example of how you could calculate the value for "bytes per second" and "remaining time" when loading external movies or images etc. I will wrap this up in a class soon. ActionScript Code:
|
|
2006-11-05
#16 |
||
|
014 - fading your mx.screens.Forms
Last edited by Codemonkey : 2007-05-06 at 03:25.
If you are working with Forms (mx.screens.Form), you can use this enhanced version to make your screens smoothly fade in/out when showing/hiding your form: ActionScript Code:
ActionScript Code:
|
|
2006-11-05
#17 |
||
|
015a - Creating sounds
To create new sound objects that you can individually control, use can use the following function: ActionScript Code:
ActionScript Code:
|
|
2006-11-05
#18 |
||
|
015b - Creating enhanced sounds
Last edited by Codemonkey : 2006-11-11 at 03:18.
To make the sound objects more manageble, you can embed it in a container that 'adds' functionality to a sound object. For example, you can simplify playing, status checks, looping etc. by adding functions for that to the sound's container. Here's an example: ActionScript Code:
|
17 Creative Assets
|
2006-11-05
#19 |
||
|
016 - Realtime Clock (tick tock)
Copy and paste the following code into a new Flash document for an instant clock. :) ActionScript Code:
|
17 Creative Assets
|
2006-11-05
#20 |
||
|
017 - Removing Non-dynamic Movie Clips
Normally you can't use removeMovieClip() on movie clips that you place on the Stage while authoring the movie. However, swap the movie clip depth to a positive value and removeMovieClip() will work fine! ActionScript Code:
|
|
2006-11-06
#21 |
||
|
018 - __resolve your properties and methods
Last edited by Codemonkey : 2006-11-11 at 03:20.
A less known and even less used feature in actionscript is the __resolve method. If you declare a method called __resolve on your class and someone tries to call a property or method of your class that doesn't exist, flash will look for a __resolve instead and call that one. You will get a parameter with the name of the unknown property/method that the user is trying to call. In addition, you can use the arguments variable inside __resolve to uncover any arguments the user passed. You can call properties/methods that don't exist on an object when:
Be cautious though, __resolve doesn't *always* work as you would expect and you should have a very specific reason where you think __resolve is the solution. |
|
2006-11-06
#22 |
||
|
019 - InspectableList metatag
Last edited by Codemonkey : 2006-11-07 at 08:50.
If you are creating your own components and you are extending an existing component class to do that, it may happen that you want to hide inherited public properties from the Component Inspector in Flash. The answer is a Flash meta tag for components called InspectableList. While the component inspector knows what properties to list from each separate property that has Inspectable before it, you can override this list by using the InspectableList meta tag. For example, when you know a component works with public x and y Numbers, but you want to work with a setPos() method and hide the x and y properties, you need a way to do this clean and simple (at least to users in the component inspector): ActionScript Code:
ActionScript Code:
|
|
2006-11-06
#23 |
||
|
020 - Tip 20
There is no Tip 20. |
|
2006-11-07
#24 |
||
|
021 - Bits as Flags
Last edited by Codemonkey : 2006-11-12 at 01:38.
If you want a clean and efficient way of testing against flags, you can use bit-wise operators. Define your flags as numbers like 2, 4, 8, 16 (double for each new flag constant): ActionScript Code:
ActionScript Code:
ActionScript Code:
The key to this mechanism is being able to use many different flags together in one number variable. Take the Array class for example. If you want to sort an Array with the method Array.sort(), you can use constants from the Array class to indicate how the sort should happen. Those constants are flag numbers you bitwise combine to quickly, cleanly and efficiently let the sort method know your needs. --- You can store an entire chessboard in one variable, because there are 64 spaces in a chess board and you can have 64 different single bits in a Number (and each bit is represented by 2, 4, 8, etc...). Even better, you can do cool stuff like: if (blackHorse & whitePieces) { // make black horse take whatever piece is occupying the same space } This principle is also known as bitboards, or boards of bits. It's one of the optimizations to make chess AI ultrafast. --- |
|
2006-11-07
#25 |
||
|
022 - Removing a single bitflag
Last edited by Codemonkey : 2006-11-12 at 02:08.
  As you now know, you can combine flags to a combined flag, which means you join the ones '1' into a single number. Then you can check that number against a single flag whether it occurs. To remove a single flag from such a combined number, you simply AND it with the opposite of the single bit flag. Huh? Take a look: ActionScript Code:
--- With the '~' operator you can flip all zeros and ones so that the bit representation is exactly the opposite (ie. 11001 becomes 00110). AND'ing that to the combined flag means: keep everything of the original number, but throw away what's behind the AND. This works because that number has zeros '0' where the ones '1' were and AND'ing with zeroes won't work. In math, there's actually a special operator for this kind of thing. instead of AND'ing with the opposite of a flag, you can NAND a flag. NAND stands for Not And, which is exactly the same. --- Here's another example with a rabbit that drinks blood when it is full moon: ActionScript Code:
|
|
2006-11-07
#26 |
||
|
023 - List all flags in a combined variable
Last edited by Codemonkey : 2006-11-12 at 02:45.
  Sometimes it is useful to list all the single flags in a combined flag number. For example for debugging, logging or some other operation. You can do this by AND'ing all single flags by hand like this: ActionScript Code:
ActionScript Code:
ActionScript Code:
|
|
2006-11-12
#27 |
||
|
024 - Bitflags by bitshifting
Last edited by Codemonkey : 2006-11-28 at 02:10.
  When you add new flags, move them around or change them a lot, it can be an annoyance to have to update all the flag values each time, making sure they are unique and doubled each time. To make things a little bit easier, you can use the bitshift operator. The bitshift operator 'knows' you want to store a value as a bit representation, like all the flags we've been defining. Here's an example: ActionScript Code:
ActionScript Code:
|
17 Creative Assets
|
2006-11-13
#28 |
||
|
025 - Shared Library, go away! (AS3)
Shared Libraries can be summed up in four words: pain in the ass. They can still be used with AS3 but why bother with them when there is an easier way to pull library assets from SWF files! Let's say that you have a SWF and in that SWF file's library is a sound file linked to the first frame of the movie with the class name "MySound". The following code demonstrates how to load that SWF file into another SWF file, grab the sound from the library, and then play the sound. Woohoo! ActionScript Code:
Have fun!
|
|
2006-11-15
#29 |
||
|
026 - Listening in on the listeners
If you use the EventDispatcher to allow listeners to listen for events on your object, your object gets a list for these listeners and a bunch of functions like dispatchEvent etc. To access this array of listeners, use the property "__q_" on your object. This is the name of the array the EventDispatcher adds to your object: Setting up the event dispatching object and a listener: ActionScript Code:
ActionScript Code:
|
|
2006-11-15
#30 |
||
|
027 - Fixing scope with Delegates (as2)
Last edited by Codemonkey : 2006-11-28 at 02:11.
If for some reason you think "Why is this or that tracing undefined??" or "Why isn't my function executing omg!". Then for the love of God, think of Delegates!! Search Ultrashock on the word "scope" and "Delegate" and don't ask us again please ![]() Classic example: ActionScript Code:
Fix this with a Delegate: ActionScript Code:
|
17 Creative Assets
|
2006-11-15
#31 |
||
|
028 - Delegate Alternative
Last edited by Nutrox : 2006-11-22 at 05:23.
Following on from Codemonkey's look at the Delegate class, I actually use my own delegate-like class for fixing scope. This version allows you to pass custom arguments to the target method without overwriting any arguments that Flash might spit out, the standard Delegate class will overwrite those arguments. Here is the AS2 class: ActionScript Code:
ActionScript Code:
|
17 Creative Assets
|
2006-11-19
#32 |
||
|
029 - Math.floor Alternative
You can use the bitwise right-shift operator to floor a number instead of using Math.floor(). Normally Math.floor() is absolutely fine to use, but if you are concerned about CPU cycles then this alternative will come in handy because it is a lot quicker than Math.floor(). Here is an example using Math.floor()... ActionScript Code:
ActionScript Code:
|
17 Creative Assets
|
2006-11-19
#33 |
||
|
030 - Rounding Numbers
Here is a little utility function that you can use to round numbers to a certain decimal point. It is very easy to use so I'll just drop some example code here... ActionScript Code:
|
17 Creative Assets
|
2006-11-22
#34 |
||
|
031 - Array Mapping
Last edited by Nutrox : 2006-11-22 at 05:29.
Array mapping can come in very useful sometimes. What array mapping basically does is pass each array value to a function, and that function can then either alter the array value or return a result based on the array value. I have included a prototype function and a standard function here just in case you prefer a particular flava. ![]() You setup the Array prototype method like this: ActionScript Code:
Prototype Example 1 This example checks to see which values in the myNumbers array are greater than 3. The results (in this example true or false) will be returned in an array. ActionScript Code:
This example will prefix each value in the myNames array with "Name: ". ActionScript Code:
We have left the world of prototypes now. This is the same example as Prototype Example 1 but instead of using a prototype a standalone function is used for the array mapping. ActionScript Code:
PS: Array mapping is built-in to the AS3 Array class, so these examples are only useful for AS1 / AS2. |
|
2006-11-25
#35 |
||
|
032 - Calling methods with eval()
You can't dynamically call functions with eval() like in Javascript or PHP unless you create your own parser for it, as well as for assignments etc. Flash can only locate identifiers for you with eval and that isn't necessary anymore these days. If you want a quick way to call certain functions using a string, here's one way to do that (in AS2 at least): ActionScript Code:
|
17 Creative Assets
|
2006-11-25
#36 |
||
|
033 - Array Rotation
Last edited by Nutrox : 2006-11-25 at 07:52.
In a nutshell: Array rotation simply means shifting the array values to new positions while keeping their relative order. Here is a quick example followed by the prototype version: ActionScript Code:
ActionScript Code:
|
17 Creative Assets
|
2006-11-27
#37 |
||
|
034 - Colour Manipulation
Last edited by Nutrox : 2007-11-15 at 00:54.
Using the bit-shifting techniques that Codemonkey has covered allows you to quickly and easily manipulate colour values. This first example demostrates how you can get the individual colour values from a RGB colour value: ActionScript Code:
ActionScript Code:
|


), so please post any comments or questions in the 
45 comments
| 11009 views


17