cocos2dx实现小地图

使用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()方法,实时刷新小地图,不过应该很消耗性能,所以刷新频率不要太频繁

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值