1、哈希表(ActionScript中叫关联数组)
在ActionScript中是Object的一个实例,
一、创建:
var memebers:Object = {scribe: "Franklin",
chairperson: "Gina",
treasurer: "Sindhu"};
或
var members:Object = new Object( );
members.scribe = "Franklin";
members.chairperson = "Gina";
members.treasurer = "Sindhu";
二、访问:
trace(members.scribe);
或
trace(members["scribe"]);
三、读取,使用for...in
// 显示:
// treasurer: Sindhu
// chairperson: Gina
// scribe: Franklin
for (var sRole:String in members) {
trace(sRole + ": " + members[sRole]);
}
========================================================================
2、几种常用可视化类应继承的父类说明
如果创建一个图形类,这就要继承Shape类。
如果要创建自定义按钮,可能要继承SimpleButton类。
如果要创建一个容纳其他对象的容器,Sprite 就是个很好的继承类,它不需要使用时间线。
如果需要时间线,需要继承MovieClip
========================================================================
3、创建鼠标移动至按钮上时的浮动按钮
思路:
一、创建类继承自SimpleButton,定义按钮的四种状态upState、overState、downState、hitTestState
四种状态意义:
upState:鼠标移开时
overState:鼠标移到按钮上时
downState:鼠标按下时
hitTestState:鼠标移动按钮时的可反应区域
二、创建TextField,用来显示按钮上的文字。
三、在创建TextField这个方法中,如果为按下状态时,此TextField的X和Y座标分别 +1。
代码:
public class RectangleButton extends SimpleButton { // 显示在按钮上的文本 private var _text:String; // 保存矩形的宽度和高度 private var _width:Number; private var _height:Number; public function RectangleButton( text:String, width:Number, height:Number ) { // Save the values to use them to create the button states _text = text; _width = width; _height = height; // 创建按钮状态 upState = createUpState( ); overState = createOverState( ); downState = createDownState( ); hitTestState = upState; } // 创建状态对象 private function createUpState( ):Sprite { var sprite:Sprite = new Sprite( ); var background:Shape = createdColoredRectangle( 0x33FF66 ); var textField:TextField = createTextField( false ); sprite.addChild( background ); sprite.addChild( textField ); return sprite; } private function createOverState( ):Sprite { var sprite:Sprite = new Sprite( ); var background:Shape = createdColoredRectangle( 0x70FF94 ); var textField:TextField = createTextField( false ); sprite.addChild( background ); sprite.addChild( textField ); return sprite; } private function createDownState( ):Sprite { var sprite:Sprite = new Sprite( ); var background:Shape = createdColoredRectangle( 0xCCCCCC ); var textField:TextField = createTextField( true ); sprite.addChild( background ); sprite.addChild( textField ); return sprite; } private function createdColoredRectangle( color:uint ):Shape { var rect:Shape = new Shape( ); rect.graphics.lineStyle( 1, 0x000000 ); rect.graphics.beginFill( color ); rect.graphics.drawRoundRect( 0, 0, _width, _height, 15 ); rect.graphics.endFill( ); rect.filters = [ new DropShadowFilter( 2 ) ]; return rect; } // 创建按钮上的文字 private function createTextField( downState:Boolean ):TextField { var textField:TextField = new TextField( ); textField.text = _text; textField.width = _width; var format:TextFormat = new TextFormat( ); format.align = TextFormatAlign.CENTER; textField.setTextFormat( format ); //垂直居中 textField.y = ( _height - textField.textHeight ) / 2; textField.y -= 2; // Subtract 2 pixels to adjust for offset if ( downState ) { textField.x += 1; textField.y += 1; } } } //调用按钮类 package { import flash.display.*; public class SimpleButtonDemo extends Sprite { public function SimpleButtonDemo( ) { // 创建三个不同文字不同大小和位置的矩形按钮 var button1:RectangleButton = new RectangleButton( "Button 1", 60, 100 ); button1.x = 20; button1.y = 20; var button2:RectangleButton = new RectangleButton( "Button 2", 80, 30 ); button2.x = 90; button2.y = 20; var button3:RectangleButton = new RectangleButton( "Button 3", 100, 40 ); button3.x = 100; button3.y = 60; addChild( button1 ); addChild( button2 ); addChild( button3 ); } } }
========================================================================
4、Loader实例的contentLoaderInfo的事件说明:
open
当资源开始下载时触发
progress
资源在下载中时触发
complete
当资源下载完成时触发
init
当载入外部的.swf初始化时触发
httpStatus
当载入外部资源的HTTP请求产生状态代码错误时触发
ioError
当一个错误导致下载被终止时触发,比如找不到相应资源
securityError
当试图读取安全沙漏以外的数据时触发
unload
当unload( ) 方法被调用或移除载入的内容时或再次调用load( ) 方法时都会触发该事件
========================================================================
5、利loader的事件显示加载对象时的百分比
private function handleOpen( event:Event ):void { _loaderStatus = new TextField( ); addChild( loaderStatus ); _loaderStatus.text = "Loading: 0%"; } private function handleProgress( event:ProgressEvent ):void { var percent:Number = event.bytesLoaded / event.bytesTotal * 100; _loaderStatus.text = "Loading: " + percent + "%"; } private function handleComplete( event:Event ):void { removeChild(_loaderStatus); _loaderStatus = null; }
========================================================================
6、装载SWF,并调用此SWF中的方法
使用Loader,监听Init事件,并通过loader.content访问装载SWF中的方法
如:
private function handleInit( event:Event ):void {
var movie:* = _loader.content;
//可以调用装载swf的方法了
trace( movie.getColor( ) );
movie.setColor( 0xFF0000 );
}
========================================================================