上篇文章中,我们可以移动我们的坦克了,但你会发现,坦克可以随意移动,并不会被墙之类的挡住。在这之前,我要讲下地图中的坐标值跟瓦片值,看如下图:
每一个瓦片,都有一个坐标,你可以把它看作是在一个二维坐标系里,它的原点是在左上角,
瓦片值:
每一个瓦片都有一个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;
}
我们把坦克移动时的坐标点转换成所在地图的坐标点,再转换成瓦片值,实时检测瓦片值,