加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-js05-添加身体和移动身体

发布时间:2020-12-14 21:36:40 所属栏目:百科 来源:网络整理
导读:Cocos2d-js05-添加身体和移动身体 1、检测蛇是否吃到食物,代码: // 检测蛇是否吃到食物 if ( this . _head . now_col == this . _food . now_col this . _head . now_row == this . _food . now_row ){ // 播放音效 cc .audioEngine. playEffect ( res . b

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
后期也会把该源码传在群里面去,欢迎大家加入讨论!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读