Học Actionscript 3.0 - p 12

Managing Object Names, Positions, and Data Types However, the ActionScript compiler doesn’t know if gotoAndStop() is a legal method of mc’s parent because it doesn’t know the parent’s data type. For example, the parent might be a sprite and a sprite doesn’t have a timeline. As such, you can’t very well go to frame 20 of a sprite. If the data type of the parent is unknown to the ActionScript compiler, you will get an error similar to: Call to a possibly undefined method gotoAndStop through a reference with static type . You can avoid this error by casting the object. Previously discussed in Chapter 2, casting is particularly important when manipulating the display list. | Managing Object Names Positions and Data Types However the ActionScript compiler doesn t know if gotoAndStop is a legal method of mcs parent because it doesn t know the parent s data type. For example the parent might be a sprite and a sprite doesn t have a timeline. As such you can t very well go to frame 20 of a sprite. If the data type of the parent is unknown to the ActionScript compiler you will get an error similar to Call to a possibly undefined method gotoAndStop through a reference with static type DisplayObjectContainer. You can avoid this error by casting the object. Previously discussed in Chapter 2 casting is particularly important when manipulating the display list and warrants another mention. Casting means you are explicitly telling the ActionScript compiler the data type of the object changing the compiler s understanding of the data from one type to another. Casting does not actually change data. In our example to make sure the compiler doesn t object to the gotoAndStop method you must cast the parent from DisplayObjectContainer to MovieClip. You can do this by surrounding the object of unknown type with the desired class name. The following syntax tells the compiler that mc s parent is of data type MovieClip MovieClip .gotoAndStop 20 NOTE Another way to cast an object is by using the as operator. Continuing the example on this page this syntax will also cast mc s parent as a movie clip var mc2 MovieClip as MovieClip 20 Although this is more verbose it has advantages. For example the ClassName syntax may be confusing because it looks like you are calling a function or instantiating a class. Also some conversion or creation functions takes precedence over casting and prevents casting from working. For example Array will not cast to an array because that syntax is equivalent to new Array which creates an array. This means it s possible to cast an object to Array only using the as operator. One reason

Bấm vào đây để xem trước nội dung
TÀI LIỆU MỚI ĐĂNG
476    18    1    30-11-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.