在NewRendererTest 中 展示了Sprite 的4种用法
分别在这4个类中
NewSpriteTest
NewSpriteBatchTest
NewClippingNodeTest
NewDrawNodeTest
//创建普通的精灵
void NewSpriteTest::createSpriteTest()
{
//得到窗口尺寸
Size winSize = Director::getInstance()->getWinSize();
Sprite* parent = Sprite::create("Images/grossini.png");
parent->setPosition(winSize.width/4, winSize.height/2);
Sprite* child1 = Sprite::create("Images/grossinis_sister1.png");
child1->setPosition(0.0f, -20.0f);
Sprite* child2 = Sprite::create("Images/grossinis_sister2.png");
child2->setPosition(20.0f, -20.0f);
Sprite* child3 = Sprite::create("Images/grossinis_sister1.png");
child3->setPosition(40.0f, -20.0f);
Sprite* child4 = Sprite::create("Images/grossinis_sister2.png");
child4->setPosition(60.0f, -20.0f);
Sprite* child5 = Sprite::create("Images/grossinis_sister2.png");
child5->setPosition(80.0f, -20.0f);
Sprite* child6 = Sprite::create("Images/grossinis_sister2.png");
child6->setPosition(100.0f, -20.0f);
Sprite* child7 = Sprite::create("Images/grossinis_sister2.png");
child7->setPosition(120.0f, -20.0f);
//精灵可以包含精灵
parent->addChild(child1);
parent->addChild(child2);
parent->addChild(child3);
parent->addChild(child4);
parent->addChild(child5);
parent->addChild(child6);
parent->addChild(child7);
//层中添加精灵
addChild(parent);
}
//-------- New Sprite Batch Test
//当创建了很多个纹理一样的精灵时 要用BatchNode 加快在手机上运行的速度
NewSpriteBatchTest::NewSpriteBatchTest()
{
//注册触摸监听事件
auto touchListener = EventListenerTouchAllAtOnce::create();
touchListener->onTouchesEnded = CC_CALLBACK_2(NewSpriteBatchTest::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
//创建一个批量节点 这是一张大图 包含很多动作的图
auto BatchNode = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 50);
//将批量节点添加到舞台上
addChild(BatchNode, 0, kTagSpriteBatchNode);
}
void NewSpriteBatchTest::onTouchesEnded(const std::vector<Touch *> &touches, Event *event)
{
for (auto &touch : touches)
{
//得到点击位置
auto location = touch->getLocation();
addNewSpriteWithCoords(location);
}
}
void NewSpriteBatchTest::addNewSpriteWithCoords(Point p)
{
//得到刚才添加到舞台上的批量节点
auto BatchNode = static_cast<SpriteBatchNode*>( getChildByTag(kTagSpriteBatchNode) );
//随机得到0-14
int idx = (int) (CCRANDOM_0_1() * 1400 / 100);
int x = (idx%5) * 85;
int y = (idx/5) * 121;
auto sprite = Sprite::createWithTexture(/*动作集合的大图*/BatchNode->getTexture(), /*得到指定的动作*/Rect(x,y,85,121));
//添加到批量节点中去
BatchNode->addChild(sprite);
sprite->setPosition( Point( p.x, p.y) );
//动作类
ActionInterval* action;
float random = CCRANDOM_0_1();
if( random < 0.20 )
//缩放
action = ScaleBy::create(3, 2);
else if(random < 0.40)
//旋转
action = RotateBy::create(3, 360);
else if( random < 0.60)
//闪烁
action = Blink::create(1, 3);
else if( random < 0.8 )
//颜色
action = TintBy::create(2, 0, -255, -255);
else
//淡出
action = FadeOut::create(2);
//反方向动作
auto action_back = action->reverse();
//创建动作列表
auto seq = Sequence::create(action, action_back, NULL);
//执行动作 永久重复
sprite->runAction( RepeatForever::create(seq));
}
//修剪节点
NewClippingNodeTest::NewClippingNodeTest()
{
auto s = Director::getInstance()->getWinSize();
//初始化一个修剪类
auto clipper = ClippingNode::create();
clipper->setTag( kTagClipperNode );
//设置容器大小
clipper->setContentSize( Size(200, 200) );
//设置锚点
clipper->setAnchorPoint( Point(0.5, 0.5) );
clipper->setPosition( Point(s.width / 2, s.height / 2) );
clipper->runAction(RepeatForever::create(RotateBy::create(1, 45)));
this->addChild(clipper);
//Test with alpha Test 设置透明度值
clipper->setAlphaThreshold(0.05f);
//模板
auto stencil = Sprite::create("Images/grossini.png");
stencil->setPosition(s.width/2, s.height/2);
clipper->setStencil(stencil);
//内容 即需要显示的图片
auto content = Sprite::create("Images/background2.png");
content->setTag( kTagContentNode );
content->setAnchorPoint( Point(0.5, 0.5) );
content->setPosition( Point(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2) );
clipper->addChild(content);
_scrolling = false;
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(NewClippingNodeTest::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(NewClippingNodeTest::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(NewClippingNodeTest::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
/**
* NewDrawNode 绘制一个节点
*/
NewDrawNodeTest::NewDrawNodeTest()
{
auto s = Director::getInstance()->getWinSize();
//创建一个普通节点
auto parent = Node::create();
parent->setPosition(s.width/2, s.height/2);
addChild(parent);
//创建一个绘制节点
auto rectNode = DrawNode::create();
Point rectangle[4];
//角的位置
rectangle[0] = Point(-50, -50);
rectangle[1] = Point(50, -50);
rectangle[2] = Point(50, 50);
rectangle[3] = Point(-50, 50);
//颜色
Color4F white(1, 1, 1, 1);
//绘制多边形
rectNode->drawPolygon(rectangle, 4, white, 1, white);
//把可绘制的节点添加到普通节点上
parent->addChild(rectNode);
}