cocos2dx 横版游戏中屏幕如何随精灵移动
在横版游戏中,实现屏幕随精灵移动而移动,要实现这个,首先要明白这个问题中的变与不变:
不变:地图位置;
变:精灵的位置、屏幕(或理解为所在图层)的位置;
故,说是滚动地图,其实是图层滚动;
从不变入手,首先要知道地图(CCTMXTileMap)的两个方法:
1、map->getMapSize():地图的图块数
2、map->getTileSize():每一个图块的宽高
再而分析变:
精灵先移动,屏幕根据精灵移动的坐标去调整;
<喎?http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+ICAgICAgINfczOW31s72x7CjrM/IvMbL48jnz8K8uLj2yv0mIzIwNTQwO6O6PC9wPgo8cD4gICAgICAgICAgICAgILXYzby/7bbIo7p0dz10aWxlbWFwLT5nZXRNYXBTaXplKCkud2lkdGgqdGlsZW1hcC0+Z2V0VGlsZVNpemUoKS53aWR0aDs8L3A+CjxwPiAgICAgICAgICAgICAgtdjNvLjftsijunRoPXRpbGVtYXAtPmdldE1hcFNpemUoKS5oZWlnaHQqdGlsZW1hcC0+Z2V0VGlsZVNpemUoKS5oZWlnaHQ7PC9wPgo8cD4gICAgICAgICAgICAgIMbBxLu/7bbIo7p3dz13aW5zaXplLndpZHRoOzwvcD4KPHA+ICAgICAgICAgICAgICDGwcS7uN+2yKO6d2g9d2luc2l6ZS5oZWlnaHQ7PC9wPgo8cD4gICAgICAgICAgICAgIL6rwenX+LHqo7p4o6x5o7s8L3A+CjxwPjxicj4KPC9wPgo8cD4gICAgICAg19zM5bfWzvajusrXz8jXvMi3xdC2qL6rwenSxravuvO1xNf4seqjrNLyzqrO0sPHtPOyv7fW087Pt9bQtcS+q8HptrzKx9K7uPbNvMaso6yyu8rH0ru49rXjo6y+q8HptcTX+LHqtb3G5M28xqy1xNfz09Kx37rNyc/PwrHfysfT0L7gwOu1xKOst9ax8M6qyc/NvKOotMvNvLP219TKsb/VwdTIy6OsxuS0+sLryrXP1tPrsb7OxM7eudijqdbQtcRBsd+6zUKx36Osy/nS1KOs1fu49r6rwem1xMuuxr3X+LHqz9TKvsrH09C3ts6ntcSjqEE8PXg8PXR3LUGjqaOstvi+q8HptcS0udax1/ix6srHzazA7bXEoaPV4sqxo6y+q8Hp1/ix6srHxNzIt7aotcSjrL3Tz8LAtMrHxsHEu7n2tq/By6GjxsHEu7n2tq/P1Mi709DS1M/CyP3W1sfpv/ajujGhong8PXd3LzKjuwogMqGid3cvMjx4PD10dy13dy8yo7szoaJ0dy13dy8yPHiju9TaMcfpv/ajrMbBxLvOu9bDzqqjqHd3LzKjrHdoLzKjqaO71Noy1tCjrMbBxLvOu9bDzqqjqHijrHdoLzKjqaO71Noz1tCjrMbBxLvOu9bDzqqjqHR3LXd3LzKjrHdoLzKjqaO709C63LbgyMu/ycTc0tG+rbeiz9ajrMbBxLu0udax1/ix6srHsrvTw8iluMSx5LXEo6zS8s6quuGw5tPOz7fU2rS51rHJz8rHw7vT0Ln2tq+1xKOoxsHEu7jftsg9tdjNvLjftsijrLWxyLvV4sDvysfWuLTzsr+31qOpoaPX7rrzo6y9q8bBxLvW0LXjo6h3dy8yo6x3aC8yo6m89cilyc/D5sj91tbH6b/2tcTGwcS7zrvWw7Xjo6y1w7P2ufa2r7XEvuDA66Osvau+4MDrJiMyMDU0MDvS1Nf4serQzsq9tPrI63NldFBvc2l0aW9uo6ijqdbQoaM8L3A+CjxwPjxicj4KPC9wPgo8cD7N6tX7tPrC68jnz8KjujwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">void GameLayer::update(float dt)
{
this->_hero->updateDesiredPosition(dt);
//设置英雄位置
float posX=MIN(this->_tilemp->getMapSize().width*this->_tilemp->getTileSize().width-this->_hero->_centerToSides,MAX(this->_hero->_centerToSides,this->_hero->_desiredPosition.x));
float posY=MIN(3*this->_tilemp->getTileSize().height+this->_hero->_centerToBottom,MAX(_hero->_centerToBottom,_hero->_desiredPosition.y));
this->_hero->setPosition(ccp(posX,posY));
//地图滚动
CCSize winSize=CCDirector::sharedDirector()->getWinSize();
int x=MAX(_hero->getPositionX(),winSize.width/2);
int y=MAX(_hero->getPositionY(),winSize.height/2);
x=MIN(x,(this->_tilemp->getMapSize().width*this->_tilemp->getTileSize().width)-winSize.width/2);
CCPoint actualPosition=ccp(x,y);
CCPoint centerOfView=ccp(winSize.width/2,winSize.height/2);
CCPoint viewPoint=ccpSub(centerOfView,actualPosition);
this->setPosition(viewPoint);
}
}
相关新闻>>
- 发表评论
-
- 最新评论 更多>>