API Docs for: 最后更新日期:2014年8月25日
Google搜索   
Show:

File: main/Function.js

/** @language japanese
 * @class グローバル関数
 * @since 1.0.0
 * @public
 */
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function (elt) {
		var len = this.length >>> 0;
		var from = Number(arguments[1]) || 0;
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0) {
			from += len;
		}
		for (; from < len; from++){
			if (from in this && this[from] === elt) {
				return from;
			}
		}
		return -1;
	};
}
if (!Array.isArray){
	Array.isArray = function(value){
		return Object.prototype.toString.apply(value) == '[object Array]';
	};
}
if (!Function.prototype.bind) {
	Function.prototype.bind = function (oThis) {
		if (typeof this !== "function") {
			throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
		}
		var aArgs = Array.prototype.slice.call(arguments, 1), 
			fToBind = this, 
			fNOP = function () {},
			fBound = function () {
			return fToBind.apply(this instanceof fNOP && oThis ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
        };
		fNOP.prototype = this.prototype;
		fBound.prototype = new fNOP();
		return fBound;
	};
}
if (!Array.prototype.find) {
	Array.prototype.find = function (predicate) {
		if (this == null) {
			throw new TypeError('Array.prototype.find called on null or undefined');
		}
		if (typeof predicate !== 'function') {
			throw new TypeError('predicate must be a function');
		}
		var list = Object(this);
		var length = list.length >>> 0;
		var thisArg = arguments[1];
		var value;
		for (var i = 0; i < length; i++) {
			value = list[i];
			if (predicate.call(thisArg, value, i, list)) {
				return value;
			}
		}
		return undefined;
	};
}
if (!Array.prototype.findIndex) {
	Array.prototype.findIndex = function (predicate) {
		if (this == null) {
			throw new TypeError('Array.prototype.find called on null or undefined');
		}
		if ( typeof predicate !== 'function') {
			throw new TypeError('predicate must be a function');
		}
		var list = Object(this);
		var length = list.length >>> 0;
		var thisArg = arguments[1];
		var value;
		for (var i = 0; i < length; i++) {
			value = list[i];
			if (predicate.call(thisArg, value, i, list)) {
				return i;
			}
		}
		return -1;
	};
}
if (!Array.prototype.forEach) {
	Array.prototype.forEach = function (callback, thisArg) {
		var T, k;
		if (this == null) {
			throw new TypeError(' this is null or not defined');
		}
		var O = Object(this);
		var len = O.length >>> 0;
		if ( typeof callback !== "function") {
			throw new TypeError(callback + ' is not a function');
		}
		if (arguments.length > 1) {
			T = thisArg;
		}
		k = 0;
		while (k < len) {
			var kValue;
			if ( k in O) {
				kValue = O[k];
				callback.call(T, kValue, k, O);
			}
			k++;
		}
	};
}
if (!Array.prototype.every) {
	Array.prototype.every = function(callbackfn, thisArg) {
		'use strict';
		var T, k;
		if (this == null) {
			throw new TypeError('this is null or not defined');
		}
		var O = Object(this);
		var len = O.length >>> 0;
		if ( typeof callbackfn !== 'function') {
			throw new TypeError();
		}
		if (arguments.length > 1) {
			T = thisArg;
		}
		k = 0;
		while (k < len) {
			var kValue;
			if ( k in O) {
				kValue = O[k];
				var testResult = callbackfn.call(T, kValue, k, O);
				if (!testResult) {
					return false;
				}
			}
			k++;
		}
		return true;
	};
}
if (!Array.prototype.some) {
	Array.prototype.some = function(fun) {
		'use strict';
		if (this == null) {
			throw new TypeError('Array.prototype.some called on null or undefined');
		}
		if ( typeof fun !== 'function') {
			throw new TypeError();
		}
		var t = Object(this);
		var len = t.length >>> 0;
		var thisArg = arguments.length >= 2 ? arguments[1] :
		void 0;
		for (var i = 0; i < len; i++) {
			if ( i in t && fun.call(thisArg, t[i], i, t)) {
				return true;
			}
		}
		return false;
	};
}
/** @language japanese
 * Debugモード を使用すると、trace() 関数の出力を取得し、その結果を表示できます。trace ステートメント内の引数に String 以外のデータ型が含まれる場合、trace 関数はそのデータ型に関連した toString() メソッドを呼び出します。たとえば、引数がブール値の場合、trace 関数は Boolean.toString() を呼び出して戻り値を表示します。
 * @method trace
 * @param {Object} expression 評価する式。expression パラメータの値が [出力] パネルに表示されます。
 * @example
 * 	trace("debug text 1", "debug text 2", "debug text 3");
 * @examplelink <p><a href="../../../api/GlobalFunctions/trace.html" target="_blank">実際のサンプルを見る</a></p>
 * @since 1.0.0
 * @public
 */
function trace() {
	if (!LGlobal.traceDebug) return;
	var t = document.getElementById("traceObject"), i;
	if (trace.arguments.length > 0 && t == null) {
		t = document.createElement("TEXTAREA");
		t.id = "traceObject";
		t.style.position = "absolute";
		t.style.top = (LGlobal.height + 20) + "px";
		t.style.width = LGlobal.width + "px";
		t.style.height = "200px";
		document.body.appendChild(t);
	}
	for (i = 0; i < trace.arguments.length; i++) {
		t.value = t.value + trace.arguments[i] + "\r\n";
		t.scrollTop = t.scrollHeight;
	}
}
/** @language japanese
 * Stageに子 DisplayObject インスタンスを追加します。
 * @method addChild
 * @param {Object} expression 追加される DisplayObject インスタンスです。
 * @example
 * 	var backLayer = LSprite();
 * 	addChild(backLayer);
 * @since 1.0.0
 * @public
 */
function addChild (o) {
	LGlobal.stage.addChild(o);
}
/** @language japanese
 * Stage インスタンスの子リストから指定の child DisplayObject インスタンスを削除します。
 * @method removeChild
 * @param {Object} expression 削除する DisplayObject インスタンスです。
 * @example
 * 	var backLayer = LSprite();
 * 	addChild(backLayer);
 * 	removeChild(backLayer);
 * @since 1.0.0
 * @public
 */
function removeChild (o) {
	LGlobal.stage.removeChild(o);
}
/** @language japanese
 * ライブラリの初期化。init と同等。
 * @method LInit
 * @param {float} speed ゲームスピード(单位:ミリ秒), FPS = 1000 / speed。
 * @param {String} divid divタブのid,ライブラリの初期化をする時,自動的にこのdivタブの中にcanvasを生成する。
 * @param {int} width ゲーム画面の幅。
 * @param {int} height ゲーム画面の高さ。
 * @param {Function} callback ライブラリの初期化が終わったら,この関数を呼び出す。
 * @param {String} type ライブラリの初期化がwindow.onloadの後にしたら,このパラメータをLEvent.INITに設定しなければなりません。
 * @example
 * 	<!DOCTYPE html>
 * 	<html>
 * 	<head>
 * 	<meta charset="UTF-8">
 * 	<script type="text/javascript" src="../lufylegend-x.x.x.min.js"></script> 
 * 	<title>demo</title>
 * 	</head>
 * 	<body>
 * 	<div id="mylegend">loading……</div>
 * 	<script>
 * 	LInit(50,"mylegend",800,480,main);
 * 	//window.onload = function(){LInit(50, "mylegend", 800, 480, main, LEvent.INIT);};
 * 	function main(){
 * 		alert("Hello lufylegend!");
 * 	}
 * 	</script>
 * 	</body>
 * 	</html>
 * @examplelink <p><a href="../../../api/GlobalFunctions/LInit.html" target="_blank">実際のサンプルを見る</a></p>
 * @since 1.0.0
 * @public
 */
function init (s, c, w, h, f, t) {
	LGlobal.speed = s;
	var _f = function () {
		if (LGlobal.canTouch && LGlobal.aspectRatio == LANDSCAPE && window.innerWidth < window.innerHeight) {
			LGlobal.horizontalError();
		} else if (LGlobal.canTouch && LGlobal.aspectRatio == PORTRAIT && window.innerWidth > window.innerHeight) {
			LGlobal.verticalError();
		} else {
			setTimeout(f, 100);
		}
		LGlobal.startTimer = (new Date()).getTime();
	};
	if (t != null && t == LEvent.INIT) {
		LGlobal.frameRate = setInterval(function () {
			LGlobal.onShow();
		}, s);
		LGlobal.setCanvas(c, w, h);
		_f();
	}else{
		LEvent.addEventListener(window, "load", function () {
			LGlobal.frameRate = setInterval(function () {
				LGlobal.onShow();
			}, s);
			LGlobal.setCanvas(c, w, h);
			_f();
		});
	}
}
var LInit = init;
/** @language japanese
 * オブジェクトの継承。base と同等。
 * @method LExtends
 * @param {LDisplayObject} child 子供オブジェクト。
 * @param {Object} parent 親オブジェクト。
 * @param {Array} params パラメータ。
 * @example
 * 	LInit(50, "legend", 800, 480, main);
 * 	function FatherClass(){
 * 		this.name = "Father";
 * 	}
 * 	FatherClass.prototype.getName = function(){
 * 		return this.name;
 * 	};
 * 	function ChildClass(){
 * 		LExtends(this,FatherClass,[]);
 * 		this.name = "Child";
 * 	}
 * 	function main () {
 * 		LGlobal.setDebug(true);
 * 		var father = new FatherClass();
 * 		var child = new ChildClass();
 * 		trace("father.getName() = " + father.getName()); //father.getName() = Father
 * 		trace("child.getName() = " + child.getName());//child.getName() = Child
 * 	}
 * @examplelink <p><a href="../../../api/GlobalFunctions/LExtends.html" target="_blank">実際のサンプルを見る</a></p>
 * @since 1.0.0
 * @public
 */
function base (d, b, a) {
	var p = null, o = d.constructor.prototype, h = {};
	if(d.constructor.name == "Object"){
		console.warn( "When you use the extends. You must make a method like 'XX.prototype.xxx=function(){}'. but not 'XX.prototype={xxx:function(){}}'.");
	}
	d.__ll__parent__ = d.__ll__parent__ || [];
	d.__ll__parent__.push(b.prototype);
	for (p in o) {
		h[p] = 1;
	}
	for (p in b.prototype) {
		if (!h[p]) {
			o[p] = b.prototype[p];
		}
	}
	if (o.toString == Object.prototype.toString) {
		o.toString = LObject.prototype.toString;
	}
	b.apply(d, a);
}
var LExtends = base;
/** @language japanese
 * ライブラリの初期化終わってからの経過時間をミリ秒単位で返します。
 * @method getTimer
 * @return {float} ライブラリの初期化終わってからの経過時間。
 * @since 1.0.0
 * @public
 */
function getTimer () {
	return (new Date()).getTime() - LGlobal.startTimer;
}
function getExtension (path) {
	var r, pattern = /([^#?]+\.)([^.#?]+)/;
	r = path.match(pattern);
	if (r.length >= 3) {
		return r[2].toLowerCase();
	}
	return null;
}