`

ActionScript3 Cookbook中摘取(六)

    博客分类:
  • Flex
阅读更多

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说明:将"<br>"替换为"\n",join( )方法重新加入新的分隔符

trace( example.split( "<br>" ).join( '\n' ) );

========================================================================

 

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;
		}
	}
}

 

分享到:
评论

相关推荐

    ActionScript 3.0 Cookbook 中文完整版

    ActionScript 3.0 Cookbook 中文完整版.pdf

    ActionScript 3.0 CookBook 中文翻译

    压缩包中的"FLASH-FLEX3[1].0开发中文版+完整版+.pdf"文件包含了全书的完整内容,读者可以通过阅读其中的章节,了解和学习如何利用ActionScript 3.0来实现各种功能,例如: 1. 类和对象:学习如何定义类、创建对象...

    ActionScript3.0cookbook中文版

    标签“ac3”指的是ActionScript3.0,“actionscript3 cookbook”强调了这本书的实践性,而“actionscript”则是对整个ActionScript语言的泛指。 在压缩包内包含的文件《51CTO下载-ActionScript.3.0.Cookbook.中文...

    ActionScript3.0 Cookbook 中文完整版 pdf

    《ActionScript 3.0 Cookbook 中文完整版》通过具体实例,为读者提供了解决问题的“食谱”,涵盖了许多实际开发中常见的问题和挑战。每个章节都针对特定问题,提供可直接应用的代码片段,有助于快速理解和解决遇到的...

    ActionScript 3.0 cookbook中文简体完整版电子书

    《ActionScript 3.0 Cookbook》是一本专为ActionScript 3.0开发者设计的实用指南,它提供了大量具体的代码示例,帮助读者解决在开发过程中遇到的各种问题。这本书中文简体的完整版,旨在让中国地区的开发者能够更...

    ActionScript+3.0+Cookbook+中文完整版

    ActionScript+3.0+Cookbook+中文完整版source文件夹目录结构如下: org中主要是org.kingda.book.*包,所有的类文件都在其中。 com中应存放com.mimswright.*,是Mims Wright(www.mimswright.com)编写的生成抽象类的...

    ActionScript 3.0 Cookbook.rar 中文版

    在《ActionScript 3.0 Cookbook》中,你可以找到关于以下主题的知识点: 1. **基础语法**:包括变量声明、数据类型(如Number、String、Boolean)、操作符、流程控制语句(如if、for、while)、函数定义和调用等。 ...

    ActionScript 3 cookbook 锦囊妙计

    ### ActionScript 3 Cookbook 锦囊妙计 #### 一、概述 《ActionScript 3 Cookbook 锦囊妙计》是一本专为ActionScript开发者设计的实用指南。它旨在通过一系列精心挑选的示例和解决方案来帮助读者解决实际开发过程...

    ActionScript3_Cookbook_cn pdf

    Flex ActionScript3_Cookbook_cn

    ActionScript 3 Cookbook.PDF

    ActionScript 3 Cookbook.PDF

    ActionScript 3 Cookbook code

    《ActionScript 3 Cookbook》是一本专注于ActionScript 3编程技术的实用指南,源码包含在提供的多个文本文件中,如ch01.txt至ch20.txt。这些文件很可能是书中的各个章节代码示例,方便读者直接查看和运行。...

    ActionScript 3.0 Cookbook 中文完整版.pdf+源码

    在“ActionScript 3.0 Cookbook 中文完整版.pdf”中,你可以找到各种编程技巧和解决方案,每个章节都围绕一个特定的问题或任务展开,如创建动态图形、处理事件、使用XML或JSON进行数据交换、实现高级动画效果等。...

    ActionScript 3.0 Cookbook 中文版.pdf

    ActionScript 3.0 Cookbook 中文版.pdf 博文链接:https://lvxuehu.iteye.com/blog/183335

    ActionScript 3 Cookbook 中文版

    ActionScript 3 Cookbook 中文版,开发ActionScript必备资料

Global site tag (gtag.js) - Google Analytics