/**
* Flash资源加载或者管理类,静态类,不可实例化
* 一般都是初始化或者设置从Flash里导出的资源
* @class annie
*/
var annie;
(function (annie) {
var URLLoader = annie.URLLoader;
var Event = annie.Event;
var ColorFilter = annie.ColorFilter;
var Shape = annie.Shape;
var BlurFilter = annie.BlurFilter;
var ShadowFilter = annie.ShadowFilter;
var ColorMatrixFilter = annie.ColorMatrixFilter;
//打包swf用
annie._isReleased = false;
annie.suffixName = ".swf";
//打包swf用
annie._shareSceneList = [];
//存储加载资源的总对象
annie.res = {};
// 加载器是否正在加载中
var _isLoading;
// 加载中的场景名列表
var _loadSceneNames;
//加载地址的域名地址或前缀
var _domain;
//当前加载到哪一个资源
var _loadIndex;
// 当前加载的总资源数
var _totalLoadRes;
//当前已经加载的资源数
var _loadedLoadRes;
//加载资源的完成回调
var _completeCallback;
//加载资源时的进度回调
var _progressCallback;
//加载配置文件的加载器
var _JSONQueue;
//加载资源文件的加载器
var _loaderQueue;
// 加载器是否初始化过
var _isInited;
// 当前加载的资源配置文件内容
var _currentConfig;
//获取当前加载的时间当作随机数用
var _time = new Date().getTime();
// 加载资源数和总资源数的比
var _loadPer;
//单个资源占总资源数的比
var _loadSinglePer;
/**
* <h4><font color="red">注意:小程序 小游戏里这个方法是同步方法</font></h4>
* 加载一个flash2x转换的文件内容,如果未加载完成继续调用此方法将会刷新加载器,中断未被加载完成的资源
* @method annie.loadScene
* @public
* @static
* @since 1.0.0
* @param {string} sceneName fla通过flash2x转换时设置的包名
* @param {Function} progressFun 加载进度回调,回调参数为当前的进度值1-100
* @param {Function} completeFun 加载完成回调,回调参数为当前加载的场景信息
* @param {string} domain 加载时要设置的url前缀,默认则不更改加载路径
*/
annie.loadScene = function (sceneName, progressFun, completeFun, domain) {
if (domain === void 0) { domain = ""; }
//加载资源配置文件
if (_isLoading) {
_JSONQueue.loadCancel();
_loaderQueue.loadCancel();
}
_loadSceneNames = [];
_domain = domain;
if (typeof (sceneName) == "string") {
if (!isLoadedScene(sceneName)) {
annie.res[sceneName] = {};
_loadSceneNames.push(sceneName);
}
else {
var info = {};
info.sceneName = sceneName;
info.sceneId = 1;
info.sceneTotal = 1;
completeFun(info);
}
}
else {
var len = sceneName.length;
var index = 0;
for (var i = 0; i < len; i++) {
if (!isLoadedScene(sceneName[i])) {
annie.res[sceneName[i]] = {};
_loadSceneNames.push(sceneName[i]);
}
else {
var info = {};
info.sceneName = sceneName[i];
info.sceneId = ++index;
info.sceneTotal = len;
completeFun(info);
}
}
}
if (_loadSceneNames.length == 0) {
return;
}
if (!_isInited) {
_JSONQueue = new URLLoader();
_JSONQueue.addEventListener(Event.COMPLETE, onCFGComplete);
_loaderQueue = new URLLoader();
_loaderQueue.addEventListener(Event.COMPLETE, _onRESComplete);
_loaderQueue.addEventListener(Event.PROGRESS, _onRESProgress);
_isInited = true;
}
_loadResCount = 0;
_loadPer = 0;
_loadIndex = 0;
_totalLoadRes = 0;
_loadedLoadRes = 0;
_isLoading = true;
_completeCallback = completeFun;
_progressCallback = progressFun;
_currentConfig = [];
if (!annie._isReleased) {
_loadConfig();
}
else {
//加载正式的单个文件
//看看是否需要加载共享资源
if (annie._shareSceneList.length > 0 && (!isLoadedScene("f2xShare"))) {
for (var i = 0; i < _loadSceneNames.length; i++) {
if (annie._shareSceneList.indexOf(_loadSceneNames[i]) >= 0) {
_loadSceneNames.unshift("f2xShare");
break;
}
}
}
_loadIndex = 0;
_totalLoadRes = _loadSceneNames.length;
_loadSinglePer = 1 / _totalLoadRes;
for (var i = 0; i < _totalLoadRes; i++) {
_currentConfig.push([{ src: "src/" + _loadSceneNames[i] + "/" + _loadSceneNames[i] + annie.suffixName }]);
}
_loadRes();
}
};
//加载配置文件,打包成released线上版时才会用到这个方法。
//打包released后,所有资源都被base64了,所以线上版不会调用这个方法。
function _loadConfig() {
_JSONQueue.load(_domain + "resource/" + _loadSceneNames[_loadIndex] + "/" + _loadSceneNames[_loadIndex] + ".res.json?t=" + _time);
}
//加载配置文件完成时回调,打包成released线上版时才会用到这个方法。
//打包released后,所有资源都被base64了,所以线上版不会调用这个方法。
function onCFGComplete(e) {
//配置文件加载完成
var resList = e.data.response;
_currentConfig.push(resList);
_totalLoadRes += resList.length;
_loadIndex++;
if (_loadSceneNames[_loadIndex]) {
_loadConfig();
}
else {
//所有配置文件加载完成,那就开始加载资源
_loadIndex = 0;
_loadSinglePer = 1 / _totalLoadRes;
_loadRes();
}
}
// 加载资源过程中调用的回调方法。
function _onRESProgress(e) {
if (_progressCallback) {
var ww = window;
var total = e.data.totalBytes;
if (ww.swfBytes && ww.swfBytes[_loadSceneNames[_loadIndex]]) {
total = ww.swfBytes[_loadSceneNames[_loadIndex]];
}
_progressCallback((_loadPer + e.data.loadedBytes / total * _loadSinglePer) * 100 >> 0);
}
}
//解析加载后的json资源数据
function _parseContent(loadContent) {
//在加载完成之后解析并调整json数据文件,_a2x_con应该是con.json文件里最后一个被加载的,这个一定在fla生成json文件时注意
//主要工作就是遍历时间轴并调整成方便js读取的方式
var mc;
for (var item in loadContent) {
mc = loadContent[item];
if (mc.t == 1) {
if (!(mc.f instanceof Object)) {
mc.f = [];
continue;
}
if (mc.tf > 1) {
var frameList = mc.f;
var count = frameList.length;
var frameCon = null;
var lastFrameCon = null;
var ol = [];
for (var i = 0; i < count; i++) {
frameCon = frameList[i].c;
//这帧是否为空
if (frameCon instanceof Object) {
for (var j in frameCon) {
var at = frameCon[j].at;
if (at != undefined && at != -1) {
if (at == 0) {
ol.push(j);
}
else {
for (var l = 0; l < ol.length; l++) {
if (ol[l] == at) {
ol.splice(l, 0, j);
break;
}
}
}
delete frameCon[j].at;
}
}
//上一帧是否为空
if (lastFrameCon instanceof Object) {
for (var j in lastFrameCon) {
//上一帧有,这一帧没有,加进来
if (!(frameCon[j] instanceof Object)) {
frameCon[j] = lastFrameCon[j];
}
else {
//上一帧有,这一帧也有那么at就只有-1一种可能
if (frameCon[j].at != -1) {
//如果不为空,则更新元素
for (var m in lastFrameCon[j]) {
//这个地方一定要用undefined。因为有些元素可能为0.当然不是所有的元素都要补,比如滤镜,为空就不需要补
if (frameCon[j][m] == void 0 && m != "fi") {
frameCon[j][m] = lastFrameCon[j][m];
}
}
}
else {
//如果为-1,删除元素
delete frameCon[j];
}
}
}
}
}
lastFrameCon = frameCon;
}
mc.ol = ol;
}
}
}
_loadResCount--;
if (_loadResCount == 0) {
_checkComplete();
}
}
var _loadResCount = 0;
var mediaResourceOnload = function (e) {
if (e != void 0) {
URL.revokeObjectURL(e.target.url);
if (e.target instanceof Image) {
e.target.onload = null;
}
else {
e.target.mediaResourceOnload = null;
}
}
_loadResCount--;
if (_loadResCount == 0) {
_checkComplete();
}
};
// 一个场景加载完成后的事件回调
function _onRESComplete(e) {
var scene = _loadSceneNames[_loadIndex];
var loadContent = e.data.response;
if (!annie._isReleased) {
if (e.data.type != "js" && e.data.type != "css") {
annie.res[scene][_currentConfig[_loadIndex][0].id] = loadContent;
if (_currentConfig[_loadIndex][0].id == "_a2x_con") {
_loadResCount++;
_parseContent(loadContent);
}
else {
if (e.data.type == "image") {
//图片
_loadResCount++;
var image = new Image();
image.onload = mediaResourceOnload;
image.src = URL.createObjectURL(loadContent);
annie.res[scene][_currentConfig[_loadIndex][0].id] = image;
}
else if (e.data.type == "sound") {
//声音
var audio_1 = new Audio();
_loadResCount++;
if (annie.osType == "ios") {
var sFileReader_1 = new FileReader();
sFileReader_1.onload = function () {
audio_1.src = sFileReader_1.result;
mediaResourceOnload(null);
};
sFileReader_1.readAsDataURL(loadContent);
}
else {
audio_1.onloadedmetadata = mediaResourceOnload;
audio_1.src = URL.createObjectURL(loadContent);
}
annie.res[scene][_currentConfig[_loadIndex][0].id] = audio_1;
}
else {
_checkComplete();
}
}
}
else {
_checkComplete();
}
}
else {
//解析swf
var fileReader_1 = new FileReader();
var state_1 = 0;
var lastIndex_1 = 0;
var currIndex_1 = 1;
var JSONData_1;
fileReader_1.readAsText(loadContent.slice(loadContent.size - currIndex_1, loadContent.size));
fileReader_1.onload = function () {
if (state_1 == 0) {
//获取JSON有多少字节
state_1++;
lastIndex_1 = currIndex_1;
currIndex_1 += parseInt(fileReader_1.result);
fileReader_1.readAsText(loadContent.slice(loadContent.size - currIndex_1, loadContent.size - lastIndex_1));
}
else if (state_1 == 1) {
//获取JSON具体字节数
state_1++;
lastIndex_1 = currIndex_1;
currIndex_1 += parseInt(fileReader_1.result);
fileReader_1.readAsText(loadContent.slice(loadContent.size - currIndex_1, loadContent.size - lastIndex_1));
}
else if (state_1 == 2) {
state_1++;
lastIndex_1 = 0;
currIndex_1 = 0;
//解析JSON数据
JSONData_1 = JSON.parse(fileReader_1.result);
lastIndex_1 = currIndex_1;
currIndex_1 += JSONData_1[0].src;
_loadResCount = JSONData_1.length - 1;
fileReader_1.readAsText(loadContent.slice(lastIndex_1, currIndex_1));
}
else if (state_1 == 3) {
state_1++;
annie.Eval(fileReader_1.result);
//解析JSON数据
var _loop_1 = function(i) {
lastIndex_1 = currIndex_1;
currIndex_1 += JSONData_1[i].src;
if (JSONData_1[i].type == "image") {
var image = new Image();
image.onload = mediaResourceOnload;
image.src = URL.createObjectURL(loadContent.slice(lastIndex_1, currIndex_1));
annie.res[scene][JSONData_1[i].id] = image;
}
else if (JSONData_1[i].type == "sound") {
var audio_2 = new Audio();
if (annie.osType == "ios") {
var sFileReader_2 = new FileReader();
sFileReader_2.onload = function () {
audio_2.src = sFileReader_2.result;
mediaResourceOnload(null);
};
sFileReader_2.readAsDataURL(loadContent.slice(lastIndex_1, currIndex_1, "audio/mp3"));
}
else {
audio_2.onloadedmetadata = mediaResourceOnload;
audio_2.src = URL.createObjectURL(loadContent.slice(lastIndex_1, currIndex_1, "audio/mp3"));
}
annie.res[scene][JSONData_1[i].id] = audio_2;
}
else if (JSONData_1[i].type == "json") {
if (JSONData_1[i].id == "_a2x_con") {
var conReader_1 = new FileReader();
conReader_1.onload = function () {
annie.res[scene]["_a2x_con"] = JSON.parse(conReader_1.result);
_parseContent(annie.res[scene]["_a2x_con"]);
conReader_1.onload = null;
};
conReader_1.readAsText(loadContent.slice(lastIndex_1, currIndex_1));
}
}
};
for (var i = 1; i < JSONData_1.length; i++) {
_loop_1(i);
}
}
};
}
}
//检查所有资源是否全加载完成
function _checkComplete() {
if (!annie._isReleased)
_currentConfig[_loadIndex].shift();
_loadedLoadRes++;
_loadPer = _loadedLoadRes / _totalLoadRes;
if (!annie._isReleased && _currentConfig[_loadIndex].length > 0) {
_loadRes();
}
else {
annie.res[_loadSceneNames[_loadIndex]]._f2x_had_loaded_scene = true;
var info = {};
info.sceneName = _loadSceneNames[_loadIndex];
_loadIndex++;
info.sceneId = _loadIndex;
info.sceneTotal = _loadSceneNames.length;
if (_loadIndex == _loadSceneNames.length) {
//全部资源加载完成
_isLoading = false;
_completeCallback(info);
}
else {
_completeCallback(info);
_loadRes();
}
}
}
//加载场景资源
function _loadRes() {
var url = _domain + _currentConfig[_loadIndex][0].src;
if (annie._isReleased) {
_loaderQueue.responseType = "swf";
url += "?v=" + annie._isReleased;
}
else {
url += "?v=" + _time;
}
_loaderQueue.load(url);
}
/**
* 判断一个场景是否已经被加载
* @method annie.isLoadedScene
* @public
* @static
* @since 1.0.0
* @param {string} sceneName
* @return {boolean}
*/
function isLoadedScene(sceneName) {
if (annie.res[sceneName] != undefined && annie.res[sceneName] != null && annie.res[sceneName]._f2x_had_loaded_scene) {
return true;
}
else {
return false;
}
}
annie.isLoadedScene = isLoadedScene;
/**
* 删除一个场景资源,以方便系统垃圾回收
* @method annie.unLoadScene
* @public
* @static
* @since 1.0.2
* @param {string} sceneName
*/
function unLoadScene(sceneName) {
delete annie.res[sceneName];
var w = window;
var scene = w[sceneName];
for (var i in scene) {
delete scene[i];
}
delete w[sceneName];
scene = null;
}
annie.unLoadScene = unLoadScene;
/**
* 获取已经加载场景中的资源
* @method annie.getResource
* @public
* @static
* @since 2.0.0
* @param {string} sceneName
* @param {string} resName
* @return {any}
*/
function getResource(sceneName, resName) {
return annie.res[sceneName][resName];
}
annie.getResource = getResource;
/**
* 新建一个已经加载到场景中的类生成的对象
* @method annie.getDisplay
* @public
* @static
* @since 3.2.1
* @param {string} sceneName
* @param {string} className
* @return {any}
*/
function getDisplay(sceneName, className) {
var Root = window;
return new Root[sceneName][className]();
}
annie.getDisplay = getDisplay;
// 通过已经加载场景中的图片资源创建Bitmap对象实例,此方法一般给Annie2x工具自动调用
function b(sceneName, resName) {
return new annie.Bitmap(annie.res[sceneName][resName]);
}
//用一个对象批量设置另一个对象的属性值,此方法一般给Annie2x工具自动调用
function d(target, info, isMc) {
if (isMc === void 0) { isMc = false; }
if (target._a2x_res_obj == info) {
return;
}
else {
//信息设置的时候看看是不是文本,如果有文本的话还需要设置宽和高
if (info.tr == undefined || info.tr.length == 1) {
info.tr = [0, 0, 1, 1, 0, 0];
}
var lastInfo = target._a2x_res_obj;
if (info.al == void 0) {
info.al = 1;
}
if (info.m == void 0) {
info.m = 0;
}
target.blendMode = info.m;
if (isMc) {
var isUmChange = target.a2x_um;
if (!target._changeTransformInfo[0] && target._x != info.tr[0]) {
target._x = info.tr[0];
isUmChange = true;
}
if (!target._changeTransformInfo[1] && target._y != info.tr[1]) {
target._y = info.tr[1];
isUmChange = true;
}
if (!target._changeTransformInfo[2] && target._scaleX != info.tr[2]) {
target._scaleX = info.tr[2];
isUmChange = true;
}
if (!target._changeTransformInfo[3] && target._scaleY != info.tr[3]) {
target._scaleY = info.tr[3];
isUmChange = true;
}
if (!target._changeTransformInfo[4]) {
if (target._skewX != info.tr[4]) {
target._skewX = info.tr[4];
target._rotation = 0;
isUmChange = true;
}
if (target._skewY != info.tr[5]) {
target._skewY = info.tr[5];
target._rotation = 0;
isUmChange = true;
}
}
target.a2x_um = isUmChange;
if (!target._changeTransformInfo[5] && target._alpha != info.al) {
target._alpha = info.al;
target.a2x_ua = true;
}
}
else {
if (lastInfo.tr != info.tr) {
_a = info.tr, target._x = _a[0], target._y = _a[1], target._scaleX = _a[2], target._scaleY = _a[3], target._skewX = _a[4], target._skewY = _a[5];
target.a2x_um = true;
}
if (target._alpha != info.al) {
target._alpha = info.al;
target.a2x_ua = true;
}
}
if (info.w != undefined) {
target.textWidth = info.w;
target.textHeight = info.h;
}
//动画播放模式 图形 按钮 动画
if (info.t != undefined) {
if (info.t == -1) {
//initButton
if (target.initButton) {
target.initButton();
}
}
target._a2x_mode = info.t;
}
///////////////////////////////////////////
//添加滤镜
if (lastInfo.fi != info.fi) {
if (info.fi != void 0) {
var filters = [];
var blur_1;
var color = void 0;
for (var i = 0; i < info.fi.length; i++) {
switch (info.fi[i][0]) {
case 0:
blur_1 = (info.fi[i][2] + info.fi[i][3]) * 0.5;
color = Shape.getRGBA(info.fi[i][10], info.fi[i][11]);
var offsetX = info.fi[i][4] * Math.cos(info.fi[i][1]);
var offsetY = info.fi[i][4] * Math.sin(info.fi[i][1]);
filters[filters.length] = new ShadowFilter(color, offsetX, offsetY, blur_1);
break;
case 1:
//模糊滤镜
filters[filters.length] = new BlurFilter(info.fi[i][1], info.fi[i][2], info.fi[i][3]);
break;
case 2:
blur_1 = (info.fi[i][1] + info.fi[i][2]) * 0.5;
color = Shape.getRGBA(info.fi[i][7], info.fi[i][6]);
filters[filters.length] = new ShadowFilter(color, 0, 0, blur_1);
break;
case 6:
filters[filters.length] = new ColorMatrixFilter(info.fi[i][1], info.fi[i][2], info.fi[i][3], info.fi[i][4]);
break;
case 7:
filters[filters.length] = new ColorFilter(info.fi[i][1]);
break;
default:
}
}
if (filters.length > 0) {
target.filters = filters;
}
else {
target.filters = null;
}
}
else {
target.filters = null;
}
}
target._a2x_res_obj = info;
}
var _a;
}
annie.d = d;
// 解析数据里需要确定的文本类型
var _textLineType = ["single", "multiline"];
//解析数据里需要确定的文本对齐方式
var _textAlign = ["left", "center", "right"];
//创建一个动态文本或输入文本,此方法一般给Annie2x工具自动调用
function t(sceneName, resName) {
var textDate = annie.res[sceneName]._a2x_con[resName];
var textObj;
var text = decodeURIComponent(textDate[9]);
var font = decodeURIComponent(textDate[4]).replace(/\s(Regular|Medium)/, "");
var size = textDate[5];
var textAlign = _textAlign[textDate[3]];
var lineType = _textLineType[textDate[2]];
var italic = textDate[11];
var bold = textDate[10];
var color = textDate[6];
var textAlpha = textDate[7];
var border = textDate[12];
var lineHeight = textDate[8];
if (textDate[1] == 0 || textDate[1] == 1) {
textObj = new annie.TextField();
textObj.text = text;
textObj.font = font;
textObj.size = size;
textObj.textAlign = textAlign;
textObj.lineType = lineType;
textObj.italic = italic;
textObj.bold = bold;
textObj.color = color;
textObj.textAlpha = textAlpha;
textObj.border = border;
textObj.lineHeight = lineHeight;
}
else {
textObj = new annie.InputText(textDate[2]);
textObj.initInfo(text, color, textAlign, size, font, border, lineHeight);
textObj.italic = italic;
textObj.bold = bold;
}
return textObj;
}
//创建一个Shape矢量对象,此方法一般给Annie2x工具自动调用
function g(sceneName, resName) {
var shapeDate = annie.res[sceneName]._a2x_con[resName][1];
var shape = new annie.Shape();
for (var i = 0; i < shapeDate.length; i++) {
if (shapeDate[i][0] == 1) {
if (shapeDate[i][1] == 0) {
shape.beginFill(annie.Shape.getRGBA(shapeDate[i][2][0], shapeDate[i][2][1]));
}
else if (shapeDate[i][1] == 1) {
shape.beginLinearGradientFill(shapeDate[i][2][0], shapeDate[i][2][1]);
}
else if (shapeDate[i][1] == 2) {
shape.beginRadialGradientFill(shapeDate[i][2][0], shapeDate[i][2][1]);
}
else {
shape.beginBitmapFill(getResource(sceneName, shapeDate[i][2][0]), shapeDate[i][2][1]);
}
shape.decodePath(shapeDate[i][3]);
shape.endFill();
}
else {
if (shapeDate[i][1] == 0) {
shape.beginStroke(annie.Shape.getRGBA(shapeDate[i][2][0], shapeDate[i][2][1]), shapeDate[i][4], shapeDate[i][5], shapeDate[i][6], shapeDate[i][7]);
}
else if (shapeDate[i][1] == 1) {
shape.beginLinearGradientStroke(shapeDate[i][2][0], shapeDate[i][2][1], shapeDate[i][4], shapeDate[i][5], shapeDate[i][6], shapeDate[i][7]);
}
else if (shapeDate[i][1] == 2) {
shape.beginRadialGradientStroke(shapeDate[i][2][0], shapeDate[i][2][1], shapeDate[i][4], shapeDate[i][5], shapeDate[i][6], shapeDate[i][7]);
}
else {
shape.beginBitmapStroke(getResource(sceneName, shapeDate[i][2][0]), shapeDate[i][2][1], shapeDate[i][4], shapeDate[i][5], shapeDate[i][6], shapeDate[i][7]);
}
shape.decodePath(shapeDate[i][3]);
shape.endStroke();
}
}
return shape;
}
// 获取声音实例
function s(sceneName, resName) {
return new annie.Sound(annie.res[sceneName][resName]);
}
/**
* <h4><font color="red">注意:小程序 小游戏不支持</font></h4>
* 向后台请求或者传输数据的快速简便方法,比直接用URLLoader要方便,小巧
* @method annie.ajax
* @public
* @static
* @since 1.0.0
* @param info 向后台传送数据所需要设置的信息
* @param {url} info.url 向后台请求的地址
* @param {string} info.type 向后台请求的类型 get 和 post,默认为get
* @param {Function} info.success 发送成功后的回调方法,后台数据将通过参数传回
* @param {Function} info.error 发送出错后的回调方法,出错信息通过参数传回
* @param {Object} info.data 向后台发送的信息对象,默认为null
* @param {string} info.responseType 后台返回数据的类型,默认为"text"
* @param {boolean} info.isNeedOption 是否需要添加X-Requested-With 头
* @example
* //get
* annie.ajax({
* type: "GET",
* url: serverUrl + "Home/Getinfo/getPersonInfo",
* responseType: 'json',
* success: function (result) {console.log(result)},
* error: function (result) {console.log(result)}
* })
* //post
* annie.ajax({
* type: "POST",
* url: serverUrl + "Home/Getinfo/getPersonInfo",
* data: {phone:'135******58'},
* responseType: 'json',
* success: function (result) {console.log(result)},
* error: function (result) {console.log(result)}
* })
*/
function ajax(info) {
var urlLoader = new URLLoader();
if (info.isNeedOption) {
urlLoader.addHeader("X-Requested-With", "XMLHttpRequest");
}
urlLoader.method = info.type == undefined ? "get" : info.type;
urlLoader.data = info.data == undefined ? null : info.data;
urlLoader.responseType = info.responseType == undefined ? (info.dataType == undefined ? "text" : info.dataType) : info.responseType;
if (info.success instanceof Object) {
urlLoader.addEventListener(annie.Event.COMPLETE, info.success);
}
if (info.error instanceof Object) {
urlLoader.addEventListener(annie.Event.ERROR, info.error);
}
urlLoader.load(info.url);
}
annie.ajax = ajax;
/**
* <h4><font color="red">注意:小程序 小游戏不支持</font></h4>
* jsonp调用方法
* @method annie.jsonp
* @param url
* @param type 0或者1 如果是0,后台返回的是data型jsonp 如果是1,后台返回的是方法型jsonp
* @param callbackName
* @param callbackFun
* @static
* @since 1.0.4
* @example
* annie.jsonp('js/testData.js', 1, 'getdata', function (result) {
* console.log(result);
* })
*/
function jsonp(url, type, callbackName, callbackFun) {
var w = window;
if (type == 1) {
w[callbackName] = callbackFun;
}
var jsonpScript = document.createElement('script');
var head = document.getElementsByTagName('head')[0];
jsonpScript.onload = function (e) {
if (type == 0) {
callbackFun(w[callbackName]);
}
e.path[0].src = "";
w[callbackName] = null;
delete w[callbackName];
head.removeChild(e.path[0]);
};
head.appendChild(jsonpScript);
var param;
if (url.indexOf("?") > 0) {
param = "&";
}
else {
param = "?";
}
jsonpScript.src = url + param + "a_n_n_i_e=" + Math.random() + "&callback=" + callbackName;
}
annie.jsonp = jsonp;
/**
* <h4><font color="red">注意:小程序 小游戏不支持</font></h4>
* 获取url地址中的get参数
* @method annie.getQueryString
* @static
* @param name
* @return {any}
* @since 1.0.9
* @public
* @example
* //如果当前网页的地址为http://xxx.xxx.com?id=1&username=anlun
* //通过此方法获取id和username的值
* var id=annie.getQueryString("id");
* var userName=annie.getQueryString("username");
* console.log(id,userName);
*/
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return decodeURIComponent(r[2]);
return null;
}
annie.getQueryString = getQueryString;
/**
* 引擎自调用.初始化 sprite和movieClip用
* @method annie.initRes
* @param target
* @param {string} sceneName
* @param {string} resName
* @public
* @static
*/
function initRes(target, sceneName, resName) {
var Root = window;
//资源树最顶层
var resRoot = annie.res[sceneName];
//资源树里类对象json数据
var classRoot = resRoot._a2x_con;
//资源树里类对象json数据里非资源类数据
var resClass = classRoot[resName];
//时间轴
target._a2x_res_class = resClass;
var isMc = false;
var i;
if (resClass.tf > 1) {
isMc = true;
if (resClass.timeLine == void 0) {
//将时间轴丰满,抽出脚本,抽出标签
var keyFrameCount = resClass.f.length;
var timeLine = [];
var curKeyFrame = keyFrameCount > 0 ? resClass.f[0].i : resClass.tf;
var nextFrame = 0;
if (curKeyFrame > 0) {
var frameValue = -1;
for (var j = 0; j < curKeyFrame; j++) {
timeLine[timeLine.length] = frameValue;
}
}
if (keyFrameCount > 0) {
for (i = 0; i < keyFrameCount; i++) {
if (i + 1 < keyFrameCount) {
nextFrame = resClass.f[i + 1].i;
}
else {
nextFrame = resClass.tf;
}
curKeyFrame = resClass.f[i].i;
//将时间线补齐
for (var j = 0; j < nextFrame - curKeyFrame; j++) {
timeLine[timeLine.length] = i;
}
}
}
resClass.timeLine = timeLine;
//初始化标签对象方便gotoAndStop gotoAndPlay
if (!(resClass.f instanceof Object))
resClass.f = [];
if (!(resClass.a instanceof Object))
resClass.a = {};
if (!(resClass.s instanceof Object))
resClass.s = {};
if (!(resClass.e instanceof Object))
resClass.e = {};
var label = {};
if (!(resClass.l instanceof Object)) {
resClass.l = [];
}
else {
for (var index in resClass.l) {
for (var n = 0; n < resClass.l[index].length; n++) {
label[resClass.l[index][n]] = parseInt(index) + 1;
}
}
}
resClass.label = label;
}
}
var children = resClass.c;
if (children instanceof Object) {
var allChildren = [];
var objCount = children.length;
var obj = null;
var objType = 0;
var maskObj = null;
var maskTillId = 0;
for (i = 0; i < objCount; i++) {
//if (children[i].indexOf("_$") == 0) {
if (classRoot[children[i]] instanceof Array) {
objType = classRoot[children[i]][0];
}
else {
objType = classRoot[children[i]].t;
}
switch (objType) {
case 1:
case 4:
//text 和 Sprite
//检查是否有名字,并且已经初始化过了
if (resClass.n && resClass.n[i] && target[resClass.n[i]]) {
obj = target[resClass.n[i]];
}
else {
if (objType == 4) {
obj = t(sceneName, children[i]);
}
else {
//displayObject
if (children[i].indexOf("_$") == 0) {
if (classRoot[children[i]].tf > 1) {
obj = new annie.MovieClip();
}
else {
obj = new annie.Sprite();
}
initRes(obj, sceneName, children[i]);
}
else {
obj = new Root[sceneName][children[i]]();
}
}
if (resClass.n && resClass.n[i]) {
target[resClass.n[i]] = obj;
obj.name = resClass.n[i];
}
}
break;
case 2:
//bitmap
obj = b(sceneName, children[i]);
break;
case 3:
//shape
obj = g(sceneName, children[i]);
break;
case 5:
//sound
obj = s(sceneName, children[i]);
obj.name = children[i];
target.addSound(obj);
}
if (!isMc) {
var index = i + 1;
if (objType == 5) {
obj._loop = obj._repeate = resClass.s[0][index];
}
else {
d(obj, resClass.f[0].c[index]);
// 检查是否有遮罩
if (resClass.f[0].c[index].ma != void 0) {
maskObj = obj;
maskTillId = resClass.f[0].c[index].ma - 1;
}
else {
if (maskObj instanceof Object && i <= maskTillId) {
obj.mask = maskObj;
if (i == maskTillId) {
maskObj = null;
}
}
}
target.addChildAt(obj, 0);
}
}
else {
//这里一定把要声音添加到里面,以保证objectId与数组下标对应
allChildren[allChildren.length] = obj;
//如果是声音,还要把i这个顺序保存下来
if (objType == 5) {
obj.isPlaying = false;
if (!(target._a2x_sounds instanceof Object)) {
target._a2x_sounds = {};
}
target._a2x_sounds[i] = obj;
}
}
}
if (isMc) {
//将mc里面的实例按照时间轴上的图层排序
var ol = resClass.ol;
if (ol instanceof Object) {
for (var o = 0; o < ol.length; o++) {
target._a2x_res_children[o] = [ol[o], allChildren[ol[o] - 1]];
}
}
}
}
}
annie.initRes = initRes;
console.log("https://github.com/flash2x/AnnieJS");
})(annie || (annie = {}));