Drawing A Sine Wave In Actionscript
Just a simple way to draw a sine wave with a specific amplitude and frequency. Use the sliders to adjust the values.
Here’s the main function:
function drawWave(amp:int, freq:int):void{ newSine.graphics.clear(); newSine.graphics.lineStyle(2, 0xff0000, 0.4); newSine.graphics.moveTo(0, yaxis); for(var i:int=0; i<=stage.stageWidth; i++){ var ang:Number = 2 * Math.PI * freq * i/stage.stageWidth; newSine.graphics.lineTo(i, yaxis - amp*Math.sin(ang)); } }
(EDIT 8/14/09: In the SWF above, the sine wave is upside down. That’s because I had a typo when I wrote it. On line 7 of the code, I had typed “…yaxis + amp…” but it should be minus. The code above is correct, but the SWF hasn’t been updated)
Although I don’t know much about as3, i find this stuff really interesting. Good stuff man! Keep up the good posts.
thanks jake!
compadre no tienes por ai el ejecutable, te lo agradeceria mucho. gracias
Hey Adam,
Nice work! I noticed that the wave sometime disappear on a page refresh. When the sliders are adjusted though, the wave arrears at the top of the stage. Any idea why this happens? I’d love to see how this was put together. Can you share the fla? Much Thanks.
@Steve
Unfortunately I don’t have the source anymore, but the code that is posted above is all you need. The only thing you need to set is the “yaxis” variable, which is just the point on the stage where your y-axis is at zero.
i cant get the sine wave can any one help me
@shine
yes.
worked like charm. thanks a lot !
Hi,
So that’s a nifty little function. I’m wondering if you would know how to plot a specific point along that sine?
Specifically, I want to place a round movie clip randomly along the sine I just drew.
Wil