cocos2d 制作5星评分效果

做的是一个评分的展示,不涉及评分的操作

cocos2dx 3.0+


效果如下,分别为8.6分,5分和10分



五角星图片原图



代码片段

for (int i = 0; i < (int)score / 2; i++)
	{
		auto starImg = Sprite::create("appstore/applist/star_full.png");
		starImg->setAnchorPoint(Point::ANCHOR_TOP_LEFT);
		starImg->setPosition(26 + i * 35,48);
		_detailHolder->addChild(starImg,POSTER_LAYER);
	}
	float percent = score/2 - (int)score/2;
	if ( percent != 0)
	{
		auto starImg = Sprite::create("appstore/applist/star_empty.png");
		starImg->setAnchorPoint(Point::ANCHOR_TOP_LEFT);
		starImg->setPosition(26 + (int)score / 2 * 35 + starImg->getContentSize().width * percent,48);
		starImg->setTextureRect(Rect(starImg->getContentSize().width * percent,starImg->getContentSize().width - starImg->getContentSize().width * percent,starImg->getContentSize().height));
		_detailHolder->addChild(starImg,POSTER_LAYER);

		auto starImg2 = Sprite::create("appstore/applist/star_full.png");
		starImg2->setAnchorPoint(Point::ANCHOR_TOP_LEFT);
		starImg2->setPosition(26 + (int)score / 2 * 35,48);
		starImg2->setTextureRect(Rect(0,starImg2->getContentSize().width * percent,starImg2->getContentSize().height));
		_detailHolder->addChild(starImg2,POSTER_LAYER);
	}


这边主要是用setTextureRect来切割星星的图片,我也是参考别人学习的这个方法,具体可以看如下链接

http://blog.sina.com.cn/s/blog_65be11bd0101o8ou.html

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...