ActionScript 3 Cookbookからの抜粋(六)


1.テキストハイパーリンクイベントのキャプチャ
TextEvent.LINK
========================================================================
 
2、ユーザーシステムのフォントを取得する
trace(TextField.fontList);
========================================================================
 
3、可視化オブジェクトの色を変える
var color:ColorTransform = sampleSprite.transform.colorTransform;color.rgb = 0xFFFFFF;sampleSprite.transform.colorTransform = color;
========================================================================
 
4、Matrixクラス(斜め拡大・縮小)
flash.geom.Matrixクラスはa,b,c,d,tx,ty属性を定義する.bとcは傾斜度を決定する(aとdはスケール比を決定し、txとtyはxとyの平行移動値を決定する).b属性はY座標の傾き値を決定し、c属性はX座標の傾き値を決定し、bとcはデフォルトで0で、値が大きいほど下と右に傾きます.
既定値は(a=1,b=0,c=0,d=1,tx=0,ty=0)です.
========================================================================
 
5、タイマー
_timer = new Timer(30);_timer.addEventListener("timer", onTimer);_timer.start( );
========================================================================
 
6、指定したサブストリングが文字列に含まれているかどうかを検索
String.indexOf(substr)
すべてのサブストリングをループで検出
while ( ( index = example.indexOf( "cool", index + 1 ) ) != -1 ) {    trace( "String contains word cool at index "+ index );}
========================================================================
 
7、join説明:「
」を「」に置き換え、join()メソッドは新しい区切り記号を再追加する
trace( example.split( "
").join( '' ) );
========================================================================
 
8、Stringのencodeとdecode
public static function encode( original:String ):String {
	// The codeMap property is assigned to the StringUtilities class when the encode( )
	// method is first run. Therefore, if no codeMap is yet defined, it needs
	// to be created.
	if ( codeMap == null ) {
		// codeMap                    
		codeMap = new Object( );
		//      0 255   
		var originalMap:Array = new Array( );
		for ( var i:int = 0; i < 256 ; i++ ) {
			originalMap.push( i );
		}
		//                   
		var tempChars:Array = originalMap.concat( );
		//           
		for ( var i:int = 0; i < originalMap.length; i++ ) {
			var randomIndex:int = Math.floor( Math.random( ) * ( tempChars.length - 1 ) );
			codeMap[ originalMap[i] ] = tempChars[ randomIndex ];
			tempChars.splice( randomIndex, 1 );
		}
	}
	var characters:Array = original.split("");
	for ( i = 0; i < characters.length; i++ ) {
		characters[i] = String.fromCharCode( codeMap[ characters[i].charCodeAt( 0 ) ] );
	}
}

public static function decode( encoded:String ):String {
	var characters:Array = encoded.split( "" );
	if ( reverseCodeMap == null ) {
		reverseCodeMap = new Object( );
		for ( var key in codeMap ) {
			reverseCodesMap[ codeMap[key] ] = key;
		}
	}
	for ( var i:int = 0; i < characters.length; i++ ) {
		characters[i] = String.fromCharCode( reverseCodeMap[ characters[i].charCodeAt( 0 ) ] );
	}
	return characters.join( "" );
}

//  
var example:String = "Peter Piper picked a peck of pickled peppers.";
var encoded:String = StringUtilities.encode( example );
trace( StringUtilities.decode( encoded ) );

 ========================================================================
 
9、音声再生
//緩衝しない
var _sound: Sound = new Sound(new URLRequest("1.mp3"));
_sound.play();
//バッファ5秒
var request:URLRequest = new URLRequest("1.mp3");var buffer:SoundLoaderContext = new SoundLoaderContext(5000);_sound = new Sound( );_sound.load(request, buffer);_sound.play( );
//ループ再生、最初のパラメータがスタート再生位置
_sound.play(0, int.MAX_VALUE);
========================================================================
 
10、音声読み込み進捗バー
package {
	import flash.display.Sprite;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.events.Event;
	public class ProgressBar extends Sprite {
		public function ProgressBar( ) {
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			_sound = new Sound(new URLRequest("song.mp3"));
			_sound.play( );
		}
		public function onEnterFrame(event:Event):void
		{
			var barWidth:int = 200;
			var barHeight:int = 5;
			var loaded:int = _sound.bytesLoaded;
			var total:int = _sound.bytesTotal;
			if(total > 0) {
				// Draw a background bar
				graphics.clear( );
				graphics.beginFill(0xFFFFFF);
				graphics.drawRect(10, 10, barWidth, barHeight);
				graphics.endFill( );
				// The percent of the sound that has loaded
				var percent:Number = loaded / total;
				// Draw a bar that represents the percent of
				// the sound that has loaded
				graphics.beginFill(0xCCCCCC);
				graphics.drawRect(10, 10,
				barWidth * percent, barHeight);
				graphics.endFill( );
			}
		}
	}
}

 ========================================================================
 
11、音声間の再生/一時停止
package {
	import flash.display.Sprite;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	public class PlayPause extends Sprite {
		private var _sound:Sound;
		private var _channel:SoundChannel;
		private var _playPauseButton:Sprite;
		private var _playing:Boolean = false;
		private var _position:int;
		public function PlayPause( ) {
			// Create sound and start it
			_sound = new Sound(new URLRequest("song.mp3"));
			_channel = _sound.play( );
			_playing = true;
			// A sprite to use as a Play/Pause button
			_playPauseButton = new Sprite( );
			addChild(_playPauseButton);
			_playPauseButton.x = 10;
			_playPauseButton.y = 20;
			_playPauseButton.graphics.beginFill(0xcccccc);
			_playPauseButton.graphics.drawRect(0, 0, 20, 20);
			_playPauseButton.addEventListener(MouseEvent.MOUSE_UP,
			onPlayPause);
		}
		public function onPlayPause(event:MouseEvent):void {
			// If playing, stop. Take note of position
			if(_playing) {
				_position = _channel.position;
				_channel.stop( );
			}
			else {
				// If not playing, re-start it at
				// last known position
				_channel = _sound.play(_position);
			}
			_playing = !_playing;
		}
	}
}