In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

保存サンプル

// write as3 code here..
package  {
	import com.adobe.images.JPGEncoder;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.net.FileReference;
	import flash.events.*;
	import flash.net.FileReferenceList;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.utils.ByteArray;
	
	public class Main extends Sprite{
		private var txt:TextField;
	
		public function Main() {
			
			//テキスト入力
			txt = new TextField();
			txt.width = 400;
			txt.multiline = true;
			txt.text = "ここのテキストフィールドは変更できます☆\r\n改行";
			txt.border = true;
			txt.type = TextFieldType.INPUT;
			txt.x = 20;
			txt.y = 20;
			addChild(txt);
			
					
			//保存ボタン(JPG)
			var jpgSave:TextField = new TextField();
			CreateSaveButton(jpgSave, "JPGで保存",20, 340 );
			addChild( jpgSave );

			jpgSave.addEventListener(MouseEvent.CLICK, SaveFile(SaveJPG, "test.jpg"));			
			//保存ボタン(TXT)
			var txtSave:TextField = new TextField();
			CreateSaveButton(txtSave, "TXTで保存",20, 380 );
			addChild( txtSave );
			txtSave.addEventListener(MouseEvent.CLICK, SaveFile(SaveTXT, "test.txt"));	
		}
		
		/**
		 * セーブボタンのデフォルト設定
		 * @param	button
		 */
		private function CreateSaveButton( button:TextField, bName:String , x:int, y:int) : void
		{
			button.text = bName;
			button.y = y;
			button.x = x;
			button.width = 100;
			button.height = 20;
			button.background = true;
			button.backgroundColor = 0x888888;
			button.textColor = 0x000000;
			button.selectable = false;
		}
		
		
		private function SaveFile(encFunc:Function, fileName:String):Function {
		
			return function(e:MouseEvent) : void
			{
				
				var ff:FileReference = new FileReference();
				ff.addEventListener(Event.OPEN, function(e:Event):void { trace("open");  } );
				ff.addEventListener(ProgressEvent.PROGRESS, function(e:ProgressEvent):void { trace("progress");  } );
				ff.addEventListener(Event.COMPLETE, function(e:Event):void { trace("complete");  } );
				ff.addEventListener(Event.CANCEL, function(e:Event):void { trace("cancel");  } );
				ff.addEventListener(Event.SELECT, function(e:Event):void { trace("select");  } );
				ff.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void { trace("ioError");  } );
				
				
				
				ff.save(encFunc(), fileName);
			}

		}
		
                //  JPGでセーブ
		private function SaveJPG() : ByteArray
		{
			var bmp_data:BitmapData = new BitmapData(txt.width, txt.height);
			
			bmp_data.draw(txt)
			
			var jpgEncoder:JPGEncoder = new JPGEncoder(80);
			var jpgData:ByteArray = jpgEncoder.encode(bmp_data); 

			return jpgData;
		}
		
		// TXTでセーブ
		private function SaveTXT() : String
		{
			return txt.text;               
		}

	}
	
}