cocos2d-x学习笔记—图片按钮

前端之家收集整理的这篇文章主要介绍了cocos2d-x学习笔记—图片按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. //@H_502_19@
  2. //button.cpp@H_502_19@
  3. //buttontest@H_502_19@
  4. //@H_502_19@
  5. #include"MyButtonScene.h"@H_502_19@
  6. #include"SimpleAudioEngine.h"@H_502_19@
  7. using@H_502_19@namespace@H_502_19@cocos2d;
  8. namespace@H_502_19@CocosDenshion;
  9. bool@H_502_19@MyButton::init()
  10. {
  11. if@H_502_19@(!CCLayer::init())
  12. {
  13. return@H_502_19@false@H_502_19@;
  14. }
  15. CCSizesize=CCDirector::sharedDirector()->getWinSize();//获得屏幕的大小@H_502_19@
  16. CCMenuItemImage*menuImage1=CCMenuItemImage::create("CloseNormal.png"@H_502_19@,"CloseSelected.png"@H_502_19@,this@H_502_19@,menu_selector(MyButton::menuCloseCallback1));
  17. menuImage1->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
  18. CCMenu*menu=CCMenu::create(menuImage1,NULL);
  19. menu->setPosition(CCPointZero);
  20. this@H_502_19@->addChild(menu,2);
  21. CCSprite*nsprite=CCSprite::create("HelloWorld.png"@H_502_19@);
  22. nsprite->setPosition(ccp(size.width/2,size.height/2));
  23. this@H_502_19@->addChild(nsprite,0);
  24. //CCLabelTTF*pttf=CCLabelTTF::create("hah","Thonburi",50);@H_502_19@
  25. //pttf->setPosition(CCDirector::sharedDirector()->getWinSize().width/2,120);@H_502_19@
  26. CCMenuItemFont*fontpttf=CCMenuItemFont::create("pttf"@H_502_19@,153); font-weight:bold; background-color:inherit">this@H_502_19@->addChild(fontpttf,1);
  27. this@H_502_19@->setScale(0.5);
  28. //实现横排列图标及透明度切换@H_502_19@
  29. for@H_502_19@(int@H_502_19@i=0;i<2;i++){
  30. CCMenuItemImage*menuImage2=CCMenuItemImage::create("btn-about-normal.png"@H_502_19@,"btn-about-selected.png"@H_502_19@,248)"> CCMenuItemImage*menuImage3=CCMenuItemImage::create("btn-highscores-normal.png"@H_502_19@,"btn-highscores-selected.png"@H_502_19@,menu_selector(MyButton::menuCloseCallback1));
  31. CCMenuItemImage*menuImage4=CCMenuItemImage::create("btn-play-normal.png"@H_502_19@,"btn-play-selected.png"@H_502_19@,menuImage2,menu_selector(MyButton::menuCloseCallback2));
  32. menuImage2->setTag(2);//添加标签,以便后期调用@H_502_19@
  33. menuImage2->setEnabled(false@H_502_19@);//设置按钮不可用@H_502_19@
  34. CCMenu*menu2=CCMenu::create(menuImage2,menuImage3,menuImage4,248)"> CCSizesize=CCDirector::sharedDirector()->getWinSize();
  35. menu2->setPosition(ccp(size.width/4,size.height/4));
  36. menu2->alignItemsHorizontally();
  37. menu2->setOpacity(128);//设置透明度@H_502_19@
  38. this@H_502_19@->addChild(menu2,1,100+i);
  39. //实现图标的竖排排列@H_502_19@
  40. CCMenuItemImage*menuImage8=CCMenuItemImage::create("btn-about-normal.png"@H_502_19@,menu_selector(MyButton::menuCloseCallback3));
  41. CCMenuItemImage*menuImage9=CCMenuItemImage::create("btn-highscores-normal.png"@H_502_19@,menu_selector(MyButton::menuCloseCallback4));
  42. CCMenuItemImage*menuImage10=CCMenuItemImage::create("btn-play-normal.png"@H_502_19@,menu_selector(MyButton::menuCloseCallback5));
  43. CCMenu*menu4=CCMenu::create(menuImage8,menuImage9,menuImage10,NULL);
  44. menu4->alignItemsVerticallyWithPadding(200);
  45. //menu3->setPosition(ccp()@H_502_19@
  46. menu4->setTag(20);
  47. this@H_502_19@->addChild(menu4,0); background-color:inherit">//实现文字之间的切换(回调函数实现透明度的改变)@H_502_19@
  48. CCMenuItemFont::setFontSize(34);
  49. CCMenuItemToggle*toggle=CCMenuItemToggle::createWithTarget(
  50. menu_selector(MyButton::menuCloseCallback1),CCMenuItemFont::create("on"@H_502_19@),
  51. CCMenuItemFont::create("off"@H_502_19@),248)"> NULL);
  52. toggle->setSelectedIndex(0);
  53. toggle->setPosition(ccp(size.width/4*3,size.height/4*3));
  54. CCMenu*menu5=CCMenu::create(toggle,153); font-weight:bold; background-color:inherit">this@H_502_19@->addChild(menu5,2);
  55. //实现图片之间点击切换@H_502_19@
  56. CCMenuItemImage*image1=CCMenuItemImage::create("btn-about-normal.png"@H_502_19@,"btn-about-selected.png"@H_502_19@);
  57. CCMenuItemImage*image2=CCMenuItemImage::create("btn-highscores-normal.png"@H_502_19@,"btn-highscores-selected.png"@H_502_19@);
  58. CCMenuItemImage*image3=CCMenuItemImage::create("btn-play-normal.png"@H_502_19@,"btn-play-selected.png"@H_502_19@);
  59. CCMenuItemToggle*togglee=CCMenuItemToggle::createWithTarget( menu_selector(MyButton::menuCallback),
  60. image1,
  61. image2,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> image3,248)"> CCMenu*ccmenuu=CCMenu::create(togglee,NULL);//实现三者之间的顺序切换@H_502_19@
  62. ccmenuu->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2-120,CCDirector::sharedDirector()->getWinSize().height/2));
  63. this@H_502_19@->addChild(ccmenuu,0); background-color:inherit">//精灵相关@H_502_19@
  64. int@H_502_19@idx=(int@H_502_19@)(CCRANDOM_0_1()*1400/100);//CCRANDOM_0_1()为系统定义的宏,实现产生0-1之间的随机@H_502_19@
  65. int@H_502_19@x=(idx%5)*85;
  66. int@H_502_19@y=(idx/5)*121;
  67. CCSprite*sprite=CCSprite::create("grossini_dance_atlas.png"@H_502_19@,CCRectMake(x,y,85,121));//从图片截取部分,构成精灵@H_502_19@
  68. this@H_502_19@->addChild(sprite,248)"> sprite->setPosition(ccp(size.width/2+70,size.height/2));
  69. CCActionInterval*newaction=CCMoveTo::create(13,ccp(size.width/2-120,size.height/2));//设置动作,移动到指定位置@H_502_19@
  70. sprite->runAction(CCRepeatForever::create(newaction));//执行动作@H_502_19@
  71. setTouchEnabled(true@H_502_19@);//设置接收触摸事件@H_502_19@
  72. CCSpriteBatchNode*batchsprite=CCSpriteBatchNode::create("grossini_dance_atlas.png"@H_502_19@,50);//设置精灵批处理,预计产生50个精灵@H_502_19@
  73. this@H_502_19@->addChild(batchsprite,2,11);
  74. batchsprite->setPosition(ccp(size.width/2,248)"> CCSprite*sprite1=CCSprite::createWithTexture(batchsprite->getTexture(),121));
  75. CCSprite*sprite2=CCSprite::createWithTexture(batchsprite->getTexture(),CCRectMake(0,121));
  76. this@H_502_19@->addChild(sprite1);
  77. this@H_502_19@->addChild(sprite2);
  78. sprite2->setPosition(ccp(size.width/2,0); background-color:inherit">//sprite1->@H_502_19@
  79. CCActionInterval*action;
  80. float@H_502_19@random=CCRANDOM_0_1();
  81. if@H_502_19@(random<0.20)
  82. action=CCScaleBy::create(3,2);
  83. else@H_502_19@if@H_502_19@(random<0.40)
  84. action=CCRotateBy::create(3,360);
  85. if@H_502_19@(random<0.60)
  86. action=CCBlink::create(1,3);
  87. if@H_502_19@(random<0.8)
  88. action=CCTintBy::create(2,-255,-255);
  89. else@H_502_19@
  90. action=CCFadeOut::create(2);
  91. //sprite1->runAction(action);@H_502_19@
  92. CCFiniteTimeAction*actionback=CCSequence::create(action,action->reverse(),0); background-color:inherit">//设置动作集合,顺序依次执行@H_502_19@
  93. sprite1->runAction(CCRepeat::create(actionback,6));//重复6次执行动作@H_502_19@
  94. CCActionInterval*action1=CCFadeIn::create(2);
  95. CCActionInterval*action2=CCJumpTo::create(2,CCPointMake(300,0),80,5);
  96. sprite1->runAction(CCSequence::create(action1,action1->reverse(),NULL));
  97. sprite2->runAction(CCRepeatForever::create(action2));
  98. CCSprite*sprite3=CCSprite::create("grossini_dance_atlas.png"@H_502_19@,121));
  99. addChild(sprite3);
  100. CCPointArray*array=CCPointArray::create(20);//创建点集合@H_502_19@
  101. array->addControlPoint(ccp(0,0));
  102. array->addControlPoint(ccp(size.width/2-30,0));
  103. array->addControlPoint(ccp(size.width/2-30,size.height-80));
  104. array->addControlPoint(ccp(0,size.height-80));
  105. CCCardinalSplineBy*action3=CCCardinalSplineBy::create(3,array,2);//在三秒内在这几个点之间来回,最后一个参数为张力,1为正方形,大于1为内陷的正方形,小于1为外凸的正方形(内陷外凸是针对直线来说的)@H_502_19@
  106. CCActionInterval*reverserv=action3->reverse();
  107. CCFiniteTimeAction*seq=CCSequence::create(action3,reverserv,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> sprite3->runAction(CCRepeat::create(seq,5));
  108. CCSprite*sprite4=CCSprite::create("grossini_dance_atlas.png"@H_502_19@,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> addChild(sprite4,2);
  109. CCAction*action4=CCSpawn::create(CCJumpBy::create(9,50,50),CCRotateBy::create(9,720),0); background-color:inherit">//动作集合,同时执行@H_502_19@
  110. sprite4->runAction(action4);
  111. true@H_502_19@;
  112. void@H_502_19@MyButton::menuCallback(CCLayer*pSender)
  113. void@H_502_19@MyButton::menuCloseCallback1(CCLayer*pSender)
  114. CCMenu*menu=(CCMenu*)(((CCNode*)(pSender))->getParent());
  115. GLubyteopacity=menu->getOpacity();
  116. if@H_502_19@(opacity==128)
  117. menu->setOpacity(255);
  118. else@H_502_19@
  119. menu->setOpacity(128);
  120. }
  121. void@H_502_19@MyButton::menuCloseCallback2(CCLayer*pSender)
  122. CCMenuItemImage*image=(CCMenuItemImage*)pSender->getParent()->getChildByTag(2);
  123. if@H_502_19@(image->isEnabled())
  124. image->setEnabled(false@H_502_19@);
  125. true@H_502_19@);
  126. void@H_502_19@MyButton::menuCloseCallback3(CCLayer*pSender)
  127. float@H_502_19@kwidth=pSender->getScale();
  128. if@H_502_19@(kwidth<=1.0)
  129. pSender->setScale(2*kwidth);
  130. pSender->setScale(kwidth/2);
  131. void@H_502_19@MyButton::menuCloseCallback4(CCLayer*pSender)
  132. float@H_502_19@tmpskew=pSender->getSkewX();
  133. if@H_502_19@(tmpskew==0)
  134. pSender->setSkewX(-45);
  135. pSender->setSkewX(45);
  136. //pSender->setSkewY(0);@H_502_19@
  137. void@H_502_19@MyButton::menuCloseCallback5(CCLayer*pSender)
  138. CCMenuItemFont*newfont=CCMenuItemFont::create("点击继续"@H_502_19@,(CCMenu*)pSender->getParent(),menu_selector(MyButton::menuCloseCallback6));
  139. newfont->setColor(ccc3(255,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> newfont->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,248)"> CCMenu*newmenu=CCMenu::create(newfont,153); font-weight:bold; background-color:inherit">this@H_502_19@->addChild(newmenu,3);
  140. CCMenu*menu=(CCMenu*)pSender->getParent();
  141. menu->setEnabled(false@H_502_19@);
  142. menu->setOpacity(128);
  143. void@H_502_19@MyButton::menuCloseCallback6(CCLayer*pSender)
  144. CCMenuItemFont*font=(CCMenuItemFont*)pSender;
  145. //font->setColor(ccc3(255,255));@H_502_19@
  146. CCMenu*clicked=(CCMenu*)this@H_502_19@->getParent()->getChildByTag(20);
  147. clicked->setOpacity(255);
  148. clicked->setEnabled(true@H_502_19@);
  149. font->setVisible(//CCDirector::sharedDirector()->resume();@H_502_19@
  150. CCScene*MyButton::scene()
  151. CCScene*scene=CCScene::create();
  152. CCLayer*layer=MyButton::create();
  153. scene->addChild(layer);
  154. return@H_502_19@scene;
  155. void@H_502_19@MyButton::menuCloseCallback(cocos2d::CCLayer*pSender)
  156. CCDirector::sharedDirector()->end();
  157. #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)@H_502_19@
  158. exit(0);
  159. #endif@H_502_19@
  160. CCLog("点击过了"@H_502_19@);
  161. bool@H_502_19@mySprite::init()
  162. CCSizesize=CCDirector::sharedDirector()->getWinSize();
  163. int@H_502_19@)(CCRANDOM_0_1()*1400/100);
  164. int@H_502_19@x=(idx%5)*85;
  165. int@H_502_19@y=(idx/5)*121;
  166. CCSprite*sprite=CCSprite::create("grossini_dance_atlas.png"@H_502_19@,121));
  167. sprite->setPosition(ccp(size.width/2,0); background-color:inherit">//欢迎转载,转载请注明出处:<atarget="_blank"href="http://blog.csdn.net/somestill/article/details/9412111">http://blog.csdn.net/somestill/article/details/9412111</a>@H_502_19@



from:http://blog.csdn.net/somestill/article/details/9412111

猜你在找的Cocos2d-x相关文章