Home > actionscript, flash > Drawing A Sine Wave In Actionscript

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.
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

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)

  1. August 7th, 2009 at 12:18 | #1

    Although I don’t know much about as3, i find this stuff really interesting. Good stuff man! Keep up the good posts.

  2. August 7th, 2009 at 12:36 | #2

    thanks jake!

  3. toshe
    May 19th, 2010 at 00:59 | #3

    compadre no tienes por ai el ejecutable, te lo agradeceria mucho. gracias

  4. Steve
    June 25th, 2010 at 08:24 | #4

    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.

  5. July 1st, 2010 at 11:15 | #5

    @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.

  6. shine
    September 18th, 2010 at 00:40 | #6

    i cant get the sine wave can any one help me

  7. September 20th, 2010 at 09:47 | #7

    @shine
    yes.

  8. January 12th, 2011 at 17:31 | #8

    worked like charm. thanks a lot !

  9. Wil
    June 24th, 2011 at 16:49 | #9

    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

  1. No trackbacks yet.
*