Friday, February 23, 2007

Using Math.cos() to make an object swing

Math.cos() is often used in many animation applications, such as when you need to determine the x and y locations of an object that revolves around another object in a circle. I know many people see the word "cos" and think "oh no, not trigonometry!" But don't worry. This technique does not require you to understand trigonometry, but it definitely helps to know it.

The result of our swinging object should look like this:


How do we achieve this using the Math.cos() method? Well, leaving out the trigonometry terms, we know that if we give its parameter a number n, where n can be any number, it will always give back a number between -1 and 1. This characteristic allows us to write a formula that computes the rotation property of an object at any given point:

    mc._rotation = Math.cos(n)*force;

The variable force basically enhances the effect of the number, because the one given by Math.cos() is so small (less than 1). So all we need to do now is increment n:

    var force = 20;
    var n = 0;
    function swing() {
        n += 0.3;
        this._rotation = Math.cos(n)*force;
    }
    mc.onEnterFrame = swing;

This technique can be useful in other animation applications you might have seen, such as falling leaves:

Download the source

or Newton's Cradle:

Download the source

You can also download the source of the first example here. And for a more thorough understanding of trigonometry and its real world applications to animation, I encourage researching into it more, because this post is only breaking the tip of the iceberg.

Wednesday, February 21, 2007

Another Firefox plug-in made for Flash developers

https://addons.mozilla.org/firefox/3649

This plug-in allows you to switch between different versions of Flash players instantly, making testing your work across different versions of Flash players much easier. This is only available for Firefox. So if you don't have Firefox, find the link on the right and try it out.

Tuesday, February 13, 2007

Live HTTP Header solved my Sound problem

For Firefox: http://livehttpheaders.mozdev.org/installation.html

For IE: http://www.blunck.se/iehttpheaders/iehttpheaders.html

The HTTP Header contains some of the most useful data a Flash developer can have. It acts like the trace window, except it works when your Flash file lives on a live server. It tells you everything that is being requested and downloaded by your web page, including your Flash movie. For example, if you use the loadSound() method, it will tell you the file being called and whether if it exists. Although the Sound.onLoad() handler might get the job done, it seems weak at it such that it's unable to detect/handle certain server errors, such as a drop in connection or a memory leak in the browser running the Flash movie. This became evident to me in a project I worked on that required me to load multiple external sounds that would play one after another; the Sound.onLoad() handler folded like a bad poker hand. The HTTP Header prompt showed the file getting loaded but Sound.onLoad() never fired. My workaround was to use onEnterFrame to check the size of the file to determine the existence of it. Something simple like this:


    function checkSoundLoaded() {
        if(Sound.getBytesTotal() != undefined){
            if(Sound.getBytesLoaded() >= Sound.getBytesTotal()) {
                delete this.onEnterFrame;
            }
        }
    }
    this.onEnterFrame = checkSoundLoaded;



Of course, the actual code should do more, such that if after a certain number of tries it does not succeed, it should move on. But it should give you the basic idea.

Thursday, February 8, 2007

I live, sleep and breathe Flash (and ActionScript).

Welcome! As the title suggests, my ability to learn Flash and willingness to tackle a scripting obstacle would not be possible without my obsession with it. The best way to learn is to teach. Therefore I will throw my ideas and tricks out there and hope to learn from those who are willing to reciprocate. I also plan to, of course, post any code and source files that may help in facilitating such conversations.