Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

method chaining

  • Loading branch information...
commit 0d6d7f1aaebd515f7f87b593d0d74d1839b757f0 1 parent 7fbda31
@michalbe authored
View
4 README.md
@@ -19,6 +19,10 @@ Mibbu gives you everything you need for fast prototyping your Javascript game in
### Change Log ###
+2011 06 12 - **0.3/arkansas** (5.89 KB, gzip 2.37 KB)
+
+* every method now return itself when you call it with parameters (like .speed(3)) so it's possile to connect multiple methods into one chain (like background.speed(4).direction(-20)). Methods called without arguments returns value of given metod (like background.speed() return the speed of the background)
+
2011 06 12 - **0.2.4/annefrank** (5.57 KB, gzip 2.30 KB) by [MartinDoms](https://github.com/MartinDoms)
* background direction could be now specified in both, strings ("N", "S", "E", "W") and numbers (radians)
View
60 example/index-minifiedLib.html
@@ -1,6 +1,6 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
- <head>
+ <head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Mibbu test</title>
@@ -15,20 +15,13 @@
var Game = new mibbu(500, 500);
//uncomment line below for disabling
- //canvas and swiching to DOM rendering
- Game.canvasOff();
-
- //comment line below (only in DOM mode) to enable CSS3 Animations
- //(only in webkit browsers)
- //Game.cssAnimationOff();
-
- //show FPS counter
- Game.fps();
-
- //now init all the elements - before calling
- //init funciton Mibbu didn't know if you want
- //to render it on canvas or using DOM
- Game.init();
+ //
+ Game.fps() //show FPS counter
+ .canvasOff() //comment this line for disabling canvas and swiching to DOM rendering
+ //.cssAnimationOff()//uncomment this in DOM mode to disable CSS3 Animations (only in webkit & Firefox Beta browsers)
+ .init() //now init all the elements - before calling
+ //init() funciton Mibbu didn't know if you want
+ //to render it on canvas or using DOM
//create two sprites using reptile.png image, with
//width & height of single frame equals to 200,
@@ -39,40 +32,43 @@
//create new background using bg.jpg file and set it's speed
//to 6, direction to SOUTH, and initial position to (0,0)
background = new Game.bg('img/bg.jpg', 6, "S", {x:0,y:0});
-
+
+
//set position of 1st sprite to (100, 100) and
//it's x-index to 2
- sprite.position(100, 100, 2);
-
- //set speed of redrwing the sprite frames
- sprite.speed(4);
+ //then speed of redrwing the sprite frames
+ sprite.position(100, 100, 2).speed(4);
//extend sprite object with some additional
//variables fr control movement etc.
//it is up to you how you will achieve this -
//- it is not part of Mibbu
- sprite.y = 100;
+ sprite.y = 100;
sprite.d = 1;
sprite.type = 0;
//the same here
- //but we create variable for the speed of the animation
+ //but we create variable for the speed of the animation
//we will use it later
- var speed = sprite2.speed(4);
-
- sprite2.position(100, 300, 2);
+ var speed = 4;
+
+ sprite2.speed(4).position(100, 300, 2);
sprite2.y = 300;
sprite2.d = -1;
//resize second sprite and remember
//it's size in the file (functions like
- //size(), frames(), etc. always return
- //their values, even if you call them
+ //size(), frames(), etc. return
+ //their values when you call them
//without parameters, like sprite2.size();
- var size = sprite2.size(100, 100);
+ //We use chaining in here, so to receive
+ //size we have to use that dirty hack with
+ //calling one function twice
+ var size = sprite2.size(100, 100).size();
//start moving background
- background.on();
+ //and setting random direction and speed
+ background.speed(~~(Math.random()*10)+1).dir(~~(Math.random()*180)).on();
//start main game loop
Game.on();
@@ -95,10 +91,10 @@
}
//now add that function to the loop
- Game.hook(additionalLoop);
+ //and start checking for the collisions
+ Game.hook(additionalLoop).hitsOn();
- //lets check for the collisions
- Game.hitsOn();
+
//if 'sprite' will collide with 'sprite2' then:
sprite.hit(sprite2, function(){
View
58 example/index.html
@@ -15,20 +15,13 @@
var Game = new mibbu(500, 500);
//uncomment line below for disabling
- //canvas and swiching to DOM rendering
- Game.canvasOff();
-
- //comment line below (only in DOM mode) to enable CSS3 Animations
- //(only in webkit & Firefox Beta browsers)
- //Game.cssAnimationOff();
-
- //show FPS counter
- Game.fps();
-
- //now init all the elements - before calling
- //init funciton Mibbu didn't know if you want
- //to render it on canvas or using DOM
- Game.init();
+ //
+ Game.fps() //show FPS counter
+ .canvasOff() //comment this line for disabling canvas and swiching to DOM rendering
+ //.cssAnimationOff()//uncomment this in DOM mode to disable CSS3 Animations (only in webkit & Firefox Beta browsers)
+ .init() //now init all the elements - before calling
+ //init() funciton Mibbu didn't know if you want
+ //to render it on canvas or using DOM
//create two sprites using reptile.png image, with
//width & height of single frame equals to 200,
@@ -39,40 +32,43 @@
//create new background using bg.jpg file and set it's speed
//to 6, direction to SOUTH, and initial position to (0,0)
background = new Game.bg('img/bg.jpg', 6, "S", {x:0,y:0});
-
+
+
//set position of 1st sprite to (100, 100) and
//it's x-index to 2
- sprite.position(100, 100, 2);
-
- //set speed of redrwing the sprite frames
- sprite.speed(4);
+ //then speed of redrwing the sprite frames
+ sprite.position(100, 100, 2).speed(4);
//extend sprite object with some additional
//variables fr control movement etc.
//it is up to you how you will achieve this -
//- it is not part of Mibbu
- sprite.y = 100;
+ sprite.y = 100;
sprite.d = 1;
sprite.type = 0;
//the same here
- //but we create variable for the speed of the animation
+ //but we create variable for the speed of the animation
//we will use it later
- var speed = sprite2.speed(4);
-
- sprite2.position(100, 300, 2);
+ var speed = 4;
+
+ sprite2.speed(4).position(100, 300, 2);
sprite2.y = 300;
sprite2.d = -1;
//resize second sprite and remember
//it's size in the file (functions like
- //size(), frames(), etc. always return
- //their values, even if you call them
+ //size(), frames(), etc. return
+ //their values when you call them
//without parameters, like sprite2.size();
- var size = sprite2.size(100, 100);
+ //We use chaining in here, so to receive
+ //size we have to use that dirty hack with
+ //calling one function twice
+ var size = sprite2.size(100, 100).size();
//start moving background
- background.on();
+ //and setting random direction and speed
+ background.speed(~~(Math.random()*10)+1).dir(~~(Math.random()*180)).on();
//start main game loop
Game.on();
@@ -95,10 +91,10 @@
}
//now add that function to the loop
- Game.hook(additionalLoop);
+ //and start checking for the collisions
+ Game.hook(additionalLoop).hitsOn();
- //lets check for the collisions
- Game.hitsOn();
+
//if 'sprite' will collide with 'sprite2' then:
sprite.hit(sprite2, function(){
View
2  mibbu-min.js
@@ -1 +1 @@
-var p=void 0,mibbu=function(K,L,B){function q(){for(var c=m.length,e,d,f,g,i,b,a,C,D;c--;)for(e in d=m[c],g=d.c+d.e.A,i=d.c+d.height-d.e.q,b=d.b+d.e.u,d=d.b+d.width-d.e.w,m[c].t)if(f=E[e],a=f.c+f.e.A,C=f.c+f.height-f.e.q,D=f.b+f.e.u,f=f.b+f.width-f.e.w,!(g>C||i<a||b>f||d<D))m[c].t[e]()}function F(){G();for(var c=e.length;c--;)e[c].G();for(c=h.length;c--;)h[c]();if(s){c=+new Date;~~(c-v)>=1E3&&(H=w,w=0,v=c);c="FPS: "+H;if(i)n.fillText(c,4,15);else if(t)t.innerHTML=c;w++}x&&(I=J(F,g))}function M(){g=l.createElement("canvas");n=g.getContext("2d");n.i=n.drawImage;e.sort(function(c,e){return c.j-e.j})}function N(){G=i?function(){n.clearRect(0,0,y,z)}:function(){};s&&(v=new Date);J=function(){return window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(c){setTimeout(c,1E3/60)}}();o=g.style;g.width=y;g.height=z;o.width=y+"px";o.height=z+"px";o.position="absolute";o.overflow="hidden";A.appendChild(g)}var i=!0,k=!1,l=document,e=[],A=B?l.getElementById(B):l.body,g,n,y=K||400,z=L||300,h=[],I,G,v=new Date,s=!1,t,m=[],E=[],J,o,u,j;Array.prototype.i=Array.prototype.indexOf||function(c){for(var e=this.length;e--&&this[e]!==c;);return e};var w=0,H=0,x=!0;l.createElement("canvas").getContext||(i=!1);return{fps:function(){s=!0},init:function(){i?M():(g=l.createElement("div"),s&&(t=l.createElement("div"),g.appendChild(t)));N()},on:function(){x=!0;F();if(k)for(var c=e.length;c--;)e[c].a&&(e[c].a.style[j+"AnimationDuration"]=~~(1/(60/e[c].f)*100)/100*(e[c].h+1)+"s")},off:function(){clearTimeout(I);x=!1;if(k)for(var c=e.length;c--;)e[c].a&&(e[c].a.style[j+"AnimationDuration"]=0)},canvas:function(){return g},ctx:function(){return n},canvasOff:function(){i=!1;typeof A.style.WebkitAnimation!=="undefined"?(u="-webkit-",j="Webkit",k=!0):typeof A.style.MozAnimation!=="undefined"&&(u="-moz-",j="Moz",k=!0)},cssAnimationOff:function(){k=!1},hitsOn:function(){h.i(q)===-1&&h.push(q)},hitsOff:function(){h.i(q)!==-1&&h.splice(h.i(q),1)},spr:function(c,h,d,f,o){function r(){for(var b="@"+u+"keyframes s"+a.id+" {\n",c=100/(a.h+1),e="% { "+u+"transform: translate(",d=0;d<a.h+1;d++)b+=~~(c*d*100)/100+e+a.k*a.width*-1+"px,"+d*a.height*-1+"px); }\n",b+=~~((c*(d+1)-0.01)*100)/100+e+a.k*a.width*-1+"px,"+d*a.height*-1+"px); }\n";return b+("100"+e+a.k*a.width+"px, 0px); }\n}")}var b=i?function(){try{n.i(a.a,a.F*a.k,a.D*a.n,a.F,a.D,a.b,a.c,a.width,a.height)}catch(b){}}:k?function(){}:function(){a.d.top=a.height*a.n*-1+"px";a.d.left=a.width*a.k*-1+"px"},a={};a.id=e.length;a.a=new Image;a.a.src=c;a.f=1;a.width=h;a.F=h;a.height=d;a.D=d;a.h=f;a.H=o;a.I=!1;a.t={};a.n=0;a.k=0;a.f=1;a.p=0;a.b=0;a.c=0;a.j=1;a.s=null;a.r=0;a.B=0;a.e={A:0,u:0,q:0,w:0};if(!i){a.C=l.createElement("div");a.g=a.C.style;a.g.overflow="hidden";a.g.width=h+"px";a.g.height=d+"px";a.g.position="absolute";a.g.zIndex=a.j;a.d=a.a.style;a.d.position="absolute";if(k)a.o=l.createElement("style"),a.o.innerHTML=r(),l.body.appendChild(a.o),a.d[j+"Animation"]="s"+a.id+" "+~~(1/(60/a.f)*100)/100*(a.h+1)+"s linear 0s infinite";a.C.appendChild(a.a);g.appendChild(a.C)}a.id=e.push(a)-1;E.push(a);a.G=function(){if(a.h>0){if(a.p==a.f&&a.f!==0){if(a.n==a.h){if(a.n=0,typeof a.s==="function"&&(a.r++,a.r===a.B))a.s(),a.r=0}else a.n++;a.p=0}a.f!==0&&a.p++;b()}};return{position:function(b,c,d){a.b=b||a.b;a.c=c||a.c;a.j=d||a.j;i?d&&e.sort(function(a,b){return b.j-a.j}):(a.g.left=b+"px",a.g.top=c+"px",a.g.zIndex=d||a.j);return{x:a.b,y:a.c,J:a.j}},hit:function(b,c){m.i(a)===-1&&m.push(a);a.t[b.id()]=c;m.i(a)===-1&&m.push(a)},zone:function(b,c,d,e){if(b!==p)a.e.u=e,a.e.A=b,a.e.w=c,a.e.q=d;return a.e},noHits:function(){a.t={}},callback:function(b,c){a.s=b;a.B=c},change:function(b,c,d,e,f){a.a.src=b;a.width=c;a.height=d;a.F=c;a.D=d;a.h=e;a.k=f;a.p=0;a.n=0;a.s=null;a.r=0;a.B=0;if(!i&&(a.d.width=c*(a.k+1)+"px",a.d.height=d*(a.h+1)+"px",a.g.width=c+"px",a.g.height=d+"px",k))a.d[j+"AnimationName"]="",a.o.innerHTML=r(),a.d[j+"AnimationName"]="s"+a.id;a.e={A:0,u:0,q:0,w:0}},size:function(b,c){if(b!==p){if(!i)a.g.width=b+"px",a.g.height=c+"px",a.d.width=b*(a.H+1)+"px",a.d.height=c*(a.h+1)+"px";a.width=b;a.height=c;if(k)a.d[j+"AnimationName"]="",a.o.innerHTML=r(),a.d[j+"AnimationName"]="s"+a.id}return{width:a.width,height:a.height}},speed:function(b){if(b!==p)a.f=b,a.p=0,k&&(a.d[j+"AnimationDuration"]=~~(1/(60/b)*100)/100*(a.h+1)+"s");return a.f},animation:function(b){if(b!==p&&(a.k=b,k))a.d[j+"AnimationName"]="",a.o.innerHTML=r(),a.d[j+"AnimationName"]="s"+a.id;return a.k},frame:function(b){if(b!==p)a.n=b;return a.n},id:function(){return a.id}}},bg:function(c,h,d,f){function j(c){b.l=0;b.m=0;if(typeof c==="string")switch(c){case "N":b.l=0;b.m=-1;break;case "W":b.l=-1;b.m=0;break;case "S":b.l=0;b.m=1;break;case "E":b.l=1,b.m=0}else if(typeof c==="number")c*=a,b.l=Math.cos(c),b.m=Math.sin(c)}var k=i?function(){try{for(var a=b.c%b.a.height,c=b.b%b.a.width-b.a.width;c<g.width;c+=b.a.width)for(var d=a-b.a.height;d<g.height;d+=b.a.height)n.i(b.a,c,d)}catch(e){}if(b.l<0){if(b.b<b.a.width*-1)b.b=0}else if(b.l>0&&b.b>b.a.width)b.b=0;if(b.m<0){if(b.c<b.a.height*-1)b.c=0}else if(b.m>0&&b.c>b.a.height)b.c=0}:function(){var a=b.b;a.toString().indexOf("e")!=-1&&(a=0);var c=b.c;c.toString().indexOf("e")!=-1&&(c=0);g.style.backgroundPosition=a+"px "+c+"px"},b=this;i?(b.a=new Image,b.a.src=c):g.style.backgroundImage="url("+c+")";b.f=h||3;var a=Math.PI/180;j(d);b.j=f.z||0;b.b=f.x||0;b.c=f.y||0;b.id=e.push(b);b.v=0;b.G=function(){b.b+=b.f*b.l*b.v;b.c+=b.f*b.m*b.v;k()};return{on:function(){b.v=1},off:function(){b.v=0},dir:function(a){j(a)},speed:function(a){if(a!==p)b.f=a;return b.f},position:function(a,c){b.b=a||b.b;b.c=c||b.c;return{x:b.b,y:b.c}}}},hook:function(c){h.push(c)},unhook:function(c){h.i(c)!==-1&&h.splice(h.i(c),1)}}};
+var m=void 0,mibbu=function(K,L,B){function q(){for(var c=n.length,e,d,f,g,i,b,a,C,D;c--;)for(e in d=n[c],g=d.c+d.e.A,i=d.c+d.height-d.e.q,b=d.b+d.e.u,d=d.b+d.width-d.e.w,n[c].t)if(f=E[e],a=f.c+f.e.A,C=f.c+f.height-f.e.q,D=f.b+f.e.u,f=f.b+f.width-f.e.w,!(g>C||i<a||b>f||d<D))n[c].t[e]()}function F(){G();for(var c=e.length;c--;)e[c].G();for(c=h.length;c--;)h[c]();if(s){c=+new Date;~~(c-v)>=1E3&&(H=w,w=0,v=c);c="FPS: "+H;if(i)o.fillText(c,4,15);else if(t)t.innerHTML=c;w++}x&&(I=J(F,g))}function M(){g=l.createElement("canvas");o=g.getContext("2d");o.i=o.drawImage;e.sort(function(c,e){return c.j-e.j})}function N(){G=i?function(){o.clearRect(0,0,y,z)}:function(){};s&&(v=new Date);J=function(){return window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(c){setTimeout(c,1E3/60)}}();p=g.style;g.width=y;g.height=z;p.width=y+"px";p.height=z+"px";p.position="absolute";p.overflow="hidden";A.appendChild(g)}var i=!0,k=!1,l=document,e=[],A=B?l.getElementById(B):l.body,g,o,y=K||400,z=L||300,h=[],I,G,v=new Date,s=!1,t,n=[],E=[],J,p,u,j;Array.prototype.i=Array.prototype.indexOf||function(c){for(var e=this.length;e--&&this[e]!==c;);return e};var w=0,H=0,x=!0;l.createElement("canvas").getContext||(i=!1);return{fps:function(){s=!0;return this},init:function(){i?M():(g=l.createElement("div"),s&&(t=l.createElement("div"),g.appendChild(t)));N();return this},on:function(){x=!0;F();if(k)for(var c=e.length;c--;)e[c].a&&(e[c].a.style[j+"AnimationDuration"]=~~(1/(60/e[c].f)*100)/100*(e[c].h+1)+"s");return this},off:function(){clearTimeout(I);x=!1;if(k)for(var c=e.length;c--;)e[c].a&&(e[c].a.style[j+"AnimationDuration"]=0);return this},canvas:function(){return g},ctx:function(){return o},canvasOff:function(){i=!1;typeof A.style.WebkitAnimation!=="undefined"?(u="-webkit-",j="Webkit",k=!0):typeof A.style.MozAnimation!=="undefined"&&(u="-moz-",j="Moz",k=!0);return this},cssAnimationOff:function(){k=!1;return this},hitsOn:function(){h.i(q)===-1&&h.push(q);return this},hitsOff:function(){h.i(q)!==-1&&h.splice(h.i(q),1);return this},spr:function(c,h,d,f,p){function r(){for(var b="@"+u+"keyframes s"+a.id+" {\n",c=100/(a.h+1),e="% { "+u+"transform: translate(",d=0;d<a.h+1;d++)b+=~~(c*d*100)/100+e+a.k*a.width*-1+"px,"+d*a.height*-1+"px); }\n",b+=~~((c*(d+1)-0.01)*100)/100+e+a.k*a.width*-1+"px,"+d*a.height*-1+"px); }\n";return b+("100"+e+a.k*a.width+"px, 0px); }\n}")}var b=i?function(){try{o.i(a.a,a.F*a.k,a.D*a.n,a.F,a.D,a.b,a.c,a.width,a.height)}catch(b){}}:k?function(){}:function(){a.d.top=a.height*a.n*-1+"px";a.d.left=a.width*a.k*-1+"px"},a={};a.id=e.length;a.a=new Image;a.a.src=c;a.f=1;a.width=h;a.F=h;a.height=d;a.D=d;a.h=f;a.H=p;a.I=!1;a.t={};a.n=0;a.k=0;a.f=1;a.p=0;a.b=0;a.c=0;a.j=1;a.s=null;a.r=0;a.B=0;a.e={A:0,u:0,q:0,w:0};if(!i){a.C=l.createElement("div");a.g=a.C.style;a.g.overflow="hidden";a.g.width=h+"px";a.g.height=d+"px";a.g.position="absolute";a.g.zIndex=a.j;a.d=a.a.style;a.d.position="absolute";if(k)a.o=l.createElement("style"),a.o.innerHTML=r(),l.body.appendChild(a.o),a.d[j+"Animation"]="s"+a.id+" "+~~(1/(60/a.f)*100)/100*(a.h+1)+"s linear 0s infinite";a.C.appendChild(a.a);g.appendChild(a.C)}a.id=e.push(a)-1;E.push(a);a.G=function(){if(a.h>0){if(a.p==a.f&&a.f!==0){if(a.n==a.h){if(a.n=0,typeof a.s==="function"&&(a.r++,a.r===a.B))a.s(),a.r=0}else a.n++;a.p=0}a.f!==0&&a.p++;b()}};return{position:function(b,c,d){return b!==m?(a.b=b||a.b,a.c=c||a.c,a.j=d||a.j,i?d&&e.sort(function(a,b){return b.j-a.j}):(a.g.left=b+"px",a.g.top=c+"px",a.g.zIndex=d||a.j),this):{x:a.b,y:a.c,J:a.j}},hit:function(b,c){n.i(a)===-1&&n.push(a);a.t[b.id()]=c;n.i(a)===-1&&n.push(a);return this},zone:function(b,c,d,e){return b!==m?(a.e.u=e,a.e.A=b,a.e.w=c,a.e.q=d,this):a.e},noHits:function(){a.t={};return this},callback:function(b,c){a.s=b;a.B=c;return this},change:function(b,c,d,e,f){a.a.src=b;a.width=c;a.height=d;a.F=c;a.D=d;a.h=e;a.k=f;a.p=0;a.n=0;a.s=null;a.r=0;a.B=0;if(!i&&(a.d.width=c*(a.k+1)+"px",a.d.height=d*(a.h+1)+"px",a.g.width=c+"px",a.g.height=d+"px",k))a.d[j+"AnimationName"]="",a.o.innerHTML=r(),a.d[j+"AnimationName"]="s"+a.id;a.e={A:0,u:0,q:0,w:0};return this},size:function(b,c){if(b!==m){if(!i)a.g.width=b+"px",a.g.height=c+"px",a.d.width=b*(a.H+1)+"px",a.d.height=c*(a.h+1)+"px";a.width=b;a.height=c;if(k)a.d[j+"AnimationName"]="",a.o.innerHTML=r(),a.d[j+"AnimationName"]="s"+a.id;return this}else return{width:a.width,height:a.height}},speed:function(b){return b!==m?(a.f=b,a.p=0,k&&(a.d[j+"AnimationDuration"]=~~(1/(60/b)*100)/100*(a.h+1)+"s"),this):a.f},animation:function(b){if(b!==m){a.k=b;if(k)a.d[j+"AnimationName"]="",a.o.innerHTML=r(),a.d[j+"AnimationName"]="s"+a.id;return this}else return a.k},frame:function(b){return b!==m?(a.n=b,this):a.n},id:function(){return a.id}}},bg:function(c,h,d,f){function j(c){b.l=0;b.m=0;if(typeof c==="string")switch(c){case "N":b.l=0;b.m=-1;break;case "W":b.l=-1;b.m=0;break;case "S":b.l=0;b.m=1;break;case "E":b.l=1,b.m=0}else if(typeof c==="number")c*=a,b.l=Math.cos(c),b.m=Math.sin(c)}var k=i?function(){try{for(var a=b.c%b.a.height,c=b.b%b.a.width-b.a.width;c<g.width;c+=b.a.width)for(var d=a-b.a.height;d<g.height;d+=b.a.height)o.i(b.a,c,d)}catch(e){}if(b.l<0){if(b.b<b.a.width*-1)b.b=0}else if(b.l>0&&b.b>b.a.width)b.b=0;if(b.m<0){if(b.c<b.a.height*-1)b.c=0}else if(b.m>0&&b.c>b.a.height)b.c=0}:function(){var a=b.b,c=b.c;a.toString().indexOf("e")!=-1&&(a=0);c.toString().indexOf("e")!=-1&&(c=0);g.style.backgroundPosition=a+"px "+c+"px"},b=this;i?(b.a=new Image,b.a.src=c):g.style.backgroundImage="url("+c+")";b.f=h||3;var a=Math.PI/180;j(d);b.j=f.z||0;b.b=f.x||0;b.c=f.y||0;b.id=e.push(b);b.v=0;b.G=function(){b.b+=b.f*b.l*b.v;b.c+=b.f*b.m*b.v;k()};return{on:function(){b.v=1;return this},off:function(){b.v=0;return this},dir:function(a){j(a);return this},speed:function(a){return a!==m?(b.f=a,this):b.f},position:function(a,c){return a!==m?(b.b=a||b.b,b.c=c||b.c,this):{x:b.b,y:b.c}}}},hook:function(c){h.push(c);return this},unhook:function(c){h.i(c)!==-1&&h.splice(h.i(c),1);return this}}};
View
162 mibbu.js
@@ -72,17 +72,17 @@ var mibbu = function(Cwidth, Cheight, _parent){
function(value){
for (var i = this.length; i-- && this[i]!== value;) {}
return i;
- }
+ };
//and custom remove() method
var rm = function(value, array) {
if (array.i(value)!==-1) {
- array.splice(array.i(value), 1);
- return true;
- } else {
- return false;
- };
- }
+ array.splice(array.i(value), 1);
+ return true;
+ } else {
+ return false;
+ }
+ } ;
/**
@@ -180,7 +180,7 @@ var mibbu = function(Cwidth, Cheight, _parent){
MB_mainCanvas.height = MB_mainCanvasHeight;
MB_mainCanvasStyle.width = MB_mainCanvasWidth+'px';
MB_mainCanvasStyle.height =MB_mainCanvasHeight+'px';
- MB_mainCanvasStyle.position ='absolute'
+ MB_mainCanvasStyle.position ='absolute';
MB_mainCanvasStyle.overflow = 'hidden';
MB_parentElement.appendChild(MB_mainCanvas);
@@ -404,25 +404,33 @@ var mibbu = function(Cwidth, Cheight, _parent){
MB_fixedIndexColl.push(t); //for collisions, temporary
var setPosition = function(x, y, z) {
- t.posX = x || t.posX;
- t.posY = y || t.posY;
- t.zOrder = z || t.zOrder;
-
- if (MB_usingCanvas) {
- if (z) {
- MB_elements.sort(function(a, b){
- //return a.zOrder - b.zOrder;}
- return b.zOrder - a.zOrder;
- } //reversed becaouse of 'while' loop in DrawAll();
- );
-
- }
+ //there is at least one argument,
+ //set position and return 'this' for chaining
+ if (x !== undefined) {
+ t.posX = x || t.posX;
+ t.posY = y || t.posY;
+ t.zOrder = z || t.zOrder;
+
+ if (MB_usingCanvas) {
+ if (z) {
+ MB_elements.sort(function(a, b){
+
+ return b.zOrder - a.zOrder;
+ }
+ );
+
+ }
+ } else {
+ t.s.left = x+'px';
+ t.s.top = y+'px';
+ t.s.zIndex = z || t.zOrder;
+ }
+ return this;
} else {
- t.s.left = x+'px';
- t.s.top = y+'px';
- t.s.zIndex = z || t.zOrder;
- }
- return {x:t.posX, y:t.posY, z:t.zOrder}
+ //method called without parameters, return
+ //actual position
+ return {x:t.posX, y:t.posY, z:t.zOrder}
+ }
},
setCollide = function(e) {
@@ -439,6 +447,7 @@ var mibbu = function(Cwidth, Cheight, _parent){
if (MB_collides.i(t) === -1) {
MB_collides.push(t);
}
+ return this;
};
t.draw = function() {
@@ -472,6 +481,9 @@ var mibbu = function(Cwidth, Cheight, _parent){
}
};
var reSize = function(w, h){
+ //there are some arguments
+ //so change size of the sprite
+ //and return 'this' for chaining
if (w !== undefined) {
if (!MB_usingCanvas){
@@ -491,8 +503,13 @@ var mibbu = function(Cwidth, Cheight, _parent){
t.si[ MB_prefixJS+ "AnimationName" ] = 's'+t.id;
};
+
+ return this;
+
+ } else {
+
+ return {width:t.width,height:t.height};
}
- return {width:t.width,height:t.height};
};
return {
@@ -504,15 +521,20 @@ var mibbu = function(Cwidth, Cheight, _parent){
t.cZ.t = top;
t.cZ.r = right;
t.cZ.b = bottom;
+
+ return this;
+ } else {
+ return t.cZ;
}
- return t.cZ;
},
'noHits':function() {
t.hits = {};
+ return this;
},
'callback':function(fn, iteration) {
t.callback = fn;
t.callMaxIters = iteration;
+ return this;
},
'change': function(image, width, height, frames, animation) {
t.image.src = image;
@@ -547,6 +569,8 @@ var mibbu = function(Cwidth, Cheight, _parent){
b: 0,
r: 0
}
+
+ return this;
},
'size':reSize,
@@ -557,8 +581,11 @@ var mibbu = function(Cwidth, Cheight, _parent){
if (MB_usingCSSAnimations){
t.si[ MB_prefixJS+ "AnimationDuration" ] = calculateSpeed(e, t.fs)+'s';
}
- }
- return t.speed;
+
+ return this;
+ } else {
+ return t.speed;
+ }
},
'animation':function(e) {
if (e !== undefined) {
@@ -571,10 +598,20 @@ var mibbu = function(Cwidth, Cheight, _parent){
t.animStyle.innerHTML = constructAnimationClass();
t.si[ MB_prefixJS+ "AnimationName" ] = 's'+t.id;
}
- }
- return t.animation;
+
+ return this;
+ } else {
+ return t.animation;
+ }
+ },
+ 'frame':function(e) {
+ if (e !== undefined) {
+ t.f=e;
+ return this;
+ } else {
+ return t.f;
+ }
},
- 'frame':function(e) { if (e !== undefined) t.f=e; return t.f;},
'id': function() { return t.id; }
};
};
@@ -626,7 +663,7 @@ var mibbu = function(Cwidth, Cheight, _parent){
// which won't work on the DOM. There's probably a more efficient way to
// do this.
var posX = t.posX,
- posY = t.posY;
+ posY = t.posY;
if (posX.toString().indexOf('e') != -1) posX = 0;
if (posY.toString().indexOf('e') != -1) posY = 0;
@@ -686,10 +723,14 @@ var mibbu = function(Cwidth, Cheight, _parent){
t.moving = 0;
var setPosition = function(x, y) {
- t.posX = x || t.posX;
- t.posY = y || t.posY;
-
- return {x:t.posX, y:t.posY}
+ if (x !== undefined) {
+ t.posX = x || t.posX;
+ t.posY = y || t.posY;
+
+ return this;
+ } else {
+ return {x:t.posX, y:t.posY}
+ }
};
t.draw = function() {
@@ -700,10 +741,10 @@ var mibbu = function(Cwidth, Cheight, _parent){
}
return {
- 'on': function() { t.moving = 1; },
- 'off': function() { t.moving = 0; },
- 'dir': function(direction) { direcionFromParameter(direction); },
- 'speed':function(e) { if (e !== undefined) { t.speed=e; } return t.speed;},
+ 'on': function() { t.moving = 1; return this;},
+ 'off': function() { t.moving = 0; return this;},
+ 'dir': function(direction) { direcionFromParameter(direction); return this;},
+ 'speed':function(e) { if (e !== undefined) { t.speed=e; return this;} else return t.speed;},
'position':setPosition
}
@@ -718,8 +759,8 @@ var mibbu = function(Cwidth, Cheight, _parent){
return {
//config
- 'fps': function() {MB_fpsMeasure=true;},
- 'init': function() { MB_usingCanvas ? MB_InitCanvas() : MB_InitDOM(); MB_InitCore();},
+ 'fps': function() {MB_fpsMeasure=true; return this;},
+ 'init': function() { MB_usingCanvas ? MB_InitCanvas() : MB_InitDOM(); MB_InitCore(); return this;},
'on': function() {
running=true;
MB_Start();
@@ -732,6 +773,7 @@ var mibbu = function(Cwidth, Cheight, _parent){
MB_elements[i].image.style[ MB_prefixJS+ "AnimationDuration" ] = calculateSpeed(MB_elements[i].speed, MB_elements[i].fs)+'s';
}
}
+ return this;
},
'off': function(){
MB_Stop();
@@ -744,6 +786,7 @@ var mibbu = function(Cwidth, Cheight, _parent){
MB_elements[i].image.style[ MB_prefixJS+ "AnimationDuration" ] = 0;
}
}
+ return this;
},
'canvas': function(){ return MB_mainCanvas; },
'ctx': function() {return MB_mainContext; },
@@ -767,18 +810,39 @@ var mibbu = function(Cwidth, Cheight, _parent){
MB_prefixJS = "Moz";
MB_usingCSSAnimations = true;
}
+ return this;
+ },
+
+ 'cssAnimationOff': function() {
+ MB_usingCSSAnimations=false;
+ return this;
+ },
+
+ 'hitsOn': function() {
+ if (MB_addedLoops.i(MB_checkCollides) === -1)
+ MB_addedLoops.push(MB_checkCollides);
+ return this;
+ },
+
+ 'hitsOff': function() {
+ rm(MB_checkCollides, MB_addedLoops);
+ return this;
},
- 'cssAnimationOff': function() {MB_usingCSSAnimations=false;},
- 'hitsOn': function() { if(MB_addedLoops.i(MB_checkCollides) === -1) MB_addedLoops.push(MB_checkCollides); },
- 'hitsOff': function() { rm(MB_checkCollides, MB_addedLoops); },
//elements
'spr':MB_Sprite,
'bg': MB_Background,
//loops
- 'hook': function(e){MB_addedLoops.push(e);},
- 'unhook': function(e){rm(e, MB_addedLoops);}
+ 'hook': function(e){
+ MB_addedLoops.push(e);
+ return this;
+ },
+
+ 'unhook': function(e){
+ rm(e, MB_addedLoops);
+ return this;
+ }
};
};
Please sign in to comment.
Something went wrong with that request. Please try again.