6.7 加载和交互外部影片
问题
你想加载和交互外部的 .swf 影片到你的影片中
解决办法
用新的 Loader 类来加载 .swf 文件,通过 这个类的实例的 content属性来访问这个.swf
讨论
看看效果先,这是我根据本节的内容所制作的,添加了额外的功能
6.6节中示范了怎么加载外部图像,加载 .swf 文件同样的是使用 Loader 类实例的 load()方法,传递给它一个URL,这节中包括建立两个分开的 .swf 文件,ExternalMovie.swf 和 LoaderExample.swf ,第一个影片:ExternalMovie.swf 将会在第二个影片:LoaderExample.swf 运行时被加载进来,ExternalMovie.swf 的代码如下:
package { import flash.display.Sprite; import flash.display.Shape; public class ExternalMovie extends Sprite { private var _color:uint = 0x000000; private var _circle:Shape; public function ExternalMovie( ) { updateDisplay( ); } private function updateDisplay( ):void { // If the circle hasn't been created yet, create it如果圆还没有建立,建立它 // and make it visible by adding it to the display list添加它到显示列表 if ( _circle == null ) { _circle = new Shape( ); addChild( _circle ); } // Clear any previously drawn content and draw // a new circle with the fill color _circle.graphics.clear( ); _circle.graphics.beginFill( _color ); _circle.graphics.drawCircle( 100, 100, 40 ); } // Changes the color of the circle改变圆的颜色 public function setColor( color:uint ):void { _color = color; updateDisplay( ); } // Gets the current circle color value取得圆的颜色 public function getColor( ):uint { return _color; } } }
ExternalMovie.swf 在招待的时候,建立了一个平平常常的圆,没什么特别的东西,主要注意两个 public 的方法:访问和修改圆的颜色,getColor()和 setColor()。无论何时调用 setColor()方法,根据新的颜色值,圆被重新绘制。
声明这些方法为 public 是因为要让,在加载这个影片的影片,可以调用这方法,相反,private updateDisplay()方法不在加载影片的时候被使用,
现在ExternalMovie.swf 已建立,现在需要建立一个新的 LoaderExample.swf 来加载外部影片,它的代码如下:
package { import flash.display.*; import flash.net.URLRequest; import flash.events.Event; public class LoaderExample extends Sprite { private var _loader:Loader; public function LoaderExample( ) { // Create the Loader and add it to the display list建立一个 Loder ,添加它到显示列表 _loader = new Loader( ); addChild( _loader ); // Add the event handler to interact with the loaded movie添加事件操作加载的影片 _loader.contentLoaderInfo.addEventListener( Event.INIT, handleInit ); // Load the external movie加载外部影片 _loader.load( new URLRequest( "ExternalMovie.swf" ) ); } // Event handler called when the externally loaded movie is // ready to be interacted with在初始化时调用操作方法 private function handleInit( event:Event ):void { // Typed as * here because the type is not known at compile-time.类型设为*,是因为在编辑的时候不知道是什么类型 var movie:* = _loader.content; // Calls a method in the external movie to get data out调用外部影片的方法 // Displays: 0 trace( movie.getColor( ) ); // Calls a method in the external movie to set data. // Sets the color in the external movie, which draws // a circle with the new color, in this case red movie.setColor( 0xFF0000 ); } } }
在上面的代码有两个主要方面的问题:
-
Listening for the init event 侦听 init 事件
-
Accessing the loaded movie via the content property 通过 content 属性来访问加载的影片
当要加载的影片已初始化时(它的方法和属性可以被交互时),init 事件被触发。在 init 事件被触发,影片可以被控制了,如果尝试在一个要加载的影片初始化前交互它,将会产生一个运行错误。
为了控制加载的影片,你首先要取得它,可以通过 Loader 类的 content 属性,在上面的代码中, loader 变量设为 Loader,用来加载外部 .swf文件,所以你可以通过 loader.content 来访问影片,如果 loader 变量不可使用, 可以用 event.target.content 路径来替换 Loader 的 content,这是因为:event.target 等于产生这个事件的实例,也就是和 loader 实例一样。
content 属性是只读的,返回一个 DisplayObject 类型的对像,在 LoaderExample.swf 的代码中,代替影片类型的是一个*,这是必须的,因为:尝试调用 getColor()或者setColor()方法,如果设置 movie 的类型为 DisplayObject 的话,将会产生编辑错误。
注意:使用这个技术,它只可以交互9或者以上的.swf ,当加载的是 8的版本或者更低的.swf文件,这个技术无法工作,因为 运行as3.0 代码不信赖于 as1.0和as2.0 。与这些 .swf 文件通讯,可以使用 LocalConnection 作为一个工作区来发送和接收消息,可以参考第19章。