本文主要是介绍Cocos2d-js05-添加身体和移动身体,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Cocos2d-js05-添加身体和移动身体
1、检测蛇是否吃到食物,代码:
//检测蛇是否吃到食物 if(this._head.now_col==this._food.now_col && this._head.now_row==this._food.now_row){//播放音效cc.audioEngine.playEffect(res.bg_effect);cc.log("蛇吃到了食物!");//添加分数this.m_score += 100;score.setString("分数:"+this.m_score);//重置食物的位置this._food.now_row = Math.round(Math.random()*9);this._food.now_col = Math.round(Math.random()*9);this._food.setPosition(cc.p(this._food.now_col*63,this._food.now_row*63));//添加蛇的身体this._snakeBody = SnakeGame.create(2);this._snakeBody.setScale(0.9);if(SNAKE_BODY.length == 0 || SNAKE_BODY.length == null){switch (dir){case SNAKE_DIR.UP:this._snakeBody.now_row = this._head.now_row - 1;this._snakeBody.now_col = this._head.now_col;break;case SNAKE_DIR.DOWN:this._snakeBody.now_row = this._head.now_row + 1;this._snakeBody.now_col = this._head.now_col;break;case SNAKE_DIR.LEFT:this._snakeBody.now_row = this._head.now_row;this._snakeBody.now_col = this._head.now_col + 1;break;case SNAKE_DIR.RIGHT:this._snakeBody.now_row = this._head.now_row;this._snakeBody.now_col = this._head.now_col - 1;break;default :break;}cc.log("里面没有身体,添加一个!");}else{switch (dir){case SNAKE_DIR.UP:this._snakeBody.now_row = this._snakeBody.now_row - 1;this._snakeBody.now_col = this._snakeBody.now_col;break;case SNAKE_DIR.DOWN:this._snakeBody.now_row = this._snakeBody.now_row + 1;this._snakeBody.now_col = this._snakeBody.now_col;break;case SNAKE_DIR.LEFT:this._snakeBody.now_row = this._snakeBody.now_row;this._snakeBody.now_col = this._snakeBody.now_col + 1;break;case SNAKE_DIR.RIGHT:this._snakeBody.now_row = this._snakeBody.now_row;this._snakeBody.now_col = this._snakeBody.now_col - 1;break;default :break;}cc.log("里面有身体,添加一个!");}//添加到数组中去SNAKE_BODY.push(this._snakeBody);this.getChildByTag(111).addChild(this._snakeBody,2);this._snakeBody.setPosition(cc.p(this._snakeBody.now_col*63,this._snakeBody.now_row*63)); }
2、移动所有的身体,代码:
//移动所有的身体 if(SNAKE_BODY.length != 0){var Snode = null;for(var i = SNAKE_BODY.length - 1; i >= 0; i--){Snode = SNAKE_BODY[i];if(i == 0){switch (dir){case SNAKE_DIR.UP:Snode.now_row = this._head.now_row - 1;Snode.now_col = this._head.now_col;break;case SNAKE_DIR.DOWN:Snode.now_row = this._head.now_row + 1;Snode.now_col = this._head.now_col;break;case SNAKE_DIR.LEFT:Snode.now_row = this._head.now_row;Snode.now_col = this._head.now_col + 1;break;case SNAKE_DIR.RIGHT:Snode.now_row = this._head.now_row;Snode.now_col = this._head.now_col - 1;break;default :break;}}else{Snode.now_col = SNAKE_BODY[i-1].now_col;Snode.now_row = SNAKE_BODY[i-1].now_row;}Snode.setPosition(cc.p(Snode.now_col*63,Snode.now_row*63));} }
视频地址:http://www.9miaoketang.com/course/37 课程讨论帖地址:http://www.9miao.com/thread-64587-1-1.html 源码地址:https://store.cocos.com/stuff/show/128289.html QQ交流群:83459374 后期也会把该源码传在群里面去,欢迎大家加入讨论!
这篇关于Cocos2d-js05-添加身体和移动身体的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!