snakeHead
variable with snakeParts
.snakeHead: null
change to this: snakeParts: null, //
ctor
method ctor
one below: ctor: function () { /* */ var winSize = cc.view.getDesignResolutionSize(); /* */ this._super(); // /* snakeParts */ this.snakeParts = []; /* */ var snakeHead = new SnakePart(asset.SnakeHead_png); /* */ snakeHead.x = winSize.width / 2; snakeHead.y = winSize.height / 2; /* snakeParts */ this.addChild(snakeHead); // this.snakeParts.push(snakeHead); /* */ this.scheduleUpdate(); },
SnakeParts
code as shown below: var SnakePart = cc.Sprite.extend({ // prevX: this.x, prevY: this.y, ctor: function(sprite) { /* , */ this._super(sprite); }, move: function(posX, posY) { // /* */ this.prevX = this.x; this.prevY = this.y; /* */ this.x = posX; this.y = posY; }, });
SnakeLayer
code: addPart: function() { var newPart = new SnakePart(asset.SnakeBody_png), size = this.snakeParts.length, tail = this.snakeParts[size - 1]; /* */ newPart.x = tail.x; newPart.y = tail.y; /* */ this.addChild(newPart); this.snakeParts.push(newPart); },
SnakePart
object, before moving, will appear in the lower left corner of the screen. We solve this problem by placing a new sprite in the tail of the snake.moveSnake
method in moveSnake
code to use the new member of the snakeParts
class and use it to move all parts of the snake: moveSnake: function(dir) { /* , */ var up = 1, down = -1, left = -2, right = 2, step = 20; /* snakeHead */ var snakeHead = this.snakeParts[0]; /* */ var dirMap = {}; dirMap[up] = function() {snakeHead.move(snakeHead.x, snakeHead.y + step);}; dirMap[down] = function() {snakeHead.move(snakeHead.x, snakeHead.y - step);}; dirMap[left] = function() {snakeHead.move(snakeHead.x - step, snakeHead.y);}; dirMap[right] = function() {snakeHead.move(snakeHead.x + step, snakeHead.y);}; /* */ if (dirMap[dir] !== undefined) { dirMap[dir](); } // /* */ var prevX = snakeHead.prevX; var prevY = snakeHead.prevY; /* */ for (var part = 1; part < this.snakeParts.length; part++) { var curPart = this.snakeParts[part]; /* , */ curPart.move(prevX, prevY); prevX = curPart.prevX; prevY = curPart.prevY; } },
SnakeLayer
constructor. This code is temporary , it is needed only to check whether everything is done at this stage of development: ctor: function () { ... for (var parts = 0; parts < 10; parts++) { this.addPart(); } },
cc.eventManager.addListener({ event: cc.EventListener.LISTENER, // onEventType: callBackFunction // }, this);
cc.eventManager.addListener({ event: cc.EventListener.KEYBOARD, onKeyDown: callBackFunction1, onKeyUp: callBackFunction2 }, this);
SnakeLayer
: curDir: 0, /* , */
moveSnake
to “this.curDir” in the update
method. Bring him to this form: this.moveSnake(this.curDir);
ctor
method of the ctor
layer: /* */ cc.eventManager.addListener({ event: cc.EventListener.KEYBOARD, onKeyPressed: function(keyCode, event) { var targ = event.getCurrentTarget(); /* , */ var up = 1, down = -1, left = -2, right = 2; /* , */ var keyMap = {}; keyMap[87] = up; // w keyMap[83] = down; // s keyMap[65] = left; // a keyMap[68] = right; // d /* */ if (keyMap[keyCode] !== undefined) { targ.curDir = keyMap[keyCode]; } } }, this);
curDir
out that one of the keys of interest is pressed, we change the value of the curDir
variable to one that corresponds to the desired direction. At the next iteration of the game cycle, the snake will begin to move in the chosen direction until one of the directional keys is pressed. All this is good, but on a mobile device with a touch screen will not work. Therefore, we add touch control to the project.ctor
method so that the snake can control the touch screen: /* */ cc.eventManager.addListener({ event: cc.EventListener.TOUCH_ONE_BY_ONE, onTouchBegan: function() { /* onTouchMoved, true */ return true; }, onTouchMoved: function(touch, event) { var targ = event.getCurrentTarget(); var up = 1, down = -1, left = -2, right = 2; /* */ var delta = touch.getDelta(); /* */ if (delta.x !== 0 && delta.y !== 0) { if (Math.abs(delta.x) > Math.abs(delta.y)) { /* , */ targ.curDir = Math.sign(delta.x) * right; } else if (Math.abs(delta.x) < Math.abs(delta.y)) { /* , */ targ.curDir = Math.sign(delta.y) * up; } } /* , , */ } }, this);
Source: https://habr.com/ru/post/281639/
All Articles