Enjoy programming more!
Kidspod is a post site which supports young programmers.
First step to join us.
Register as a member
For members

2015/10/31 18:23:52

MixJuice GET URL : kidspod.club/mj/37

アイコン文字列

文章をリソースにある画像で表示します。
oyumaru.LabelPNG.create作ります。引数には文字列を入れてください。
使える文字は、
 A~Z(小文字は大文字になります)
 !
 ?
 スペース
です(これ以外の文字はスペースになります)。
setScale関数で拡大・縮小、setString関数で文章を書き直せます。

SOURCE CODE

var oyumaru = oyumaru || {};

oyumaru.LabelPNG = cc.Node.extend({
	// const
	CODE_EXCLAMATION_S: "!".charCodeAt(0),
 	CODE_EXCLAMATION_L: "!".charCodeAt(0),
	CODE_QUESTION_S: 	"?".charCodeAt(0),
 	CODE_QUESTION_L: 	"?".charCodeAt(0),
 	CODE_A: "A".charCodeAt(0),
	CODE_Z: "Z".charCodeAt(0),
	FILEID_A: 	 128,
	WIDTH_CHAR:  32,
	HEIGHT_CHAR: 32,

	// member
	_nLength: 	0,
	_fScale: 	1.0,

	init:function(text){
		this._super();
		this.setAnchorPoint(0, 0);
		this.setString(text);

		return true;
	},		

	setString:function(text){	
		this.removeAllChildren();

		var strText = String(text).toUpperCase();
		this._nLength = strText.length;
		
		for(var i = 0; i < this._nLength; i++)
		{		
			var sp = this._createSprite(strText.charAt(i));
			this.addChild(sp, i, i);
		}

		this._adjustSizeSelf();
		this._adjustSpritePosition();
	},

	setScale:function(fScale){
		if(fScale < 0) return;
		
		this._fScale = fScale;
		this._adjustSizeSelf();
		this._adjustSpritePosition();
	},

	_adjustSizeSelf:function(){
		this.setContentSize(this._nLength * this.WIDTH_CHAR * this._fScale,
							this.HEIGHT_CHAR * this._fScale);
	},

	_adjustSpritePosition:function(){
		var fCharWidth = this.WIDTH_CHAR * this._fScale;
		var children = this.getChildren();
		for(var i in children)
		{
			children[i].setScale(this._fScale, this._fScale);
			children[i].setPosition(fCharWidth/2 + fCharWidth * i,
									this.HEIGHT_CHAR * this._fScale/2);
		}
	},

	_createSprite:function(sChar){

		var id = "";
		var code = sChar.charCodeAt(0);
		if(code == this.CODE_EXCLAMATION_S || code == this.CODE_EXCLAMATION_L)
		{
			id = "a4";
		}
		else if(code == this.CODE_QUESTION_S || code == this.CODE_QUESTION_L)
		{
			id = "a5";
		}
		else if(this.CODE_A <= code && code <= this.CODE_Z)
		{
			var id_10 = code - this.CODE_A + this.FILEID_A;
			id = id_10.toString(16);
		}

		var spRet;
		if(id == "")
		{
			spRet = cc.Sprite.create("res/pancake/a3.png");
			spRet.setVisible(false);
		}
		else
		{
			spRet = cc.Sprite.create("res/pancake/" + id + ".png");
		}
		
		return spRet;
	},

});

oyumaru.LabelPNG.create = function(text){
	var pRet = new oyumaru.LabelPNG();
	return pRet.init(text) ? pRet : null;
};

COMMENT