cocos2d-x的初步学习十九之坦克大战六
来源:互联网 发布:淘宝店面设计 知乎 编辑:程序博客网 时间:2024/05/17 05:08
上篇文章中,我们可以移动我们的坦克了,但你会发现,坦克可以随意移动,并不会被墙之类的挡住。在这之前,我要讲下地图中的坐标值跟瓦片值,看如下图:
每一个瓦片,都有一个坐标,你可以把它看作是在一个二维坐标系里,它的原点是在左上角,
瓦片值:
每一个瓦片都有一个id,tid为0时,表示没有,或是透明,
首页我们先要获取我们的坦克在地图上的坐标点,然后把坐标点转换成瓦片值做判断,看代码:
MapLayer.cpp:
//触摸点坐标转化成地图上的坐标点CCPoint MapLayer::tileCoordinateFromPosition(CCPoint pos){ int cox,coy; CCSize szLayer=_bg1Layer->getLayerSize(); CCSize szTile=gameMap->getTileSize(); cox = pos.x / szTile.width; //与地图坐标相反,所以减 coy = szLayer.height - pos.y / szTile.height; if ((cox >=0) && (cox < szLayer.width) && (coy >= 0) && (coy < szLayer.height)) { return ccp(cox,coy); }else { return ccp(-1,-1); }}//将地图坐标点转换成瓦片值int MapLayer::tileIDFromPosition(CCPoint pos){ CCPoint cpt = this->tileCoordinateFromPosition(pos); if (cpt.x < 0) return -1; if (cpt.y < 0) return -1; if (cpt.x >= _bg1Layer->getLayerSize().width) return -1; if (cpt.y >= _bg1Layer->getLayerSize().height) return -1; return _bg1Layer->tileGIDAt(cpt);}
接下来,我们在TankSprite类里,修改我们的移动函数
void TankSprite::moveUp(){ //设置旋转方向 this->setRotation(0); kaction=kUp; CCPoint tp=this->getPosition(); CCPoint tnp = ccp(tp.x, tp.y+this->boundingBox().size.height/2+_speed); //边界检测 if ((tp.y + this->boundingBox().size.height/2 + _speed) > _mapSize.height ) return; if (this->checkPoint(tnp)) return; tnp = ccp(tp.x-this->boundingBox().size.width/2,tp.y + this->boundingBox().size.height/2 + _speed); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x - this->boundingBox().size.width/3,tp.y + this->boundingBox().size.height / 2 + _speed); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x + this->boundingBox().size.width/2,tp.y + this->boundingBox().size.height / 2 + _speed); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x + this->boundingBox().size.width/3,tp.y + this->boundingBox().size.height / 2 + _speed); if (this->checkPoint(tnp)) return; this->setPosition(ccp(this->getPosition().x, this->getPosition().y+_speed)); }void TankSprite::moveDown(){ this->setRotation(180); kaction=kDown; CCPoint tp=this->getPosition(); CCPoint tnp = ccp(tp.x, tp.y-this->boundingBox().size.height/2-_speed); // if ((tp.y - this->boundingBox().size.height/2 - _speed) < 0 ) return; if (this->checkPoint(tnp)) return; tnp = ccp(tp.x-this->boundingBox().size.width/2,tp.y - this->boundingBox().size.height/2 - _speed); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x - this->boundingBox().size.width/3,tp.y - this->boundingBox().size.height / 2 - _speed); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x + this->boundingBox().size.width/2,tp.y - this->boundingBox().size.height / 2 - _speed); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x + this->boundingBox().size.width/3,tp.y - this->boundingBox().size.height / 2 - _speed); if (this->checkPoint(tnp)) return; this->setPosition(ccp(this->getPosition().x, this->getPosition().y-_speed)); }void TankSprite::moveLeft(){ this->setRotation(-90); kaction=kLeft; CCPoint tp=this->getPosition(); CCPoint tnp = ccp(tp.x-this->boundingBox().size.width/2-_speed, tp.y); // if (tp.x-this->boundingBox().size.width/2-_speed < 0 ) return; if (this->checkPoint(tnp)) return; tnp = ccp(tp.x - this->boundingBox().size.width/2 - _speed,tp.y + this->boundingBox().size.height/2 - 2); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x - this->boundingBox().size.width/2 - _speed,tp.y + this->boundingBox().size.height/3); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x - this->boundingBox().size.width/2 - _speed,tp.y - this->boundingBox().size.height/2 + 2); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x - this->boundingBox().size.width/2 - _speed,tp.y - this->boundingBox().size.height/3); if (this->checkPoint(tnp)) return; this->setPosition(ccp(this->getPosition().x-_speed, this->getPosition().y)); }void TankSprite::moveRight(){ this->setRotation(90); kaction=kRight; CCPoint tp=this->getPosition(); CCPoint tnp = ccp(tp.x+this->boundingBox().size.width/2+_speed, tp.y); // if (tp.x+this->boundingBox().size.width/2+_speed > _mapSize.width ) return; if (this->checkPoint(tnp)) return; tnp = ccp(tp.x - this->boundingBox().size.width/2 + _speed,tp.y + this->boundingBox().size.height/2 - 2); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x - this->boundingBox().size.width/2 + _speed,tp.y + this->boundingBox().size.height/3); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x - this->boundingBox().size.width/2 + _speed,tp.y - this->boundingBox().size.height/2 + 2); if (this->checkPoint(tnp)) return; tnp = ccp(tp.x - this->boundingBox().size.width/2 + _speed,tp.y - this->boundingBox().size.height/3); if (this->checkPoint(tnp)) return; this->setPosition(ccp(this->getPosition().x+_speed, this->getPosition().y)); }//检测坐标点bool TankSprite::checkPoint(CCPoint pon){ CCPoint tnp = pon; unsigned int tid; tid = _mapLayer->tileIDFromPosition(tnp); if (tid != 0 && tid != 9 && tid != 10 && tid != 37 && tid != 38 ) return true; return false;}
我们把坦克移动时的坐标点转换成所在地图的坐标点,再转换成瓦片值,实时检测瓦片值,
- cocos2d-x的初步学习十九之坦克大战六
- cocos2d-x的初步学习十四之坦克大战一
- cocos2d-x的初步学习十五之坦克大战二
- cocos2d-x的初步学习十六之坦克大战三
- cocos2d-x的初步学习十七之坦克大战四
- cocos2d-x的初步学习十八之坦克大战五
- cocos2d-x的初步学习二十之坦克大战七
- cocos2d-x游戏开发系列教程-坦克大战游戏之坦克的显示
- cocos2d-x游戏开发系列教程-坦克大战游戏之敌方坦克AI的编写
- cocos2d-x游戏开发系列教程-坦克大战游戏之所有坦克之间的碰撞检测
- cocos2d-x的初步学习六
- cocos2d-x游戏开发系列教程-坦克大战游戏之坦克和地图碰撞的检测上
- cocos2d-x游戏开发系列教程-坦克大战游戏之坦克和地图碰撞的检测下
- cocos2d-x游戏开发系列教程-坦克大战游戏之虚拟手柄的显示
- cocos2d-x游戏开发系列教程-坦克大战游戏之子弹的碰撞检测处理
- cocos2d-x游戏开发系列教程-坦克大战游戏之虚拟手柄的显示
- cocos2d-x游戏开发系列教程-坦克大战游戏之虚拟手柄控制坦克移动
- Java坦克大战 (六) 之增加可玩性
- directX绘制球体
- android线性布局的属性解释
- 一个简单C程序的结构
- 黑马程序员---多线程
- 串的处理
- cocos2d-x的初步学习十九之坦克大战六
- ECSHOP手机访问和电脑访问效果一样
- POJ2892 Tunnel Warfare
- 反素数
- 黑马程序员_java基础--多线程
- android UI布局
- 寻找数组中最大的K个数
- 13年7月7日 ACM 第一天培训
- 黑马程序员--String