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;}

我们把坦克移动时的坐标点转换成所在地图的坐标点,再转换成瓦片值,实时检测瓦片值,



原创粉丝点击