topical media & game development
mobile-graphic-enchant-enchant.min.js / js
/* enchant.js v0.6.3 http://enchantjs.com Copyright (c) Ubiquitous Entertainment Inc. Released Under the MIT license. */
(function(e,t){typeof Object.defineProperty!="function"&&(Object.defineProperty=function(e,t,n){return"value"in n&&(e[t]=n.value),"get"in n&&e.__defineGetter__(t,n.get),"set"in n&&e.__defineSetter__(t,n.set),e}),typeof Object.defineProperties!="function"&&(Object.defineProperties=function(e,t){for(var n in t)t.hasOwnProperty(n)&&Object.defineProperty(e,n,t[n]);return e}),typeof Object.create!="function"&&(Object.create=function(e,t){function n(){}n.prototype=e;var r=new n;return t!=null&&Object.defineProperties(r,t),r}),typeof Object.getPrototypeOf!="function"&&(Object.getPrototypeOf=function(e){return e.__proto__}),typeof Function.prototype.bind!="function"&&(Function.prototype.bind=function(t){var n=this,r=Array.prototype.slice.call(arguments,1),i=function(){},s=function(){var s=r.concat(Array.prototype.slice.call(arguments));return n.apply(this instanceof i?this:t||e,s)};return i.prototype=n.prototype,s.prototype=new i,s}),e.getTime=function(){return e.performance&&e.performance.now?function(){return e.performance.now()}:e.performance&&e.performance.webkitNow?function(){return e.performance.webkitNow()}:Date.now}(),e.requestAnimationFrame=e.requestAnimationFrame||e.mozRequestAnimationFrame||e.webkitRequestAnimationFrame||e.msRequestAnimationFrame||function(){var t=e.getTime(),n=1e3/60;return function(r){var i=e.getTime(),s=setTimeout(function(){r(e.getTime())},Math.max(0,t+n-i));return t=i,s}}();var n=function(t){t!=null&&(t instanceof Array||(t=Array.prototype.slice.call(arguments)),t=t.filter(function(e){return[e].join()})),function r(n,i){var s=[],o,u;for(var a in n)n.hasOwnProperty(a)&&(typeof n[a]=="function"?e[a]=n[a]:typeof n[a]=="object"&&n[a]!==null&&Object.getPrototypeOf(n[a])===Object.prototype&&(t==null?s.push(a):(o=t.indexOf(i+a),o!==-1&&(s.push(a),t.splice(o,1)))));for(o=0,u=s.length;o<u;o++)r(n[s[o]],i+s[o]+".")}(n,""),e.Game=e.Core;if(t!=null&&t.length)throw new Error("Cannot load module: "+t.join(", "))};e.enchant=n,e.addEventListener("message",function(e,t){try{var r=JSON.parse(e.data);if(r.type==="event")n.Core.instance.dispatchEvent(new n.Event(r.value));else if(r.type==="debug")switch(r.value){case"start":n.Core.instance.start();break;case"pause":n.Core.instance.pause();break;case"resume":n.Core.instance.resume();break;case"tick":n.Core.instance._tick();break;default:}}catch(i){}},!1),n.Class=function(e,t){return n.Class.create(e,t)},n.Class.create=function(e,t){if(e==null&&t)throw new Error("superclass is undefined (enchant.Class.create)");if(e==null)throw new Error("definition is undefined (enchant.Class.create)");if(arguments.length===0)return n.Class.create(Object,t);if(arguments.length===1&&typeof arguments[0]!="function")return n.Class.create(Object,arguments[0]);for(var r in t)t.hasOwnProperty(r)&&(typeof t[r]=="object"&&t[r]!==null&&Object.getPrototypeOf(t[r])===Object.prototype?"enumerable"in t[r]||(t[r].enumerable=!0):t[r]={value:t[r],enumerable:!0,writable:!0});var i=function(){if(!(this instanceof i))return new i;i.prototype.initialize.apply(this,arguments)};i.prototype=Object.create(e.prototype,t),i.prototype.constructor=i,i.prototype.initialize==null&&(i.prototype.initialize=function(){e.apply(this,arguments)});var s=this.getInheritanceTree(e);for(var o=0,u=s.length;o<u;o++)if(typeof s[o]._inherited=="function"){s[o]._inherited(i);break}return i},n.Class.getInheritanceTree=function(e){var t=[],n=e,r=n.prototype;while(n!==Object)t.push(n),r=Object.getPrototypeOf(r),n=r.constructor;return t},n.ENV={VERSION:"0.6.1",VENDOR_PREFIX:function(){var e=navigator.userAgent;return e.indexOf("Opera")!==-1?"O":e.indexOf("MSIE")!==-1?"ms":e.indexOf("WebKit")!==-1?"webkit":navigator.product==="Gecko"?"Moz":""}(),TOUCH_ENABLED:function(){var e=document.createElement("div");return e.setAttribute("ontouchstart","return"),typeof e.ontouchstart=="function"}(),RETINA_DISPLAY:function(){if(navigator.userAgent.indexOf("iPhone")!==-1&&e.devicePixelRatio===2){var t=document.querySelector('meta[name="viewport"]');return t==null&&(t=document.createElement("meta"),document.head.appendChild(t)),t.setAttribute("content","width=640"),!0}return!1}(),USE_FLASH_SOUND:function(){var e=navigator.userAgent,t=navigator.vendor||"";return location.href.indexOf("http")===0&&e.indexOf("Mobile")===-1&&t.indexOf("Apple")!==-1}(),USE_DEFAULT_EVENT_TAGS:["input","textarea","select","area"],CANVAS_DRAWING_METHODS:["putImageData","drawImage","drawFocusRing","fill","stroke","clearRect","fillRect","strokeRect","fillText","strokeText"],KEY_BIND_TABLE:{37:"left",38:"up",39:"right",40:"down"},PREVENT_DEFAULT_KEY_CODES:[37,38,39,40,32],SOUND_ENABLED_ON_MOBILE_SAFARI:!1,USE_WEBAUDIO:function(){return location.protocol!=="file:"}(),USE_ANIMATION:!0},n.Event=n.Class.create({initialize:function(e){this.type=e,this.target=null,this.x=0,this.y=0,this.localX=0,this.localY=0},_initPosition:function(e,t){var r=n.Core.instance;this.x=this.localX=(e-r._pageX)/r.scale,this.y=this.localY=(t-r._pageY)/r.scale}}),n.Event.LOAD="load",n.Event.PROGRESS="progress",n.Event.ENTER_FRAME="enterframe",n.Event.EXIT_FRAME="exitframe",n.Event.ENTER="enter",n.Event.EXIT="exit",n.Event.CHILD_ADDED="childadded",n.Event.ADDED="added",n.Event.ADDED_TO_SCENE="addedtoscene",n.Event.CHILD_REMOVED="childremoved",n.Event.REMOVED="removed",n.Event.REMOVED_FROM_SCENE="removedfromscene",n.Event.TOUCH_START="touchstart",n.Event.TOUCH_MOVE="touchmove",n.Event.TOUCH_END="touchend",n.Event.RENDER="render",n.Event.INPUT_START="inputstart",n.Event.INPUT_CHANGE="inputchange",n.Event.INPUT_END="inputend",n.Event.LEFT_BUTTON_DOWN="leftbuttondown",n.Event.LEFT_BUTTON_UP="leftbuttonup",n.Event.RIGHT_BUTTON_DOWN="rightbuttondown",n.Event.RIGHT_BUTTON_UP="rightbuttonup",n.Event.UP_BUTTON_DOWN="upbuttondown",n.Event.UP_BUTTON_UP="upbuttonup",n.Event.DOWN_BUTTON_DOWN="downbuttondown",n.Event.DOWN_BUTTON_UP="downbuttonup",n.Event.A_BUTTON_DOWN="abuttondown",n.Event.A_BUTTON_UP="abuttonup",n.Event.B_BUTTON_DOWN="bbuttondown",n.Event.B_BUTTON_UP="bbuttonup",n.Event.ADDED_TO_TIMELINE="addedtotimeline",n.Event.REMOVED_FROM_TIMELINE="removedfromtimeline",n.Event.ACTION_START="actionstart",n.Event.ACTION_END="actionend",n.Event.ACTION_TICK="actiontick",n.Event.ACTION_ADDED="actionadded",n.Event.ACTION_REMOVED="actionremoved",n.EventTarget=n.Class.create({initialize:function(){this._listeners={}},addEventListener:function(e,t){var n=this._listeners[e];n==null?this._listeners[e]=[t]:n.indexOf(t)===-1&&n.unshift(t)},on:function(){this.addEventListener.apply(this,arguments)},removeEventListener:function(e,t){var n=this._listeners[e];if(n!=null){var r=n.indexOf(t);r!==-1&&n.splice(r,1)}},clearEventListener:function(e){e!=null?delete this._listeners[e]:this._listeners={}},dispatchEvent:function(e){e.target=this,e.localX=e.x-this._offsetX,e.localY=e.y-this._offsetY,this["on"+e.type]!=null&&this["on"+e.type](e);var t=this._listeners[e.type];if(t!=null){t=t.slice();for(var n=0,r=t.length;n<r;n++)t[n].call(this,e)}}}),function(){var t;n.Core=n.Class.create(n.EventTarget,{initialize:function(r,i){if(e.document.body===null)throw new Error("document.body is null. Please excute 'new Core()' in window.onload.");n.EventTarget.call(this);var s=!0;t&&(s=!1,t.stop()),t=n.Core.instance=this,this.width=r||320,this.height=i||320,this.scale=1;var o=document.getElementById("enchant-stage");if(!o)o=document.createElement("div"),o.id="enchant-stage",o.style.position="absolute",document.body.firstChild?document.body.insertBefore(o,document.body.firstChild):document.body.appendChild(o),this.scale=Math.min(e.innerWidth/this.width,e.innerHeight/this.height),this._pageX=0,this._pageY=0;else{var u=e.getComputedStyle(o);r=parseInt(u.width,10),i=parseInt(u.height,10),r&&i?this.scale=Math.min(r/this.width,i/this.height):(o.style.width=this.width+"px",o.style.height=this.height+"px");while(o.firstChild)o.removeChild(o.firstChild);o.style.position="relative";var a=o.getBoundingClientRect();this._pageX=Math.round(e.scrollX||e.pageXOffset+a.left),this._pageY=Math.round(e.scrollY||e.pageYOffset+a.top)}this.scale||(this.scale=1),o.style.fontSize="12px",o.style.webkitTextSizeAdjust="none",this._element=o,this.fps=30,this.frame=0,this.ready=!1,this.running=!1,this.assets={};var f=this._assets=[];(function b(e){e.assets instanceof Array&&[].push.apply(f,e.assets);for(var t in e)e.hasOwnProperty(t)&&typeof e[t]=="object"&&e[t]!==null&&Object.getPrototypeOf(e[t])===Object.prototype&&b(e[t])})(n),this._scenes=[],this.currentScene=null,this.rootScene=new n.Scene,this.pushScene(this.rootScene),this.loadingScene=new n.Scene,this.loadingScene.backgroundColor="#000";var l=this.width*.4|0,c=this.width*.05|0,h=l*.03|0,p=new n.Sprite(l,c);p.x=(this.width-l)/2,p.y=(this.height-c)/2;var d=new n.Surface(l,c);d.context.fillStyle="#fff",d.context.fillRect(0,0,l,c),d.context.fillStyle="#000",d.context.fillRect(h,h,l-h*2,c-h*2),p.image=d;var v=0,m=0;this.addEventListener("progress",function(e){v=e.loaded/e.total}),p.addEventListener("enterframe",function(){m*=.9,m+=v*.1,d.context.fillStyle="#fff",d.context.fillRect(h,0,(l-h*2)*m,c)}),this.loadingScene.addChild(p),this._mousedownID=0,this._surfaceID=0,this._soundID=0,this._activated=!1,this._offsetX=0,this._offsetY=0,this.input={},n.ENV.KEY_BIND_TABLE||(n.ENV.KEY_BIND_TABLE={}),this._keybind=n.ENV.KEY_BIND_TABLE,this.pressedKeysNum=0,this._internalButtondownListeners={},this._internalButtonupListeners={};for(var g in this._keybind)this.keybind(g,this._keybind[g]);if(s){o=n.Core.instance._element;var y;document.addEventListener("keydown",function(e){t.dispatchEvent(new n.Event("keydown")),n.ENV.PREVENT_DEFAULT_KEY_CODES.indexOf(e.keyCode)!==-1&&(e.preventDefault(),e.stopPropagation());if(!t.running)return;var r=t._keybind[e.keyCode];r&&(y=new n.Event(r+"buttondown"),t.dispatchEvent(y))},!0),document.addEventListener("keyup",function(e){if(!t.running)return;var r=t._keybind[e.keyCode];r&&(y=new n.Event(r+"buttonup"),t.dispatchEvent(y))},!0),n.ENV.TOUCH_ENABLED&&(o.addEventListener("touchstart",function(e){var r=e.target.tagName.toLowerCase();n.ENV.USE_DEFAULT_EVENT_TAGS.indexOf(r)===-1&&(e.preventDefault(),t.running||e.stopPropagation())},!0),o.addEventListener("touchmove",function(e){var r=e.target.tagName.toLowerCase();n.ENV.USE_DEFAULT_EVENT_TAGS.indexOf(r)===-1&&(e.preventDefault(),t.running||e.stopPropagation())},!0),o.addEventListener("touchend",function(e){var r=e.target.tagName.toLowerCase();n.ENV.USE_DEFAULT_EVENT_TAGS.indexOf(r)===-1&&(e.preventDefault(),t.running||e.stopPropagation())},!0)),o.addEventListener("mousedown",function(e){var r=e.target.tagName.toLowerCase();n.ENV.USE_DEFAULT_EVENT_TAGS.indexOf(r)===-1&&(e.preventDefault(),t._mousedownID++,t.running||e.stopPropagation())},!0),o.addEventListener("mousemove",function(e){var r=e.target.tagName.toLowerCase();n.ENV.USE_DEFAULT_EVENT_TAGS.indexOf(r)===-1&&(e.preventDefault(),t.running||e.stopPropagation())},!0),o.addEventListener("mouseup",function(e){var r=e.target.tagName.toLowerCase();n.ENV.USE_DEFAULT_EVENT_TAGS.indexOf(r)===-1&&(e.preventDefault(),t.running||e.stopPropagation())},!0),t._touchEventTarget={},n.ENV.TOUCH_ENABLED&&(o.addEventListener("touchstart",function(e){var t=n.Core.instance,r=new n.Event(n.Event.TOUCH_START),i=e.changedTouches,s,o;for(var u=0,a=i.length;u<a;u++)s=i[u],r._initPosition(s.pageX,s.pageY),o=t.currentScene._determineEventTarget(r),t._touchEventTarget[s.identifier]=o,o.dispatchEvent(r)},!1),o.addEventListener("touchmove",function(e){var t=n.Core.instance,r=new n.Event(n.Event.TOUCH_MOVE),i=e.changedTouches,s,o;for(var u=0,a=i.length;u<a;u++)s=i[u],o=t._touchEventTarget[s.identifier],o&&(r._initPosition(s.pageX,s.pageY),o.dispatchEvent(r))},!1),o.addEventListener("touchend",function(e){var t=n.Core.instance,r=new n.Event(n.Event.TOUCH_END),i=e.changedTouches,s,o;for(var u=0,a=i.length;u<a;u++)s=i[u],o=t._touchEventTarget[s.identifier],o&&(r._initPosition(s.pageX,s.pageY),o.dispatchEvent(r),delete t._touchEventTarget[s.identifier])},!1)),o.addEventListener("mousedown",function(e){var t=n.Core.instance,r=new n.Event(n.Event.TOUCH_START);r._initPosition(e.pageX,e.pageY);var i=t.currentScene._determineEventTarget(r);t._touchEventTarget[t._mousedownID]=i,i.dispatchEvent(r)},!1),o.addEventListener("mousemove",function(e){var t=n.Core.instance,r=new n.Event(n.Event.TOUCH_MOVE);r._initPosition(e.pageX,e.pageY);var i=t._touchEventTarget[t._mousedownID];i&&i.dispatchEvent(r)},!1),o.addEventListener("mouseup",function(e){var t=n.Core.instance,r=new n.Event(n.Event.TOUCH_END);r._initPosition(e.pageX,e.pageY),t._touchEventTarget[t._mousedownID].dispatchEvent(r),delete t._touchEventTarget[t._mousedownID]},!1)}},preload:function(e){e instanceof Array||(e=Array.prototype.slice.call(arguments)),[].push.apply(this._assets,e)},load:function(e,r){r==null&&(r=function(){});var i=n.Core.findExt(e);if(n.Core._loadFuncs[i])n.Core._loadFuncs[i].call(this,e,r,i);else{var s=new XMLHttpRequest;s.open("GET",e,!0),s.onreadystatechange=function(i){if(s.readyState===4){if(s.status!==200&&s.status!==0)throw new Error(s.status+": "+"Cannot load an asset: "+e);var o=s.getResponseHeader("Content-Type")||"";o.match(/^image/)?(t.assets[e]=n.Surface.load(e),t.assets[e].addEventListener("load",r)):o.match(/^audio/)?(t.assets[e]=n.Sound.load(e,o),t.assets[e].addEventListener("load",r)):(t.assets[e]=s.responseText,r())}},s.send(null)}},start:function(){var e=function(){this.currentTime=0,this._nextTime=0,this.removeEventListener("load",e),this.running=!0,this.ready=!0,this._requestNextFrame()};this.addEventListener("load",e);if(!this._activated&&this._assets.length){if(n.ENV.SOUND_ENABLED_ON_MOBILE_SAFARI&&!t._touched&&(navigator.userAgent.indexOf("iPhone OS")!==-1||navigator.userAgent.indexOf("iPad")!==-1)){var r=new n.Scene;r.backgroundColor="#000";var i=Math.round(t.width/10),s=new n.Sprite(t.width,i);s.y=(t.height-i)/2,s.image=new n.Surface(t.width,i),s.image.context.fillStyle="#fff",s.image.context.font=i-1+"px bold Helvetica,Arial,sans-serif";var o=s.image.context.measureText("Touch to Start").width;s.image.context.fillText("Touch to Start",(t.width-o)/2,i-1),r.addChild(s),document.addEventListener("touchstart",function(){t._touched=!0,t.removeScene(r),t.start()},!0),t.pushScene(r);return}this._activated=!0;var u={},a=this._assets.filter(function(e){return e in u?!1:u[e]=!0}),f=0,l=a.length,c=function(){var e=new n.Event("progress");e.loaded=++f,e.total=l,t.dispatchEvent(e),f===l&&(t.removeScene(t.loadingScene),t.dispatchEvent(new n.Event("load")))};for(var h=0;h<l;h++)this.load(a[h],c);this.pushScene(this.loadingScene)}else this.dispatchEvent(new n.Event("load"))},debug:function(){this._debug=!0,this.start()},actualFps:{get:function(){return this._actualFps||this.fps}},_requestNextFrame:function(){if(!this.ready)return;var t=this;e.requestAnimationFrame(t._checkTick)},_checkTick:function(t){var r=n.Core.instance;r._nextTime<t?r._tick(t):e.requestAnimationFrame(r._checkTick)},_tick:function(e){var t=new n.Event("enterframe");this.currentTime===0?t.elapsed=0:t.elapsed=e-this.currentTime,this._nextTime=e+1e3/this.fps,this.currentTime=e,this._actualFps=t.elapsed>0?1e3/t.elapsed:0;var r=this.currentScene.childNodes.slice(),i=Array.prototype.push;while(r.length){var s=r.pop();s.age++,s.dispatchEvent(t),s.childNodes&&i.apply(r,s.childNodes)}this.currentScene.age++,this.currentScene.dispatchEvent(t),this.dispatchEvent(t),this.dispatchEvent(new n.Event("exitframe")),this.frame++,this._requestNextFrame()},getTime:function(){return e.getTime()},stop:function(){this.ready=!1,this.running=!1},pause:function(){this.ready=!1},resume:function(){if(this.ready)return;this.currentTime=0,this.ready=!0,this.running=!0,this._requestNextFrame()},pushScene:function(e){return this._element.appendChild(e._element),this.currentScene&&this.currentScene.dispatchEvent(new n.Event("exit")),this.currentScene=e,this.currentScene.dispatchEvent(new n.Event("enter")),this._scenes.push(e)},popScene:function(){return this.currentScene===this.rootScene?this.currentScene:(this._element.removeChild(this.currentScene._element),this.currentScene.dispatchEvent(new n.Event("exit")),this.currentScene=this._scenes[this._scenes.length-2],this.currentScene.dispatchEvent(new n.Event("enter")),this._scenes.pop())},replaceScene:function(e){return this.popScene(),this.pushScene(e)},removeScene:function(e){if(this.currentScene===e)return this.popScene();var t=this._scenes.indexOf(e);return t!==-1?(this._scenes.splice(t,1),this._element.removeChild(e._element),e):null},keybind:function(e,t){this._keybind[e]=t;var r=function(e){var r;this.input[t]||(this.input[t]=!0,r=new n.Event(this.pressedKeysNum++?"inputchange":"inputstart"),this.dispatchEvent(r),this.currentScene.dispatchEvent(r)),this.currentScene.dispatchEvent(e)},i=function(e){var r;this.input[t]&&(this.input[t]=!1,r=new n.Event(--this.pressedKeysNum?"inputchange":"inputend"),this.dispatchEvent(r),this.currentScene.dispatchEvent(r)),this.currentScene.dispatchEvent(e)};this.addEventListener(t+"buttondown",r),this.addEventListener(t+"buttonup",i),this._internalButtondownListeners[e]=r,this._internalButtonupListeners[e]=i},keyunbind:function(e){if(!this._keybind[e])return;var t=this._internalButtondownListeners,n=this._internalButtonupListeners;this.removeEventListener(e+"buttondown",t),this.removeEventListener(e+"buttonup",n),delete t[e],delete n[e],delete this._keybind[e]},getElapsedTime:function(){return this.frame/this.fps}}),n.Core._loadFuncs={},n.Core._loadFuncs.jpg=n.Core._loadFuncs.jpeg=n.Core._loadFuncs.gif=n.Core._loadFuncs.png=n.Core._loadFuncs.bmp=function(e,t){this.assets[e]=n.Surface.load(e),this.assets[e].addEventListener("load",t)},n.Core._loadFuncs.mp3=n.Core._loadFuncs.aac=n.Core._loadFuncs.m4a=n.Core._loadFuncs.wav=n.Core._loadFuncs.ogg=function(e,t,r){this.assets[e]=n.Sound.load(e,"audio/"+r,t)},n.Core.findExt=function(e){var t=e.match(/\.\w+,J,K,Q,G,Y,Z,et,tt,nt,rt,it,st,ot,ut;if(k[0]*(F-h)-k[1]*(j-c)>0&&L[0]*(F-d)-L[1]*(j-p)>0&&A[0]*(F-y)-A[1]*(j-g)>0&&O[0]*(F-m)-O[1]*(j-v)>0)return!0;if(M[0]*(B-w)-M[1]*(H-b)>0&&_[0]*(B-S)-_[1]*(H-E)>0&&D[0]*(B-C)-D[1]*(H-N)>0&&P[0]*(B-T)-P[1]*(H-x)>0)return!0;R=[r,i,o,s],U=[u,a,l,f],z=[k,L,A,O],W=[M,_,D,P];for(I=0;I<4;I++){X=R[I],K=X[0],Q=X[1],[0],et=
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.