热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ActionScript3中的强制类型转换(as强转失败原因)

本文来自CSDN博客,转载请标明出处:http:blog.csdn.netlixinye0123archive200709021769157.aspx每一种

 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lixinye0123/archive/2007/09/02/1769157.aspx

 

     每一种编程语言都提供强制类型转换,允许你将某一种数据类型转换成另一种数据类型,AS3自然也不例外。但是虽然我编写了不少Flex程序,对 AS3中的强制类型转换还是不太清楚,以前AS中是这样进行强制类型转换的:假设有一个类叫做Class1,我们声明了一个它的对象 c1,如果想要将它转换成Class2类型,只要这样写:

Class2(c1);

   在AS3中你依然可以这样写,但是AS3 中提供了一个新的操作符: as ,并且推荐使用as 进行强制转换,上述的例子用 as 操作符实现就是这样:

c1 as Class2;

  使用 as 操作符有几个好处:

1.它的效果和第一种方法是一样的。
2.如果类型不兼容无法转换,就会返回null,而不是出错。这样你就可以自定义错误的时候该做什么。
3.没有运行时错误(Run Time Error)提示。

 

 

 

     不过有些时候我在使用 as 的时候并不能达到强制转换的目的,而使用第一种方法则可以。为什么 as 操作符有时候会不好用呢?这个问题困扰了我很久,知道昨天在MXNA上发现了一篇日志,才恍然大悟:原来在AS3.0类库中最高层类(Top Level classes)之间进行强制转换时, as 操作符是不起作用的。

比如,假如你想要将一个String 类型的字符串 str 转换成 Number 类型的数字 num 时,可能想要这样写:

num = str as Number;  //这样写是没有用的,你只能通过第一种方法来达到强制转换的目的:

num = Number(str);

 

例如arrary转换成vector也只能用方法一:

vector =Vector. (xmlStr.split("|",12));

 

 

 

 

 

 

 

 

Top-level classes

The top level contains the ActionScript classes and global functions, many of which provide core functionality for your applications. Core classes, borrowed directly from ECMAScript, include Array, Boolean, Date, Error, Function, Math, Number, Object, String, and System.

 

NOTE

 

The CustomActions and XMLUI classes are available only in the Flash authoring environment.

 

 

Class

Description

Accessibility

The Accessibility class manages communication between SWF files and screen reader applications. You use the methods of this class with the global _accProps property to control accessible properties for movie clips, buttons, and text fields at runtime. See Accessibility.

Array

The Array class represents arrays in ActionScript and all array objects are instances of this class. The Array class contains methods and properties for working with array objects. See Array.

AsBroadcaster

Provides event notification and listener management capabilities that can be added to other objects. See AsBroadcaster.

Boolean

The Boolean class is a wrapper for Boolean (true or false) values. See Boolean.>.

Button

The Button class provides methods, properties, and event handlers for working with buttons. See Button. Note that the built-in Button class is different from the Button component class, associated with the version 2 component, Button.

Camera

The Camera class provides access to the user's camera, if one is installed. When used with Flash Media Server, your SWF file can capture, broadcast, and record images and video from a user's camera. See Camera.

Color

The Color class lets you set the RGB color value and color transform of movie clip instances and retrieve those values after you set them. The Color class is deprecated in Flash Player 8 in favor of the ColorTransform class. For information on color transforms, see ColorTransform (flash.geom.ColorTransform).

ContextMenu

The ContextMenu class lets you control the contents of the Flash Player context menu at runtime. You can associate separate ContextMenu objects with MovieClip, Button, or TextField objects by using the menu property available to those classes. You can also add custom menu items to a ContextMenu object by using the ContextMenuItem class. See ContextMenu.

ContextMenuItem

The ContextMenuItem class lets you create new menu items that appear in the Flash Player context menu. You add new menu items that you create with this class to the Flash Player context menu by using the ContextMenu class. See ContextMenuItem.

CustomActions

The CustomActions class lets you manage any custom actions that are registered with the authoring tool. See CustomActions.

Date

The Date class shows how dates and times are represented in ActionScript, and it supports operations for manipulating dates and times. The Date class also provides the means for obtaining the current date and time from the operating system. See Date.

Error

The Error class contains information about runtime errors that occur in your scripts. You typically use the throw statement to generate an error condition, which you can handle using a try..catch..finally statement. See Error.

Function

The Function class is the class representation of all ActionScript functions, including those native to ActionScript and those that you define. See Function.

Key

The Key class provides methods and properties for getting information about the keyboard and key presses. See Key.

LoadVars

The LoadVars class lets you transfer variables between a SWF file and a server in name-value pairs. See LoadVars.

LocalConnection

The LocalConnection class lets you develop SWF files that send instructions to each other without using the fscommand() method or Javascript. See LocalConnection.

Math

The Math class provides convenient access to common mathematical constants and provides several common mathematical functions. All the properties and methods of the Math class are static and must be called with the syntax Math.method(parameter) or Math.constant. See Math.

Microphone

The Microphone class provides access to the user's microphone, if one is installed. When used with Flash Media Server, your SWF file can broadcast and record audio from a user's microphone. See Microphone.

Mouse

The Mouse class provides control over the mouse in a SWF file; for example, this class lets you hide or show the mouse pointer. See Mouse.

MovieClip

Every movie clip in a SWF file is an instance of the MovieClip class. You use the methods and properties of this class to control movie clip objects. See MovieClip.

MovieClipLoader

This class lets you implement listener callbacks that provide status information while SWF, JPEG, GIF, and PNG files load into movie clip instances. See MovieClipLoader.

NetConnection

The NetConnection class establishes a local streaming connection for playing a Flash Video (FLV) file from an HTTP address or from the local file system. See NetConnection.

NetStream

The NetStream class controls playback of FLV files from a local file system or HTTP address. See NetStream.

Number

The Number class is a wrapper for the primitive number data type. See Number.

Object

The Object class is at the root of the ActionScript class hierarchy; all other classes inherit its methods and properties. See Object.

PrintJob

The PrintJob class lets you print content from a SWF file, including content that is rendered dynamically, and multipage documents. See PrintJob.

Selection

The Selection class lets you set and control the text field in which the insertion point is located (the text field that has focus). See Selection.

SharedObject

The SharedObject class offers persistent local data storage on the client computer, similar to COOKIEs. This class offers real-time data sharing between objects on the client's computer. See SharedObject.

Sound

The Sound class provides control over sounds in a SWF file. See Sound.

Stage

The Stage class provides information about a SWF file's dimensions, alignment, and scale mode. It also reports Stage resize events. See Stage.

String

The String class is a wrapper for the string primitive data type, which lets you use the methods and properties of the String object to manipulate primitive string value types. See String.

System

The System class provides information about Flash Player and the system on which Flash Player is running (for example, screen resolution and current system language). It also lets you show or hide the Flash Player Settings panel and modify SWF file security settings. See System.

TextField

The TextField class provides control over dynamic and input text fields, such as retrieving formatting information, invoking event handlers, and changing properties such as alpha or background color. See TextField.

TextFormat

The TextFormat class lets you apply formatting styles to characters or paragraphs in a TextField object. See TextFormat.

TextSnapshot

The TextSnapshot object lets you access and lay out static text inside a movie clip. See TextSnapshot.

Video

The Video class lets you show video objects in a SWF file. You can use this class with Flash Media Server to display live streaming video in a SWF file, or within Flash to display a Flash Video (FLV) file. See Video.

XML

This class contains methods and properties for working with XML objects. See XML.

XMLNode

The XMLNode class represents a single node in an XML document tree. It is the XML class's superclass. See XMLNode.

XMLSocket

The XMLSocket class lets you create a persistent socket connection between a server computer and client running Flash Player. Client sockets enable low-latency data transfer, such as that which is required for real-time chat applications. See XMLSocket.

XMLUI

The XMLUI object enables communication with SWF files that are used as a custom user interface for the Flash authoring tool's extensibility features (such as Behaviors, Commands, Effects, and Tools). See XMLUI.

 

 

 

 

 


推荐阅读
author-avatar
四川盛地地产顾问有限公司_255
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有