開発環境 |
Apache Flex SDK 4.12.1 |
|
FlashDevelop 4.6.1 |
実行環境 |
Microsoft Windows 8.1 (64bit) |
プロジェクトの種類 |
ActionScript 3/AS3 Project |
プロジェクト名 |
SoundGenerate |
参考
Main.as
package
{
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.SampleDataEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundTransform;
import flash.text.TextField;
import flash.text.TextFormat;
public class Main extends Sprite
{
private var play:Boolean = false;
private var mySound:Sound = new Sound;
private var soundChannel:SoundChannel;
public function Main():void
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var up:State = new State(0x000000);
var over:State = new State(0xff4500);
var button:SimpleButton = new SimpleButton(up, over, over, over);
button.x = (stage.stageWidth - button.width) / 2;
button.y = (stage.stageHeight - button.height) / 2;
button.addEventListener(MouseEvent.CLICK, onMouseClick);
addChild(button);
mySound.addEventListener(SampleDataEvent.SAMPLE_DATA, sineWaveGenerator);
}
private function onMouseClick(e:MouseEvent):void
{
if (play)
{
soundChannel.stop();
play = false;
}
else
{
var st:SoundTransform = new SoundTransform;
st.volume = 0.3;
soundChannel = mySound.play(0, 0, st);
play = true;
}
}
private function sineWaveGenerator(e:SampleDataEvent):void
{
for (var i:int = 0; i < 8192; i++)
{
var n:Number = Math.sin((i + e.position) / Math.PI / 4);
e.data.writeFloat(n);
e.data.writeFloat(n);
}
}
}
}
State.as
package
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
public class State extends Sprite
{
public function State(color:uint)
{
graphics.lineStyle(2, color);
graphics.beginFill(0xffffff);
graphics.drawRect(0, 0, 100, 50);
graphics.endFill();
var tf:TextField = new TextField;
tf.defaultTextFormat = new TextFormat("_typeWriter", 20, color, true);
tf.text = "Click";
tf.autoSize = "left";
tf.x = (width - tf.width) / 2;
tf.y = (height - tf.height) / 2;
tf.selectable = false;
addChild(tf);
}
}
}
最終更新:2014年06月21日 12:43