使用cocos2dx 2.0版本制作圆形的小地图
红色为player,绿色为npc
代码块
/*x,y表示玩家在大地图位置的百分比*/
void SmallMap::smallMapMove(int x, int y) {
y = 100 - y; //如果不减y的坐标是反的
int r = 80; //小地图半径
if (mX == x&&mY == y){return;}
if (map!=NULL){
removeChild(map,true);
map = NULL;
}
int rectX = mapSize.width*x / 100-r;
int rectY = mapSize.height*y / 100-r;
if (rectX<0){rectX = 0;}
if (rectY<0){rectY = 0;}
if (rectX + 160 > mapSize.width) { rectX = mapSize.width - 160; }
if (rectY + 160 > mapSize.height) { rectY = mapSize.height - 160; }
CCSprite* map1 = CCSprite::create(mapName.c_str(), CCRectMake(rectX, rectY, 160, 160));
map = maskedSprite(map1);
addChild(map, 11);
UIAdapter::sharedUIAdapter()->setPosition(map, 50, 50);
std::list<int>::iterator it;
CCPoint midPt = ccp(rectX + r, mapSize.height - (rectY + r));//显示小地图中心对应的整个中地图坐标
//遍历npc坐标,显示小地图半径内的npc
for (it = list.begin(); it != list.end(); it++)
{
CCPoint pt=npcPtPers[*it]
CCPoint npcPt = ccp(info.x*mapSize.width / 100, info.y*mapSize.height / 100);
float dis= ccpDistance(npcPt, midPt);
float d_x = npcPt.x - midPt.x;
float d_y = npcPt.y - midPt.y;
if (dis<(r-4))
{
CCSprite* p = CCSprite::create("map/npcIcon/1.png");
p->setScale(0.5);
map->addChild(p);
p->setPosition(ccp(r+d_x,r+d_y));
}
}
mX = x;
mY = y;
y = 100 - y;
int playerX = mapSize.width*x / 100;
int playerY = mapSize.height*y / 100;
CCPoint playerPt = ccp(playerX - midPt.x+r, playerY - midPt.y+r); //得到npc坐标(原点为小地图的(0,0))
float dis_player = ccpDistance(playerPt, ccp(r,r));//计算player左边与小地图中心点的距离
if (dis_player<(r-4))
{
CCSprite* player = CCSprite::create("map/npcIcon/2.png");
map->addChild(player);
player->setPosition(playerPt);
}
}
/*将图片切成圆形*/
CCSprite * SmallMap::maskedSprite(CCSprite *textureSprite)
{
CCSprite * maskSprite = CCSprite::create("map/smallBK.png");//圆形图片
CCRenderTexture * renderTexture = CCRenderTexture::create(maskSprite->getContentSize().width, maskSprite->getContentSize().height);
maskSprite->setPosition(ccp(maskSprite->getContentSize().width / 2, maskSprite->getContentSize().height / 2));
textureSprite->setPosition(ccp(textureSprite->getContentSize().width / 2, textureSprite->getContentSize().height / 2));
ccBlendFunc cb1 = { GL_ONE, GL_ZERO };
ccBlendFunc cb2 = { GL_DST_ALPHA, GL_ZERO };
maskSprite->setBlendFunc(cb1);
textureSprite->setBlendFunc(cb2);
renderTexture->begin();
maskSprite->visit();
textureSprite->visit();
renderTexture->end();
CCSprite * retval = CCSprite::createWithTexture(renderTexture->getSprite()->getTexture());
retval->setFlipY(true);
return retval;
}
在uptate中调用smallMapMove()方法,实时刷新小地图,不过应该很消耗性能,所以刷新频率不要太频繁