Flash Cookbook - Tricks - Conditional chains
...sending technology to the world from Budapest
This trick is about shortening actionscript code.
Even though Flash has a built-in css parser, still it's not the best one. After running some tests it came out, it does not really support a lot of things. For example you can't use hyphen in declarations, nor you can use spaces in selectors.
These things are needed for our work, therefore the only way left is to write our own parser. In this post we write the basics - think it through, take it further and you will get a nice and handy class for your own.
Plus, the StyleSheet.parseCSS() method does not support styles not surrounded by curly brackets. This is what you can use in the attributes of an xml-tag: style="font-size: 15px". This wouldn't be understood by the built-in parser.
Today I ran into a problem. As we all know, Flash has a size limit on bitmapdata size.
Basically there are two rules to follow (see the specs):
Both the width and height has to be maximum 8,191 pixels
and
the amount pixels of the bitmapdata cannot exceed 16,777,215.
Well, well, well...
You can create a nice little drawing application using the flash graphics tools. Let's say you created an application, where you press the mousebutton, start to draw and draw lines by holding the button down. Every point your mouse reached is stored in an array at an ENTER_FRAME event.
This way I did a hand-drawn rectangle, consisting 36 points. If you want to save this drawing point by point, you save this array in the SharedObjects or send it to a server-side script to save it somewhere on the server.
To invert the colors of a displayobject you can use the transform.colorTransform property of the displayobject and apply a ColorTransform to it.
Today I used the flash in-built xml capability for designing an xml:
var xml:XML = <data><obj>0</obj><obj>1</obj></data>;
trace(xml["obj"][1]); //outputs 1
when I arrived to a point, where I commented an item out:
var xml:XML = <data><obj>0</obj>/*<obj>1</obj>*/</data>;
trace(xml["obj"][1]); //outputs 1
Hm, I thought, what happened here (okay, for five minutes I didn't know what to do), I thought the flash miracoulusly cached my xml and loaded it from somewhere but not the IDE. Then came the hit: even though you think you've commented the part out, it didn't delete it from the xml. Of course if you want to comment this out, you need the standard notation.
var xml:XML = <data><obj>0</obj><!--<obj>1</obj>--></data>;
trace(xml["obj"][1]); //outputs undefined
But then what happens if you use the IDE's comment notations?
var xml:XML = <data><obj>0</obj>/*<obj>1</obj>*/</data>;
trace(xml);
Surprisingly outputs:
<data>
<obj>0</obj>
/*
<obj>1</obj>
*/
</data>
and
var xml:XML = <data><obj>0</obj><!--<obj>1</obj>--></data>;
trace(xml);
outputs:
<data>
<obj>0</obj>
</data>
Yes, xml takes whatever /**/ as a piece of the content. It doesn't matter, that you are working with flash in a flash IDE, it won't be a comment in your code if you use the XML type and its advantages (though we don't know the advantages of this initialization). Use the rules of XML in an XML type.
Yes. This is about array sorting. So there is the problem, that we want to sort an array, but want to know more about the indices. For this reason there is an option for Array.sort() function, which specifies the return value of sort():
Array.RETURNINDEXEDARRAY.
See the code below:
var array:Array = ["a", "c", "b"];
var sort:Array = array.sort(Array.RETURNINDEXEDARRAY);
trace(array, sort);
//a,c,b 0,2,1
It didn't sort the array, but returned an array with the sorted indices.
In the second part we create a static class for our random function, add a practical function and check whether it is really random.
Are you tired of the old randomgenerators? Here is a basic solution for creating your own.
Practical and basic usage of the Extensible Markup Language in the Flash environment.
Everybody has a dream about a world, where are no boundaries and everything is possible. This post is not about this dream, but asks some questions about Drag And Drop and how this event occurs. It also shows an alternative method on how things can be different.
Using Css for styling flash content? Is this possible? Yes it is, a good question would be "but why would I use css for this?". Even though this would be a good question, but still, it is possible, so we have to write about it. Also it is an external file, so is usable after compiling the flash movie, also a popular format, so people might be familiar with it. Sit back and enjoy the power of CSS and FLASH. This might be the beginning of a new technology. NOT!
Previously we wrote about global variables.
Going further, sometimes it's really useful to reach a certain function from a child of the child of the grandchild's child. Especially if we want to reach different functions from different objects. Imagine you have a url request in a certain object, and a sharedobject request in another, but somehow you need to reach both in a completely innocent small object somewhere in the depth of the jungle of the objects.
Sometimes it's useful to use global variables. In AS3 it's quite easy: you create a class and add static variables to it.
Why is it good for me? - asked the young padawan (YP).
See the reason of the problem, you will - answered the wise master (WM).
WM: Count 16 errors, we can. Easy to use, it is. Example, here is:
var error:Error = new Error('This was an error, my young padawan.');
and to call for this you do:
throw error;
YP: but what does this do, master?
WM: pops up a window with an errormessage, this.
YP: I see.
In this section we will examine the speed of loops with the three number types.
"Dear Master,
if I can create a Point using the Point.polar() method, why on earth can't I reuqest the angle of a Point? Please answer me, you are my only hope.
The Little Grasshopper"
Okay. Here we are with the second question. Can we fasten the request for length? - asked the little grasshopper.
Yes, we can. The result is a 15% speedup.
Point class is not a final class therefore we can extend it. To do this is quite useful because the main functions of points are in the package so you don't have to rewrite them. But still there are some extra things what you can override to get better results concerning speed.