1 //创建叶子
2 void HelloWorld::leafInit()
3 {
4 CCSprite *spriteLeaf1 = CCSprite::create("mao.png");
5 spriteLeaf1->setRotation(30);//旋转角度
6 spriteLeaf1->setAnchorPoint(ccp(0.5, 3));//设置精灵锚点
7 spriteLeaf1->setPosition(ccp(450, 500));//叶子1第一次初始位置
8 spriteLeaf1->setScale(0.5);//设置叶片大小
9 this->addChild(spriteLeaf1,100,TAG_LEAF1);
10 this->playLeafAnim(spriteLeaf1);//调用play函数播实现叶动作
11 CCSprite *spriteLeaf2 = CCSprite::create("mao.png");
12 spriteLeaf2->setRotation(50);
13 spriteLeaf2->setAnchorPoint(ccp(0.5, 3));
14 spriteLeaf2->setPosition(ccp(200, 540));
15 spriteLeaf2->setScale(0.5);
16 this->addChild(spriteLeaf2,101,TAG_LEAF2);
17 this->playLeafAnim(spriteLeaf2);
18 }
19
20 void HelloWorld::playLeafAnim(CCSprite *spriteLeaf)
21 {
22 int iTag = spriteLeaf->getTag();
23 CCLog("playtag%d", iTag);
24 time_t time, roTime;
25 float fAngle1, fAngle2;
26 if (iTag == TAG_LEAF1)
27 {
28 CCLog("tag1");
29 time = 10;//叶子下落的时间
30 roTime = 2.5;//叶子单向摆动一次时间
31 fAngle1 = -80;//叶子逆时针摆动角度
32 fAngle2 = 80;//顺时针摆动角度
33 }
34 else
35 {
36 CCLog("tag2");
37 time = 14;
38 roTime = 3.2;
39 fAngle1 = -100;
40 fAngle2 = 100;
41 }
42 CCLog("rotime%ffAngle1%ffAngle2%f",roTime, fAngle1,fAngle1);
43 //随机生成叶子横向偏移值
44 srand((UINT)GetCurrentTime());
45 int iRandPos = rand() % 250;
46 CCLog("Pianyi%d", iRandPos);
47 //叶子所运动到的位置
48 CCMoveTo *moveTo = CCMoveTo::create(time, ccp(CCDirector::sharedDirector()->getWinSize().width - iRandPos, 30));
49 //CCCallFuncN *actDone = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::resetLeafPos));
50 CCFadeOut *actDone = CCFadeOut::create(0.2f);
51 CCFiniteTimeAction *putdown = CCSequence::create(moveTo, actDone, NULL);
52 //叶子旋转动作
53 CCRotateBy *rotaBy1 = CCRotateBy::create(roTime, fAngle1);
54 CCRotateBy *rotaBy2 = CCRotateBy::create(roTime, fAngle2);
55 //叶子翻转动作
56 spriteLeaf->setVertexZ(60);//设置深度抬高60,避免出现使用CCOrbitCamera实现空间翻转时产生错位和遮挡等问题
57 //CCDirector::sharedDirector()->setDepthTest(false);
58 //关闭深度测试同样可以避免上述问题,不过,推荐使用深度设置setVertexZ来正确解决,因为有时你可能需要遮挡的效果,关闭深度测试后将造成遮挡效果的缺失
59 CCOrbitCamera * orbit = CCOrbitCamera::create(8, 1, 0, 0, 360, 45, 0);
60 //让树叶精灵始终执行三维翻转的动作
61 CCRepeat *fz3d = CCRepeat::create(orbit, -1);//无限循环执行叶片翻转的动作
62 //CCRepeatForever *fz3d = CCRepeatForever::actionWithAction(orbit);
63 //由于下面使用CCSpawn同时执行动作,所以不可以使用无限次数类型的动作,而因使用有线次数循环CCRepeat将循环次数设置为-1
64 //用CCEaseInOut包装落叶摆动的动作,让树叶的进入、出现更自然(淡入淡出效果)
65 CCEaseInOut *ease1 = CCEaseInOut::create(rotaBy1, 3);
66 CCEaseInOut *ease2 = CCEaseInOut::create(rotaBy2, 3);
67 //摆动动作合成
68 CCFiniteTimeAction *seq2 = CCSequence::create(ease1, ease2, NULL);//依次执行顺时针、逆时针摆动
69 CCRepeat *baidong = CCRepeat::create(seq2, -1);//摆动合成
70 //动作执行->同时执行所有动作
71 spriteLeaf->runAction(CCSpawn::create(putdown, baidong, fz3d, NULL));
72 }
73
74
75 void HelloWorld::resetLeafPos(CCNode* sender)
76 {
77 int iTag = int(sender->getTag());//获得被重置叶片的标签
78 int iZoder = int(sender->getZOrder());//获取被重置叶片的z轴值
79 sender->removeFromParentAndCleanup(true);//清除已经落到底点的叶子
80 char sImg[15] = "img_yezi_1.png";
81 _snprintf(sImg, sizeof(sImg), "img_yezi_%d.png", iTag % 100);
82 CCPoint pos;
83 float fAngle;
84 //随机生成叶子的起始位置
85 srand((UINT)GetCurrentTime());
86 int iRand = (rand() % 200);
87 if (iTag == TAG_LEAF1)
88 {
89 pos = ccp(iRand, 600);
90 fAngle = 30;
91 }
92 else
93 {
94 pos = ccp(iRand, 570);
95 fAngle = 50;
96 }
97 //重新生成新的叶片,在起点处释放
98 CCSprite *spriteLeaf = CCSprite::create(sImg);
99 spriteLeaf->setScale(0.5);
100 spriteLeaf->setAnchorPoint(ccp(0.5, 3));
101 spriteLeaf->setRotation(fAngle);
102 spriteLeaf->setPosition(pos);
103 this->addChild(spriteLeaf, iZoder,iTag);
104 this->playLeafAnim(spriteLeaf);//重置后的树叶再次执行飘落动作
105 }