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.
We can do a nice little tweak with the bookmarks. Bookmarks are usually used as bookmarks: they point to a web address. Browsers nowadays support add-ons or extensions and there is a huge palette of these nice little utilities helping us browsing faster and spend our time on the territory of world wide web. But for some people these add-ons (or extensions) are suspicious or some people cannot find the one they need for a task. A webprogrammer writes his/her own in this case, but what if the task we want to do is far easier then sitting down and struggling with an add-on (or extension).
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...
As RSS 2.0 is a well-established standard nowadays and a lot of people actually use it, it is quite useful sometimes to create an RSS feed for a blog or a news section. When creating the new ExpatTech TechBlog I really wanted to implement this feature, so I digged into the topic a bit.
The first important thing to know that an RSS is actually an XML document, so you have to comply with the XML standards, and also RSS has a well-defined standard structure that you want to follow. One nice tool is W3C's Feed Validation Service to check your feed.
Of course I wanted to generate the RSS using PHP, because the whole site uses this technology. On websites the recommended charset is UTF-8, so for the examples I will use that.
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"