Skip to content

Instantly share code, notes, and snippets.

@kohashi
Created June 6, 2012 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohashi/2882037 to your computer and use it in GitHub Desktop.
Save kohashi/2882037 to your computer and use it in GitHub Desktop.
enchant.jsをメソッドチェインで使うためのprototype拡張
/**
* enchant.js prototype extension
*
* Copyright (c) 2012 @hako584
* Dual licensed under the MIT or GPL Version 3 licenses
*/
/**
* enchantの中でHTML要素として画面に表示される要素に対して、
* $プロパティでjQueryオブジェクトを取り出せるようにする。
*/
Object.defineProperty(enchant.Entity.prototype,
'$',{ get : function () {return $(this._element);}}
);
Object.defineProperty(enchant.Scene.prototype,
'$',{ get : function () {return $(this._element);}}
);
Object.defineProperty(enchant.Surface.prototype,
'$',{ get : function () {return $(this._element);}}
);
/**
* 結構多そうな、オブジェクトを中央に寄せる処理。
* yプロパティの指定が無ければ画面中央、yだけ指定でx座標中央。
* y座標のみ中央は需要少ないと思うのでつけてない
*/
enchant.Node.prototype.setCenter = function (y) {
var x = (game.width - this.width)/2;
var y = y!==undefined ? y : (game.height - this.height)/2;
this.moveTo(x,y);
return this;
}
/**
* moveTo上書き
*/
enchant.Node.prototype.moveTo = function (x, y) {
this.x = x;
this.y = y;
return this;
}
/**
* moveBy上書き
*/
enchant.Node.prototype.moveBy = function(x, y) {
this.x += x;
this.y += y;
return this;
}
/**
* obj.addEventListener(enchant.Event.TOUCH_START, fn) を obj.click(fn) と書くためのショートハンド。
*/
enchant.EventTarget.prototype.click = function(fn){
this.addEventListener(enchant.Event.TOUCH_END, fn);
return this;
}
/**
* enchant.Label に fontSize追加
*/
Object.defineProperty(enchant.Label.prototype,
'fontSize', {
get: function () { return this._fontSize || ''; },
set: function (v) { this._element.style.fontSize = v + 'px'; this._fontSize = v; }
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment