3 Replies Latest reply on Dec 6, 2011 9:32 AM by kglad

# hi! i need help editing AS2 for a sound Simulated Spectrum

hi i took this code from here http://www.pixelhivedesign.com/tutorials/Simulated+Flash+Audio+Spectrum+Analyzer/

but i need to make that the color of the bars (little squares) change in gradient form (for example from red to green vertically) i don't know if i have to use a var or a function... i don't really know how to do it, mostly in wich part i have to insert that new code

here's de AS.:

1. // ---------------------------------------------
2. // Simulated Spectrum Analyzer
3. // Author: Max Langdon - max@pixelhivedesign.com
4. // Site: www.pixelhivedesign.com
5. // ---------------------------------------------
6. spacing   = 1// Spacing between bars.
7. margin   = 3; // Shift all bars to right.
8. tot_bars  = 21// Total Spectrum Bars.
9. max_height  = 20// Maximum height of line in bars.
10. // ----------------------------------
11. // The spectrum definitions.
12. // ----------------------------------
13. // low  = lowest the line(s) will go.
14. // high = highest the line(s) will go.
15. // n  = number of lines affected.
16. // Make sure the sum of your n's = tot_bars.
17. // Example Rock Spectrum.
18. rock_spectrum = [
19.         {low:5,high:20,n:2},
20.         {low:12,high:20,n:8},
21.         {low:3,high:15,n:3},
22.         {low:7,high:15,n:3},
23.         {low:5,high:10,n:3},
24.         {low:0,high:5,n:2}
25.         ];
26. // Start the spectrum analyzer.
27. analyzer(rock_spectrum);
28. // ----------------------------
29. // Simulated Spectrum Analyzer
30. // ----------------------------
31. // spectrum : Array Spectrum Definition.
32. // isOn  : Boolean - Turn on or off Analyzer
33. function analyzer(spectrum){
34.   // Index of first bar range, first {low:#, high:#, n:#}
35.   var curRange = 0;
36.   // Number of lines the current range will affect.
37.   var numLines = spectrum[curRange].n;
38.   // Generate the vertical bar lines.
39.   for(var i = 0; i < tot_bars; i++){
40.     // Attach a one_bar movieclip.
41.     var bar = this.attachMovie('one_bar','bar'+i,(i+1));
42.     // Position the movieclip based on its width,
43.     // spacing and margin specified in our variables.
44.     bar._x = i * (bar._width + spacing) + margin;
45.
46.     // Set the bar's range to the current range.
47.     bar.r = spectrum[curRange];
48.     // Animate the bar vertically.
49.     bar.onEnterFrame = function(){
50.       // Choose a random number between the range we specified.
51.       var h = Math.round(Math.random() * (this.r.high - this.r.low) + this.r.low);
52.       // Using the random height, we generate more bars accordingly.
53.       for(var i = 0; i < h; i++){
54.         // Attach another bar.
55.         var bar = this.attachMovie('one_bar','bar'+i,(i+1));
56.         // Put it above the last bar.
57.         bar._y -= i * (bar._height + spacing);
58.         // Immediatly start fading the bar.
59.         bar.onEnterFrame = function(){
60.           this._alpha -= 20;
61.           // If completely faded, remove the MovieClip.
62.           if(this._alpha == 0) this.removeMovieClip();
63.         }
64.       }
65.     }
66.     // Countdown the number of lines we setup.
67.     numLines--;
68.     // If we reach 0, it is time to get the next range from our spectrum array.
69.     if(numLines == 0) curRange++, numLines = spectrum[curRange].n;;
70.   }
71. }
72. // --------------------------
73. // Turning off the Analyzer.
74. // --------------------------
75. function turnOff(){
76.   // Loop through the number of vertical lines.
77.   for(var i = 0; i < tot_bars; i++){
78.     var bar = this['bar'+i];
79.     // Stop the animation.
80.     delete bar.onEnterFrame;
81.     // Loop through the number of bars on the line, except first one.
82.     for(var j = 1; j < max_height; j--){
83.       // Remove the bars.
84.       var b = bar['bar'+j].removeMovieClip();
85.     }
86.   }
87.
88. }