Flex动态获取flash资源库文件
2010-06-09 09:53
399 查看
有几种方式 在这里介绍两种
1. 给一个flash资源文件写个资源外壳
例如 在一个包含a 和b两个元件的flash文件中写个as类
public class Wipper
{
[Embed(source="a")]
public var A:Class;
[Embed(source="a")]
public var B:Class;
}
将此类发布到包含a,b元件的swf中
flex可以直接访问A 和 B了
2 .
1.命名前必须将所要用到的整合组件放置flash主场景中,然后在时间轴第一帧上脚本代码指定引用元件,如:this. FR;指引操作如下:
⑴. 以原来的脸部组件为例 打开flash 确认打开的是否为当前文件
(2).打开时间轴(TIMRLINE)
(3).右键点击最上面一层的第一帧选择Actions
(4).出现以下界面
(5). 选择代码指引的按钮(带十字的圆圈),出现下面弹出框
(6).选择你要命名的元件,单击(如:女性前右基础眼睛)
(7).点击Rename
(8),填写下面命名规则中相对应的名字(如:FR)点击OK
(9).选中刚命名过的元件,双击或者点击OK
(10).在此行后面加分号,回车
(11).重复(3)到(9)继续给其他元件命名。
flex中可用此自定义组件访问
package{
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import fw.ui.events.UIEvent;
import mx.events.ResizeEvent;
public class FWTransparentImage extends FWImage
{
private static const EFFECIENCY_S***E_FACTOR:uint = 20;
private var ht:Sprite = new Sprite();
public function FWTransparentImage()
{
super();
addChild(ht);
hitArea = ht;
ht.visible = false;
ht.mouseEnabled = false;
//addEventListener(Event.COMPLETE, completeHandler, false, 99, true);
addEventListener(ResizeEvent.RESIZE, completeHandler, false, 99, true);
addEventListener(UIEvent.IMAGE_RELOAD, completeHandler, false, 99, true);
}
[Inspectable(defaultValue="true")]
public var staticContent:Boolean = true;
private var _contentChanged:Boolean = false;
//
// override public function set movieClipName(value:String):void
// {
// super.movieClipName = value;
// _contentChanged = true;
// }
//
// override public function set source(value:Object):void
// {
// super.source = value;
// _contentChanged = true;
// }
private function completeHandler(e:Event):void
{
//trace("******", e.type, "*******");
_contentChanged = true;
if (!staticContent
&& content
&& content.loaderInfo.contentType == "application/x-shockwave-flash"
&& content is MovieClip
)
{
MovieClip(content).addEventListener(Event.ENTER_FRAME, refreshFrameHandler);
}
//updateHitArea();
}
public var monitorRate:uint = 24;
private var _framed:uint = 0;
private function refreshFrameHandler(e:Event):void
{
if (_framed++ < monitorRate) return;
_framed = 0;
_contentChanged = true;
//updateHitArea();
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
//return;
// private function updateHitArea():void
// {
if (mouseEnabled && _contentChanged && content)
{
_contentChanged = false;
var factor:uint =
Math.min(
Math.max(Math.min(content.width, content.height)/5, 2),
EFFECIENCY_S***E_FACTOR
);
var container:DisplayObject = content.parent as DisplayObject;
var bmp:BitmapData = new BitmapData(container.width, container.height, true, 0x00000000);
var matrix:Matrix = new Matrix();
matrix.scale(container.scaleX, container.scaleY);
bmp.draw(container, matrix);
// redraw hit area
with(ht.graphics)
{
clear();
beginFill(0x000000);
for (var x:int=0; x<container.width; x+=factor)
{
for (var y:int=0; y<container.height; y+=factor)
{
if (bmp.getPixel32(x, y))
{
drawRect(x, y, factor, factor);
}
}
}
endFill();
}
ht.x = -factor/2, ht.y = -factor/2;
addChild(ht);
}
}
}
}
此类继承了 自定义组件
<?xml version="1.0" encoding="utf-8"?>
<mx:SWFLoader xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:base="fw.base.*"
mouseChildren="false" mouseEnabled="false"
>
<mx:Script>
<![CDATA[
import fw.ui.events.UIEvent;
import fw.events.FWEvent;
import fw.base.Utils;
import mx.events.ResizeEvent;
import fw.base.Logger;
import fw.comm.ResourceLoader;
import fw.comm.events.ResourceLoaderEvent;
[Embed(source="/assets/imageAutoload.swf#loadPic")]
private static const DEFAULT_LOADING:Class;
[Embed(source="/assets/imageAutoload.swf#errorImage")]
private static const DEFAULT_ERROR:Class;
[Inspectable(defaultValue="true")]
public var showErrorImage:Boolean = true;
[Inspectable(defaultValue="false")]
public var loadChildMovieClip:Boolean = false;
private var _movieClipName:String = "unnamed";
[Bindable(event="mcNameChanged")]
public function set movieClipName(value:String):void
{
_movieClipName = value;
loadMovieClip();
dispatchEvent(new Event("mcNameChanged"));
}
public function get movieClipName():String
{
return _movieClipName;
}
public function hasMovieClipName(name:String):MovieClip
{
if (
_movieClipHolder
&& _movieClipHolder.content
&& _movieClipHolder.content.hasOwnProperty(name)
)
return _movieClipHolder.content[name] as MovieClip;
else
return null;
}
public function get isBroken():Boolean
{
return (super.source == DEFAULT_ERROR);
}
[Inspectable(defaultValue="false")]
public var transformable:Boolean = false;
private var _color:Number;
public function set color(value:Number):void
{
if (value == _color) return;
if (isNaN(value)) return;
_color = value;
transformColor();
}
public function get color():Number
{
return _color;
}
private var _movieClipHolder:Loader;
private var _loader:ResourceLoader;
private var _loading:Boolean = false;
private var _source:Object;
public override function set source(value:Object):void
{
if (_source == value) return;
_source = value;
if (_source is String)
{
_loader = new ResourceLoader();
_loader.addEventListener(ResourceLoaderEvent.SUCCESS, contentLoadedHandler);
_loader.addEventListener(ResourceLoaderEvent.ERROR, errorHandler);
super.source = DEFAULT_LOADING;
currentState = "loading";
_loading = true;
_loader.load(value as String);
}
else
{
super.source = _source;
}
}
public override function get source():Object
{
return _source;
}
private function contentLoadedHandler(e:ResourceLoaderEvent):void
{
_loader.removeEventListener(ResourceLoaderEvent.SUCCESS, contentLoadedHandler);
_loader.removeEventListener(ResourceLoaderEvent.ERROR, errorHandler);
_loading = false;
if (loadChildMovieClip)
{
if (_movieClipHolder)
_movieClipHolder.contentLoaderInfo.removeEventListener(Event.COMPLETE, movieClipLoadedHandler);
_movieClipHolder = new Loader;
var context:LoaderContext =
new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain));
_movieClipHolder.contentLoaderInfo.addEventListener(Event.COMPLETE, movieClipLoadedHandler);
_movieClipHolder.loadBytes(e.bytes, context);
}
else
{
currentState = "";
super.source = e.bytes;
}
}
private function movieClipLoadedHandler(e:Event):void
{
currentState = "";
loadMovieClip();
}
private function errorHandler(e:ResourceLoaderEvent):void
{
Logger.warning("AutoImage loading error for: ", _loader.url);
_loader.removeEventListener(ResourceLoaderEvent.SUCCESS, contentLoadedHandler);
_loader.removeEventListener(ResourceLoaderEvent.ERROR, errorHandler);
_loading = false;
if (showErrorImage) super.source = DEFAULT_ERROR;
else super.source = null;
currentState = "loading";
}
private function loadMovieClip():void
{
if (_movieClipHolder && _movieClipHolder.content)
{
super.source = _movieClipHolder.content[_movieClipName];
dispatchEvent(new UIEvent(UIEvent.IMAGE_RELOAD));
}
}
private function transformColor():void
{
if (!isNaN(_color))
{
var trans:ColorTransform = new ColorTransform;
trans.color = _color;
this.transform.colorTransform = trans;
}
}
]]>
</mx:Script>
<mx:states>
<mx:State name="loading">
<mx:SetProperty name="maintainAspectRatio" value="true"/>
<mx:SetProperty name="scaleContent" value="false"/>
<mx:SetStyle name="horizontalAlign" value="center"/>
<mx:SetStyle name="verticalAlign" value="middle"/>
</mx:State>
</mx:states>
</mx:SWFLoader>
访问可以这么写
<controls:FWTransparentImage id="hat"
mouseEnabled="false" loadChildMovieClip="true"
cacheAsBitmap="true" cachePolicy="on"
x="0" y="0" width="100%" height="100%"
source="{character.hat.movieClipURL}"
/>
1. 给一个flash资源文件写个资源外壳
例如 在一个包含a 和b两个元件的flash文件中写个as类
public class Wipper
{
[Embed(source="a")]
public var A:Class;
[Embed(source="a")]
public var B:Class;
}
将此类发布到包含a,b元件的swf中
flex可以直接访问A 和 B了
2 .
1.命名前必须将所要用到的整合组件放置flash主场景中,然后在时间轴第一帧上脚本代码指定引用元件,如:this. FR;指引操作如下:
⑴. 以原来的脸部组件为例 打开flash 确认打开的是否为当前文件
(2).打开时间轴(TIMRLINE)
(3).右键点击最上面一层的第一帧选择Actions
(4).出现以下界面
(5). 选择代码指引的按钮(带十字的圆圈),出现下面弹出框
(6).选择你要命名的元件,单击(如:女性前右基础眼睛)
(7).点击Rename
(8),填写下面命名规则中相对应的名字(如:FR)点击OK
(9).选中刚命名过的元件,双击或者点击OK
(10).在此行后面加分号,回车
(11).重复(3)到(9)继续给其他元件命名。
flex中可用此自定义组件访问
package{
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import fw.ui.events.UIEvent;
import mx.events.ResizeEvent;
public class FWTransparentImage extends FWImage
{
private static const EFFECIENCY_S***E_FACTOR:uint = 20;
private var ht:Sprite = new Sprite();
public function FWTransparentImage()
{
super();
addChild(ht);
hitArea = ht;
ht.visible = false;
ht.mouseEnabled = false;
//addEventListener(Event.COMPLETE, completeHandler, false, 99, true);
addEventListener(ResizeEvent.RESIZE, completeHandler, false, 99, true);
addEventListener(UIEvent.IMAGE_RELOAD, completeHandler, false, 99, true);
}
[Inspectable(defaultValue="true")]
public var staticContent:Boolean = true;
private var _contentChanged:Boolean = false;
//
// override public function set movieClipName(value:String):void
// {
// super.movieClipName = value;
// _contentChanged = true;
// }
//
// override public function set source(value:Object):void
// {
// super.source = value;
// _contentChanged = true;
// }
private function completeHandler(e:Event):void
{
//trace("******", e.type, "*******");
_contentChanged = true;
if (!staticContent
&& content
&& content.loaderInfo.contentType == "application/x-shockwave-flash"
&& content is MovieClip
)
{
MovieClip(content).addEventListener(Event.ENTER_FRAME, refreshFrameHandler);
}
//updateHitArea();
}
public var monitorRate:uint = 24;
private var _framed:uint = 0;
private function refreshFrameHandler(e:Event):void
{
if (_framed++ < monitorRate) return;
_framed = 0;
_contentChanged = true;
//updateHitArea();
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
//return;
// private function updateHitArea():void
// {
if (mouseEnabled && _contentChanged && content)
{
_contentChanged = false;
var factor:uint =
Math.min(
Math.max(Math.min(content.width, content.height)/5, 2),
EFFECIENCY_S***E_FACTOR
);
var container:DisplayObject = content.parent as DisplayObject;
var bmp:BitmapData = new BitmapData(container.width, container.height, true, 0x00000000);
var matrix:Matrix = new Matrix();
matrix.scale(container.scaleX, container.scaleY);
bmp.draw(container, matrix);
// redraw hit area
with(ht.graphics)
{
clear();
beginFill(0x000000);
for (var x:int=0; x<container.width; x+=factor)
{
for (var y:int=0; y<container.height; y+=factor)
{
if (bmp.getPixel32(x, y))
{
drawRect(x, y, factor, factor);
}
}
}
endFill();
}
ht.x = -factor/2, ht.y = -factor/2;
addChild(ht);
}
}
}
}
此类继承了 自定义组件
<?xml version="1.0" encoding="utf-8"?>
<mx:SWFLoader xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:base="fw.base.*"
mouseChildren="false" mouseEnabled="false"
>
<mx:Script>
<![CDATA[
import fw.ui.events.UIEvent;
import fw.events.FWEvent;
import fw.base.Utils;
import mx.events.ResizeEvent;
import fw.base.Logger;
import fw.comm.ResourceLoader;
import fw.comm.events.ResourceLoaderEvent;
[Embed(source="/assets/imageAutoload.swf#loadPic")]
private static const DEFAULT_LOADING:Class;
[Embed(source="/assets/imageAutoload.swf#errorImage")]
private static const DEFAULT_ERROR:Class;
[Inspectable(defaultValue="true")]
public var showErrorImage:Boolean = true;
[Inspectable(defaultValue="false")]
public var loadChildMovieClip:Boolean = false;
private var _movieClipName:String = "unnamed";
[Bindable(event="mcNameChanged")]
public function set movieClipName(value:String):void
{
_movieClipName = value;
loadMovieClip();
dispatchEvent(new Event("mcNameChanged"));
}
public function get movieClipName():String
{
return _movieClipName;
}
public function hasMovieClipName(name:String):MovieClip
{
if (
_movieClipHolder
&& _movieClipHolder.content
&& _movieClipHolder.content.hasOwnProperty(name)
)
return _movieClipHolder.content[name] as MovieClip;
else
return null;
}
public function get isBroken():Boolean
{
return (super.source == DEFAULT_ERROR);
}
[Inspectable(defaultValue="false")]
public var transformable:Boolean = false;
private var _color:Number;
public function set color(value:Number):void
{
if (value == _color) return;
if (isNaN(value)) return;
_color = value;
transformColor();
}
public function get color():Number
{
return _color;
}
private var _movieClipHolder:Loader;
private var _loader:ResourceLoader;
private var _loading:Boolean = false;
private var _source:Object;
public override function set source(value:Object):void
{
if (_source == value) return;
_source = value;
if (_source is String)
{
_loader = new ResourceLoader();
_loader.addEventListener(ResourceLoaderEvent.SUCCESS, contentLoadedHandler);
_loader.addEventListener(ResourceLoaderEvent.ERROR, errorHandler);
super.source = DEFAULT_LOADING;
currentState = "loading";
_loading = true;
_loader.load(value as String);
}
else
{
super.source = _source;
}
}
public override function get source():Object
{
return _source;
}
private function contentLoadedHandler(e:ResourceLoaderEvent):void
{
_loader.removeEventListener(ResourceLoaderEvent.SUCCESS, contentLoadedHandler);
_loader.removeEventListener(ResourceLoaderEvent.ERROR, errorHandler);
_loading = false;
if (loadChildMovieClip)
{
if (_movieClipHolder)
_movieClipHolder.contentLoaderInfo.removeEventListener(Event.COMPLETE, movieClipLoadedHandler);
_movieClipHolder = new Loader;
var context:LoaderContext =
new LoaderContext(false, new ApplicationDomain(ApplicationDomain.currentDomain));
_movieClipHolder.contentLoaderInfo.addEventListener(Event.COMPLETE, movieClipLoadedHandler);
_movieClipHolder.loadBytes(e.bytes, context);
}
else
{
currentState = "";
super.source = e.bytes;
}
}
private function movieClipLoadedHandler(e:Event):void
{
currentState = "";
loadMovieClip();
}
private function errorHandler(e:ResourceLoaderEvent):void
{
Logger.warning("AutoImage loading error for: ", _loader.url);
_loader.removeEventListener(ResourceLoaderEvent.SUCCESS, contentLoadedHandler);
_loader.removeEventListener(ResourceLoaderEvent.ERROR, errorHandler);
_loading = false;
if (showErrorImage) super.source = DEFAULT_ERROR;
else super.source = null;
currentState = "loading";
}
private function loadMovieClip():void
{
if (_movieClipHolder && _movieClipHolder.content)
{
super.source = _movieClipHolder.content[_movieClipName];
dispatchEvent(new UIEvent(UIEvent.IMAGE_RELOAD));
}
}
private function transformColor():void
{
if (!isNaN(_color))
{
var trans:ColorTransform = new ColorTransform;
trans.color = _color;
this.transform.colorTransform = trans;
}
}
]]>
</mx:Script>
<mx:states>
<mx:State name="loading">
<mx:SetProperty name="maintainAspectRatio" value="true"/>
<mx:SetProperty name="scaleContent" value="false"/>
<mx:SetStyle name="horizontalAlign" value="center"/>
<mx:SetStyle name="verticalAlign" value="middle"/>
</mx:State>
</mx:states>
</mx:SWFLoader>
访问可以这么写
<controls:FWTransparentImage id="hat"
mouseEnabled="false" loadChildMovieClip="true"
cacheAsBitmap="true" cachePolicy="on"
x="0" y="0" width="100%" height="100%"
source="{character.hat.movieClipURL}"
/>
相关文章推荐
- Flash加载一直在更新的资源的缓存问题,比如 在hls(HTTP Live Streaming)获取m3u8的文件
- Winform动态获取资源文件中的图片
- 通过字段名称动态获取资源的id(将Android工程做成jar包和资源文件)
- [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
- 动态获取资源ID-用getIdentifier()获取资源Id
- Android进阶:打jar包获取assets中的资源 解决selector XML文件不能解析的问题
- Web应用中的普通java程序获取资源文件
- servlet和普通类获取资源文件的方法
- 获取.exe/.dll/.icon文件的Icon图标资源
- Android动态加载外部jar包及jar包中图片等资源文件
- android获取资源文件R.drawable中的图片的相关方法
- 『IOS』IOS获取工程目录下的资源文件的路径
- CBitmap程序中动态加载 本地文件上的BMP图片资源
- [unity3d]从服务器端获取资源动态加载到场景
- android换肤功能中,如何动态获取控件中背景图片的资源id?
- flex4 动态获取对象属性,动态生成数据源,动态生成lineChart,清除数据。
- Android中获取资源 id 及资源 id 的动态获取
- android批量获取R文件下的资源
- Spring获取资源文件
- Java中获取配置/资源文件