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/03 16:03:47

MixJuice GET URL : kidspod.club/mj/30

ScoreCounter

マイアプリで使えるScoreCounterです。
得点表示なんかにどうぞ。
使うときはresに必要な画像ファイルを書いておいてください。

oyumaru.scoreCounter.create(num)でnum桁のscoreCounerをcreateできます。
setScore(num)で、引数と同じ数になり、
plusScore(num)で引数分の数字が増えて、
minusScore(num)で引数分の数字が減ります。
数字がcreateで指定した桁数を超えると、ビックリマークが付きます。

URLから飛べるところでは、キーボードの上下で数字が増えたり減ったりします。

SOURCE CODE

var res = {
	scoreCounter_ex:"res/pancake/a4.png",
	scoreCounter_0:"res/pancake/a6.png",
	scoreCounter_1:"res/pancake/a7.png",
	scoreCounter_2:"res/pancake/a8.png",
	scoreCounter_3:"res/pancake/a9.png",
	scoreCounter_4:"res/pancake/aa.png",
	scoreCounter_5:"res/pancake/ab.png",
	scoreCounter_6:"res/pancake/ac.png",
	scoreCounter_7:"res/pancake/ad.png",
	scoreCounter_8:"res/pancake/ae.png",
	scoreCounter_9:"res/pancake/af.png",
};

var oyumaru = oyumaru || {};

oyumaru.scoreCounter = cc.Layer.extend({
	_nOrder:0,
	_nScore:0,
	
	_arrResPath:[
		"res/pancake/a6.png",
		"res/pancake/a7.png",
		"res/pancake/a8.png",
		"res/pancake/a9.png",
		"res/pancake/aa.png",
		"res/pancake/ab.png",
		"res/pancake/ac.png",
		"res/pancake/ad.png",
		"res/pancake/ae.png",
		"res/pancake/af.png",
	],

	init:function(nOrder){
		this._super();
		this._nOrder = nOrder;

		var spDummy = cc.Sprite.create(this._arrResPath[0]);
		var spWidth = spDummy.getContentSize().width;
		var spHeight = spDummy.getContentSize().height;
		this.setContentSize(spWidth * this._nOrder, spHeight);
		this.setAnchorPoint(0.0, 0.0);

		for(var i = 0; i < this._nOrder; i++)
		{
			var sp = cc.Sprite.create(this._arrResPath[0]);
			sp.setPosition(this.getContentSize().width - spWidth/2 - spWidth * i,
							spHeight/2);
			this.addChild(sp, i, i);
		}

		return true;
	},

	setScore:function(nScore) {
		this._updateScore(nScore);
	},

	plusScore:function(nPlus) {
		
		this._updateScore(this._nScore + nPlus);
	},

	minusScore:function(nMinus) {
		
		this._updateScore(this._nScore - nMinus);
	},

	_updateScore:function(nScore) {
		if(nScore <= 0) nScore = 0;

		if(nScore > Math.pow(10,this._nOrder)-1)
		{
			this._safeAddExclamation();
		}
		else
		{
			this._nScore = nScore;
			this._safeRemoveExclamtion();
		}	

		for(var i = 0;  i < this._nOrder; i++)
		{
			var num = Math.floor(this._nScore / Math.pow(10, i));
			num = num % 10;

			var sp = this.getChildByTag(i);
			sp.initWithFile(this._arrResPath[num]);
		}
	},

	_safeAddExclamation:function() {
		var spExclamation = this.getChildByTag(this._nOrder);
		if(!spExclamation)
		{
			spExclamation = cc.Sprite.create("res/pancake/a4.png");
			spExclamation.setPosition(-spExclamation.getContentSize().width/2, 
									  spExclamation.getContentSize().height/2);
			this.addChild(spExclamation, this._nOrder, this._nOrder);
		}
	},

	_safeRemoveExclamtion:function() {
		var spExclamation = this.getChildByTag(this._nOrder);
		if(spExclamation)
		{
			spExclamation.removeFromParent(true);
		}
	},


});


oyumaru.scoreCounter.create = function(nOrder) {
	var pRet = new oyumaru.scoreCounter();
	if(pRet.init(nOrder)) {
		return pRet;
	}

	return null;
};

COMMENT