var cfg = require("Configure") //布丁状态 var State = cc.Enum({ None: 0, Jump: 1, Drop: 2, Dead: 3 }) cc.Class({ extends: cc.Component, properties: { gravity: 0, initJumpSpeed: 0, jumpAudio: { default: null, type: cc.AudioClip }, deadAudio: { default: null, type: cc.AudioClip }, shield: { default: null, type: cc.Node }, shieldInvincible: false, skSprite: { default: null, type: cc.Node }, //布丁状态 _state: { default: State.None, type: State, visible: false }, state: { get: function () { return this._state; }, set: function (value) { if (value !== this._state) { this._state = value; //var animName = State[this._state]; //this.anim.stop(); //this.anim.play(animName); if (value != State.Dead) { this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][value], value == State.None); } else { cc.audioEngine.playEffect(this.deadAudio); this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][value], false); } } }, type: State, }, colorType: 0, groundY: 0, startGroundY: 0 }, // LIFE-CYCLE CALLBACKS: onLoad: function () { this.groundY = (-cc.winSize.height / 2 - 25) * (cc.winSize.width / 750); this.node.parent.getChildByName("Ground").setPosition(0, this.groundY); this.node.parent.getChildByName("StartGround").setPosition(0, -cc.winSize.height / 2); this.startGroundY = this.node.parent.getChildByName("StartGround").y; }, initBuding: function () { this.colorType = Math.floor(Math.random() * 4); this.node.setPosition(cc.p(0, -200)); this.skSprite.setPosition(cc.p(0, -200)); this.node.parent.getChildByName("Ground").setPosition(0, this.groundY); var skintype = UserInfo.getSkinIndex(); var self = this; cc.loader.loadRes(cfg.buDingTypePath[skintype][self.colorType], cc.SpriteFrame, function (err, spriteFrame) { self.node.getComponent("cc.Sprite").spriteFrame = spriteFrame; self.node.active = true; self.currentSpeed = 0; self.state = State.Drop; self.skSprite.active = true; self.skSprite.getComponent("sp.Skeleton").setSkin("p" + (skintype + 1)); self.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[self.colorType][self.state], true); }) }, setSkSpriteVisible: function (bVisible) { this.skSprite.active = bVisible; }, stopBuding: function () { this.state = State.None; this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][this.state], true); }, resumeBuding: function () { this.state = State.Drop; this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][this.state], true); }, setColorType: function (colorType) { this.colorType = colorType; }, jump: function () { if (this.state != State.Dead) { this.state = State.Jump; this.currentSpeed = this.initJumpSpeed; //-- 播放跳音效 cc.audioEngine.playEffect(this.jumpAudio); } //this.spawnDust('DustUp'); }, changeColor: function (colorType) { this.colorType = colorType; var self = this; var skintype = UserInfo.getSkinIndex(); cc.loader.loadRes(cfg.buDingTypePath[skintype][colorType], cc.SpriteFrame, function (err, spriteFrame) { self.node.getComponent("cc.Sprite").spriteFrame = spriteFrame; self.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[colorType][self.state], true); }) }, //碰撞检测 onCollisionEnter: function (other, self) { console.log('onCollisionEnter'); if (other.tag < 4) { if (this.colorType == other.tag) { cc.log("穿过去了"); } else { cc.log("撞上了"); if (this.shield.active || this.shieldInvincible) { console.log('护盾无敌时间') this.removeShield(); return; } if (this.state != State.Dead) { this.state = State.Dead; this.scheduleOnce(this.scheduleCallback, 1); } } } else if (other.tag == 5) { cc.log("吃到星星"); other.node.active = false; other.node.parent.getChildByName("ScoreSkSpine").active = true; other.node.parent.getChildByName("ScoreSkSpine").getComponent("sp.Skeleton").setAnimation(0, "a1", false); this.node.parent.getComponent("GameSence").addScore(); } else if (other.tag == 4) { cc.log("吃到变色块"); other.node.active = false; var colorType = other.node.parent.getComponent("Obstacle").getColorType(); this.changeColor(colorType); this.node.parent.getComponent("GameSence").addLayerNum(); this.node.parent.getComponent("GameSence").addScore(); } else if (other.tag == 6) { cc.log(this.node.parent.getComponent("GameSence")) cc.log("掉到屏幕外 %d", this.node.parent.getChildByName("Ground").y); if (this.node.parent.getChildByName("Ground").y > this.groundY) { if (this.shield.active) { this.removeShield(); return } if (this.state != State.Dead) { this.state = State.Dead; this.scheduleOnce(this.scheduleCallback, 1); } } } }, removeShield() { this.shield.active = false; this.shieldInvincible = true; this.scheduleOnce(() => { console.log('复原无敌时间') this.shieldInvincible = false; }, 1) }, scheduleCallback: function () { this.node.parent.getComponent("GameSence").finishGame(); this.state = State.None; }, update: function (dt) { switch (this.state) { case State.Jump: if (this.currentSpeed < 0) { this.state = State.Drop; } break; case State.Drop: if (this.node.y < this.startGroundY + 140) { this.node.y = this.startGroundY + 140; this.shield.y = this.node.y + 5; this.skSprite.setPosition(cc.p(0, this.node.y)); this.state = State.None; //this.scheduleOnce(this.scheduleCallback,1); } break; case State.Dead: case State.None: return; } var flying = this.state === State.Jump || this.skSprite.y > this.startGroundY + 50; if (flying) { this.currentSpeed -= dt * this.gravity; this.node.y += dt * this.currentSpeed; this.shield.y = this.node.y + 5; this.skSprite.setPosition(cc.p(0, this.node.y)); //地面位置 if (this.skSprite.y > 0 && this.state === State.Jump) { this.node.parent.getChildByName("Ground").setPosition(0, this.skSprite.y + this.groundY); } } }, });