phinajs / phina.js

phina.js is game library

Home Page:http://phinajs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LifeCounter を作る

phi-jp opened this issue · comments

commented
commented
    // デフォルト値
    var LIFECOUNTER_DEFAULT = {
        image: "lifecounter",
        maxLifeCount: 3,
    };

    tm.define("edu.pre.LifeCounter", {
        superClass: "tm.app.CanvasElement",

        init: function (param) {

            param = param || {};
            param.$safe(LIFECOUNTER_DEFAULT);

            var image = param.image;
            var maxLifeCount = param.maxLifeCount;

            this.superInit();

            // 現在のライフポイント
            this.currentLifeNum = maxLifeCount;

            this.lifes = [];
            var posList = this._calcCenterPosition(maxLifeCount, SCREEN_CENTER_X, 77, 80);
            posList.each(function(pos, i){
                var life = tm.display.Sprite(image)
                    .addChildTo(this)
                    .setPosition(pos.x, pos.y);
                life.tweener
                    .clear()
                    .setLoop(true)
                    .to({scaleX:1.05, scaleY:1.05},  60, "easeInOutSine")
                    .to({scaleX:1.0,  scaleY:1.0},  700, "easeInOutSine")
                    .wait(600);
                this.lifes[i] = life;
            }.bind(this));
        },

        damage: function () {
            var life = this.lifes[this.currentLifeNum-1];
            if (!life) {
                return ;
            }

            var _this = this;
            life.tweener
                .clear()
                .setLoop(false)
                .popOut()
                .call(function () {
                    if (_this.currentLifeNum <= 0) {
                        var e = tm.event.Event("empty");
                        _this.dispatchEvent(e);
                    }
                });
            // ライフを減らす
            --_this.currentLifeNum;
        },

        getLife: function () {
            return this.currentLifeNum;
        },

        setDefaultPosition: function() {
            // TODO:
            this.x = SCREEN_CENTER_X;
        },

        _calcCenterPosition: function(num, offsetX, offsetY, offsetPiece) {

            var result = [];
            var start = -(num/2)|0;
            var end = (num/2)|0;

            // 偶数
            if (num%2 == 0) {
                end -= 1;
                offsetX += offsetPiece/2;
            }

            for (var i=start; i<=end; ++i) {
                var x = i*offsetPiece + offsetX;
                var y = offsetY;

                result.push({
                    x: x,
                    y: y,
                });
            }

            return result;
        },

    });