A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: setInterval oddity

  1. #1
    Member
    Join Date
    Oct 2004
    Posts
    32

    setInterval oddity

    whan I use this code to make mc fade out, I got alpha value change in about 1.27 every step instead of 1.

    code:
    function change_alpha()
    {
    mc._alpha --
    trace(mc._alpha)
    }

    setInterval(change_alpha,50)



    mc fades allright but what with the decimal fraction?
    Fish

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    This is because internally, alpha is stored as value that goes from 0-255
    (it's an 8-bit binary value). It's done this way because it's faster to the arithmetic since the R,G,B values of the colors are also 8-bit values.

    Consider the following:

    mc._alpha = 76;
    trace(mc._alpha); // result: 75.78125

    76 gets converted internally to an integer from 0-255.

    76/100 * 256 = 194 (plus some change).

    194/256 * 100 = 75.78125

    look familiar?

    This means that you can actually step the alpha down by less than 1, if you want. The smallest step you can make is 1/256 converted to a 1-100 scale or 100/256 (.390625)

    Consider the following code, which steps the alpha down by this step...

    code:

    function change_alpha()
    {
    mc._alpha -= 100/256;
    trace(mc._alpha)
    }

    setInterval(change_alpha,50);



    Results are:

    99.609375
    99.21875
    98.828125
    98.4375

    - Jim
    Last edited by jbum; 10-19-2004 at 05:53 PM.

  3. #3
    Member
    Join Date
    Oct 2004
    Posts
    32
    Thanx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center