123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- var cfg = require("Configure")
- cc.Class({
- extends: cc.Component,
- properties: {
- scoreSp: {
- default: null,
- type: cc.Sprite
- },
- budingTypeSp: {
- default: null,
- type: cc.Sprite
- },
- obstacleType: 0,
- colorType: 0,
- obstacleNum: 0,
- curColorType: 0,
- updateTime: 0,
- },
- // LIFE-CYCLE CALLBACKS:
- // onLoad () {},
- //初始化障碍物
- //type:障碍物类型(随机)
- initObstacle: function (type, obstacleNum) {
- this.obstacleNum = obstacleNum;
- if (obstacleNum == 1) {
- this.budingTypeSp.node.active = false;
- }
- this.obstacleType = type;
- switch (type) {
- case cfg.obstacleType.circle0:
- case cfg.obstacleType.square:
- this.initCircle0(type);
- break;
- case cfg.obstacleType.circle1:
- case cfg.obstacleType.circle2:
- case cfg.obstacleType.circle3:
- this.initCircle1(type);
- break;
- case cfg.obstacleType.translation:
- this.initTranslation(type);
- break;
- default:
- break;
- }
- },
- initCircle0: function (type) {
- var d = this.randDir();
- var t = this.randType();
- var dt = this.obstacleNum > 50 ? cfg.obstacleProperty2[type].rotTime : cfg.obstacleProperty[type].rotTime;
- var config = {
- dir: d,
- type: t,
- angle: cfg.obstacleProperty[type].rotAngle,
- time: dt
- }
- var circleMode = this.node.getChildByName("Circle").getComponent("CircleMode");
- circleMode.initProperties(config);
- },
- initCircle1: function (type) {
- var t = this.randType();
- var d = this.randDir();
- var dt = this.obstacleNum > 50 ? cfg.obstacleProperty2[type].rotTime : cfg.obstacleProperty[type].rotTime;
- var config1 = {
- dir: d,
- type: t,
- angle: cfg.obstacleProperty[type].rotAngle,
- time: dt,
- }
- var config2 = {
- dir: -d,
- type: t,
- angle: cfg.obstacleProperty[type].rotAngle,
- time: dt,
- }
- var circleMode1 = this.node.getChildByName("Circle1").getComponent("CircleMode");
- circleMode1.initProperties(config1);
- var circleMode2 = this.node.getChildByName("Circle2").getComponent("CircleMode");
- circleMode2.initProperties(type == cfg.obstacleType.circle1 ? config1 : config2);
- },
- initTranslation: function (type) {
- var d = this.randDir();
- var dt = this.obstacleNum > 50 ? cfg.obstacleProperty2[type].moveTime : cfg.obstacleProperty[type].moveTime;
- var config1 = {
- dir: d,
- distance: cfg.obstacleProperty[type].moveDistance,
- time: dt,
- }
- var config2 = {
- dir: -d,
- distance: cfg.obstacleProperty[type].moveDistance,
- time: dt,
- }
- for (let i = 0; i < 4; i++) {
- var transMode = this.node.getChildByName("Translation" + i).getComponent("TranslationMode");
- transMode.initProperties((i == 0 || i == 1) ? config1 : config2);
- }
- },
- randDir: function () {
- return Math.round(Math.random()) == 0 ? 1 : -1;
- },
- randType: function () {
- if (this.obstacleNum >= 10) {
- return Math.round(Math.random());
- } else {
- return 0;
- }
- },
- pauseObstacleAction: function () {
- switch (this.obstacleType) {
- case cfg.obstacleType.circle0:
- case cfg.obstacleType.square:
- this.node.getChildByName("Circle").getComponent("CircleMode").pauseAction();
- break;
- case cfg.obstacleType.circle1:
- case cfg.obstacleType.circle2:
- case cfg.obstacleType.circle3:
- this.node.getChildByName("Circle1").getComponent("CircleMode").pauseAction();
- this.node.getChildByName("Circle2").getComponent("CircleMode").pauseAction();
- break;
- case cfg.obstacleType.translation:
- this.node.getChildByName("Translation0").getComponent("TranslationMode").pauseAction();
- this.node.getChildByName("Translation1").getComponent("TranslationMode").pauseAction();
- this.node.getChildByName("Translation2").getComponent("TranslationMode").pauseAction();
- this.node.getChildByName("Translation3").getComponent("TranslationMode").pauseAction();
- break;
- default:
- break;
- }
- },
- stopObstacleAction: function () {
- switch (this.obstacleType) {
- case cfg.obstacleType.circle0:
- case cfg.obstacleType.square:
- this.node.getChildByName("Circle").stopAllActions();
- break;
- case cfg.obstacleType.circle1:
- case cfg.obstacleType.circle2:
- case cfg.obstacleType.circle3:
- this.node.getChildByName("Circle1").stopAllActions();
- this.node.getChildByName("Circle2").stopAllActions();
- break;
- case cfg.obstacleType.translation:
- this.node.getChildByName("Translation0").stopAllActions();
- this.node.getChildByName("Translation1").stopAllActions();
- this.node.getChildByName("Translation2").stopAllActions();
- this.node.getChildByName("Translation3").stopAllActions();
- break;
- default:
- break;
- }
- },
- resumeObstacleAction: function () {
- switch (this.obstacleType) {
- case cfg.obstacleType.circle0:
- case cfg.obstacleType.square:
- this.node.getChildByName("Circle").getComponent("CircleMode").resumeAction();
- break;
- case cfg.obstacleType.circle1:
- case cfg.obstacleType.circle2:
- case cfg.obstacleType.circle3:
- this.node.getChildByName("Circle1").getComponent("CircleMode").resumeAction();
- this.node.getChildByName("Circle2").getComponent("CircleMode").resumeAction();
- break;
- case cfg.obstacleType.translation:
- this.node.getChildByName("Translation0").getComponent("TranslationMode").resumeAction();
- this.node.getChildByName("Translation1").getComponent("TranslationMode").resumeAction();
- this.node.getChildByName("Translation2").getComponent("TranslationMode").resumeAction();
- this.node.getChildByName("Translation3").getComponent("TranslationMode").resumeAction();
- break;
- default:
- break;
- }
- },
- getType: function () {
- return this.obstacleType;
- },
- getColorType: function () {
- return this.curColorType;
- },
- moveObstacle: function (movePosY) {
- this.node.runAction(cc.moveBy(0.5, cc.p(0, movePosY)));
- },
- outScreen: function () {
- switch (this.obstacleType) {
- case cfg.obstacleType.circle0:
- case cfg.obstacleType.circle1:
- case cfg.obstacleType.translation:
- return this.node.y < -1000;
- break;
- case cfg.obstacleType.circle2:
- return this.node.y < -1100;
- break;
- case cfg.obstacleType.circle3:
- return this.node.y < -1720;
- break;
- case cfg.obstacleType.square:
- return this.node.y < -920;
- break;
- default:
- return false;
- break;
- }
- },
- start() {
- },
- update(dt) {
- this.updateTime += dt;
- //每隔1秒变1次色
- if (this.updateTime >= 1) {
- //变色块变色
- this.colorType++;
- if (this.colorType > 3) {
- this.colorType = 0;
- }
- if (this.obstacleType == cfg.obstacleType.circle3 || this.obstacleType == cfg.obstacleType.circle2) {
- if (this.colorType == 1 || this.colorType == 3) {
- this.colorType = Math.floor(Math.random() * 2) == 0 ? 0 : 2;
- }
- }
- var colorType = this.colorType;
- var self = this;
- cc.loader.loadRes(cfg.ColorTypePath[colorType], cc.SpriteFrame, function (err, spriteFrame) {
- self.budingTypeSp.spriteFrame = spriteFrame;
- self.curColorType = colorType;
- })
- this.updateTime = 0;
- }
- // if (this.node.active && this.outScreen())
- // {
- // this.node.active = false;
- // this.node.parent.getComponent("GameSence").destroyObstacle();
- // }
- }
- });
|