カウントダウンもカウントアップもできるタイマーを作りました。
oyumaru.timer.createの引数に、表示する桁数と最初の数とカウントアップ/ダウン(true/false)を指定します。
startTimer関数でスタート、stopTimer関数で一時停止、restartTimer関数で最初からやり直し。
getRapTime関数で時間が返ってきます。isTimerRunning関数で、タイマーが動いているかどうかが返ってきます。
カウントが終わると勝手に止まります。
resに何も書かなくても動くよ!

var oyumaru = oyumaru || {};
oyumaru.timer = cc.Layer.extend({
_nOrder:0,
_fTime:0.0,
_fInitTime:0.0,
_bAsc:true,
_bCounting:false,
_nTagDecimal:100,
_sizeNumImg:cc.p(32,32),
_sDotImgPath:"res/pancake/9d.png",
_arrNumImgPath:[
"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, fInitTime, bAsc){
this._super();
if(nOrder <= 0) return false;
if(fInitTime < 0) return false;
if(fInitTime >= Math.pow(10, nOrder)) return false;
this._nOrder = nOrder;
this._fInitTime = fInitTime;
this._fTime = this._fInitTime;
this._bAsc = bAsc;
var spWidth = this._sizeNumImg.x;
var spHeight = this._sizeNumImg.y;
this.setContentSize(spWidth * (this._nOrder 1.5), spHeight);
this.setAnchorPoint(0.0, 0.0);
var spDecimal = cc.Sprite.create(this._arrNumImgPath[0]);
spDecimal.setPosition(this.getContentSize().width - spWidth/2, spHeight/2);
this.addChild(spDecimal, this._nTagDecimal, this._nTagDecimal);
var spDot = cc.Sprite.create(this._sDotImgPath);
spDot.setPosition(spDecimal.getPosition().x - spWidth/2, spDecimal.getPosition().y);
this.addChild(spDot);
for(var i = 0; i < this._nOrder; i )
{
var sp = cc.Sprite.create(this._arrNumImgPath[0]);
sp.setPosition(spDot.getPosition().x - spWidth * (i 1), spDot.getPosition().y);
this.addChild(sp, i, i);
}
return true;
},
startTimer:function() {
if(!this._bCounting)
{
this.schedule(this._updateTime);
this._bCounting = true;
}
},
restartTimer:function() {
this.stopTimer();
this.resetTimer();
this.startTimer();
},
stopTimer:function() {
if(this._bCounting)
{
this.unschedule(this._updateTime);
this._bCounting = false;
}
},
getRapTime:function() {
return Math.floor(this._fTime * 100) / 100;
},
isTimerRunning:function() {
return this._bCounting;
},
resetTimer:function() {
this._fTime = this._fInitTime;
},
_updateTime:function(dt) {
if(this._bAsc)
{
this._fTime = dt;
var fLim = Math.pow(10, this._nOrder);
if(this._fTime > fLim)
{
this._fTime = fLim - 0.1;
this.stopTimer();
}
}
else
{
this._fTime -= dt;
if(this._fTime <= 0.0)
{
this._fTime = 0.0;
this.stopTimer();
}
}
var nDecimal = Math.round(this._fTime * 10) % 10;
var spDecimal = this.getChildByTag(this._nTagDecimal);
spDecimal.initWithFile(this._arrNumImgPath[nDecimal]);
for(var i = 0; i < this._nOrder; i )
{
var num = (Math.floor(this._fTime / Math.pow(10, i))) % 10;
this.getChildByTag(i).initWithFile(this._arrNumImgPath[num]);
}
},
});
oyumaru.timer.create = function(nOrder, fInitTime, bAsc) {
var pRet = new oyumaru.timer();
if(pRet.init(nOrder, fInitTime, bAsc)) {
return pRet;
}
return null;
};