"jsList" : [ "src/resource.js", "src/biscuit.js", "src/score.js", "src/game.js" ]
var ScoreLayer = cc.Layer.extend({ score: 0, labelScore: null, ctor: function() { /* */ this._super(); /* */ this.init(); }, init: function() { /* */ this.labelScore = cc.LabelTTF.create("Score: 0"); this.labelScore.setColor(cc.color(255, 255, 255, 255)); this.labelScore.setPosition(cc.p(240, 700)); this.addChild(this.labelScore); }, scoreIncrease: function() { /* */ this.score += 1; this.labelScore.setString("Score: " + this.score); } });
var GameScene = cc.Scene.extend({ snake_layer: {}, score_layer: {}, onEnter:function () { this._super(); // , this.score_layer = new ScoreLayer(); this.snake_layer = new SnakeLayer(); // , this.addChild(this.score_layer, 1); this.addChild(this.snake_layer, 0); } });
checkCollision: function() { ... /* */ if (head.x == this.biscuit.x && head.y == this.biscuit.y) { /* */ this.parent.score_layer.scoreIncrease(); /* */ this.updateBiscuit(); /* */ this.addPart(); } ... },
"jsList" : [ "src/resource.js", "src/biscuit.js", "src/score.js", "src/menu.js", "src/game.js" ]
var MenuScene = cc.Scene.extend({ titleSprite: {}, subTitleSprite: {}, onEnter: function() { this._super(); /* */ var winSize = cc.view.getDesignResolutionSize(); /* */ this.titleSprite = cc.LabelTTF.create("Snake.", "Arial", 50); this.titleSprite.x = winSize.width / 2; this.titleSprite.y = winSize.height / 2; this.addChild(this.titleSprite); /* */ this.subTitleSprite = cc.LabelTTF.create("Click anywhere to play", "Arial", 20); this.subTitleSprite.x = winSize.width / 2; this.subTitleSprite.y = winSize.height / 2 - 80; this.addChild(this.subTitleSprite); /* */ cc.eventManager.addListener({ event: cc.EventListener.TOUCH_ONE_BY_ONE, onTouchBegan: function (touches, event) { cc.director.runScene(new GameScene()); }, }, this); }, });
cc.director.runScene(new GameScene());
cc.director.runScene(new MenuScene());
"jsList" : [ "src/resource.js", "src/biscuit.js", "src/score.js", "src/gameOver.js", "src/menu.js", "src/game.js" ]
var GameOverScene = cc.Scene.extend({ finalScore: 0, labelGameOver: {}, labelScore: {}, labelPrompt: {}, ctor: function(score) { this._super(); this.finalScore = score; /* , */ cc.eventManager.addListener({ event: cc.EventListener.TOUCH_ONE_BY_ONE, onTouchBegan: function(touch, event) { cc.director.runScene(new MenuScene()); } }, this); }, onEnter: function() { this._super(); /* */ var winSize = cc.view.getDesignResolutionSize(); /* GameOver */ this.labelGameOver = new cc.LabelTTF("Game Over", "Arial", 60); this.labelGameOver.x = winSize.width * 0.50; this.labelGameOver.y = winSize.height * 0.50; this.addChild(this.labelGameOver); /* , */ this.labelScore = new cc.LabelTTF("Score: " + this.finalScore, "Arial", 30); this.labelScore.x = winSize.width * 0.50; this.labelScore.y = winSize.height * 0.43; this.addChild(this.labelScore); /* */ this.labelPrompt = new cc.LabelTTF("Click or Tap To Try Again"); this.labelPrompt.x = winSize.width * 0.50; this.labelPrompt.y = winSize.height * 0.39; this.addChild(this.labelPrompt); }, });
var SnakeLayer = cc.Layer.extend({ ... checkCollision: function() { ... /* */ for (var part = 1; part < body.length; part++) { if (head.x == body[part].x && head.y == body[part].y) { /* GameOver */ //this.snakeParts = null; // cc.director.runScene( new GameOverScene (this.parent.score_layer.score) ); } } ... }, ... });
updateDifficulty: function(curScore) { var scoreToDiff = {}; /* , */ scoreToDiff[0] = 0.15; scoreToDiff[5] = 0.10; scoreToDiff[15] = 0.05; scoreToDiff[25] = 0.03; scoreToDiff[35] = 0.01; /* */ if (scoreToDiff[curScore] !== undefined) { this.interval = scoreToDiff[curScore]; } },
var SnakeLayer = cc.Layer.extend({ ... ctor: function () { ... // /* */ this.updateDifficulty(); }, ... checkCollision: function() { /* */ if (head.x == this.biscuit.x && head.y == this.biscuit.y) { /* */ this.parent.score_layer.scoreIncrease(); /* */ this.updateBiscuit(); /* */ this.addPart(); // /* , */ this.updateDifficulty(this.parent.score_layer.score); } }, ... });
Source: https://habr.com/ru/post/281981/
All Articles