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

前端之家收集整理的这篇文章主要介绍了cocos2d-x学习笔记—图片按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. //
  2. //button.cpp
  3. //buttontest
  4. //
  5. #include"MyButtonScene.h"
  6. #include"SimpleAudioEngine.h"
  7. usingnamespacecocos2d;
  8. namespaceCocosDenshion;
  9. boolMyButton::init()
  10. {
  11. if(!CCLayer::init())
  12. {
  13. returnfalse;
  14. }
  15. CCSizesize=CCDirector::sharedDirector()->getWinSize();//获得屏幕的大小
  16. CCMenuItemImage*menuImage1=CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,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->addChild(menu,2);
  21. CCSprite*nsprite=CCSprite::create("HelloWorld.png");
  22. nsprite->setPosition(ccp(size.width/2,size.height/2));
  23. this->addChild(nsprite,0);
  24. //CCLabelTTF*pttf=CCLabelTTF::create("hah","Thonburi",50);
  25. //pttf->setPosition(CCDirector::sharedDirector()->getWinSize().width/2,120);
  26. CCMenuItemFont*fontpttf=CCMenuItemFont::create("pttf",153); font-weight:bold; background-color:inherit">this->addChild(fontpttf,1);
  27. this->setScale(0.5);
  28. //实现横排列图标及透明度切换
  29. for(inti=0;i<2;i++){
  30. CCMenuItemImage*menuImage2=CCMenuItemImage::create("btn-about-normal.png","btn-about-selected.png",248)"> CCMenuItemImage*menuImage3=CCMenuItemImage::create("btn-highscores-normal.png","btn-highscores-selected.png",menu_selector(MyButton::menuCloseCallback1));
  31. CCMenuItemImage*menuImage4=CCMenuItemImage::create("btn-play-normal.png","btn-play-selected.png",menuImage2,menu_selector(MyButton::menuCloseCallback2));
  32. menuImage2->setTag(2);//添加标签,以便后期调用
  33. menuImage2->setEnabled(false);//设置按钮不可用
  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);//设置透明度
  38. this->addChild(menu2,1,100+i);
  39. //实现图标的竖排排列
  40. CCMenuItemImage*menuImage8=CCMenuItemImage::create("btn-about-normal.png",menu_selector(MyButton::menuCloseCallback3));
  41. CCMenuItemImage*menuImage9=CCMenuItemImage::create("btn-highscores-normal.png",menu_selector(MyButton::menuCloseCallback4));
  42. CCMenuItemImage*menuImage10=CCMenuItemImage::create("btn-play-normal.png",menu_selector(MyButton::menuCloseCallback5));
  43. CCMenu*menu4=CCMenu::create(menuImage8,menuImage9,menuImage10,NULL);
  44. menu4->alignItemsVerticallyWithPadding(200);
  45. //menu3->setPosition(ccp()
  46. menu4->setTag(20);
  47. this->addChild(menu4,0); background-color:inherit">//实现文字之间的切换(回调函数实现透明度的改变)
  48. CCMenuItemFont::setFontSize(34);
  49. CCMenuItemToggle*toggle=CCMenuItemToggle::createWithTarget(
  50. menu_selector(MyButton::menuCloseCallback1),CCMenuItemFont::create("on"),
  51. CCMenuItemFont::create("off"),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->addChild(menu5,2);
  55. //实现图片之间点击切换
  56. CCMenuItemImage*image1=CCMenuItemImage::create("btn-about-normal.png","btn-about-selected.png");
  57. CCMenuItemImage*image2=CCMenuItemImage::create("btn-highscores-normal.png","btn-highscores-selected.png");
  58. CCMenuItemImage*image3=CCMenuItemImage::create("btn-play-normal.png","btn-play-selected.png");
  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);//实现三者之间的顺序切换
  62. ccmenuu->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2-120,CCDirector::sharedDirector()->getWinSize().height/2));
  63. this->addChild(ccmenuu,0); background-color:inherit">//精灵相关
  64. intidx=(int)(CCRANDOM_0_1()*1400/100);//CCRANDOM_0_1()为系统定义的宏,实现产生0-1之间的随机
  65. intx=(idx%5)*85;
  66. inty=(idx/5)*121;
  67. CCSprite*sprite=CCSprite::create("grossini_dance_atlas.png",CCRectMake(x,y,85,121));//从图片截取部分,构成精灵
  68. this->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));//设置动作,移动到指定位置
  70. sprite->runAction(CCRepeatForever::create(newaction));//执行动作
  71. setTouchEnabled(true);//设置接收触摸事件
  72. CCSpriteBatchNode*batchsprite=CCSpriteBatchNode::create("grossini_dance_atlas.png",50);//设置精灵批处理,预计产生50个精灵
  73. this->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->addChild(sprite1);
  77. this->addChild(sprite2);
  78. sprite2->setPosition(ccp(size.width/2,0); background-color:inherit">//sprite1->
  79. CCActionInterval*action;
  80. floatrandom=CCRANDOM_0_1();
  81. if(random<0.20)
  82. action=CCScaleBy::create(3,2);
  83. elseif(random<0.40)
  84. action=CCRotateBy::create(3,360);
  85. if(random<0.60)
  86. action=CCBlink::create(1,3);
  87. if(random<0.8)
  88. action=CCTintBy::create(2,-255,-255);
  89. else
  90. action=CCFadeOut::create(2);
  91. //sprite1->runAction(action);
  92. CCFiniteTimeAction*actionback=CCSequence::create(action,action->reverse(),0); background-color:inherit">//设置动作集合,顺序依次执行
  93. sprite1->runAction(CCRepeat::create(actionback,6));//重复6次执行动作
  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",121));
  99. addChild(sprite3);
  100. CCPointArray*array=CCPointArray::create(20);//创建点集合
  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为外凸的正方形(内陷外凸是针对直线来说的)
  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",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">//动作集合,同时执行
  110. sprite4->runAction(action4);
  111. true;
  112. voidMyButton::menuCallback(CCLayer*pSender)
  113. voidMyButton::menuCloseCallback1(CCLayer*pSender)
  114. CCMenu*menu=(CCMenu*)(((CCNode*)(pSender))->getParent());
  115. GLubyteopacity=menu->getOpacity();
  116. if(opacity==128)
  117. menu->setOpacity(255);
  118. else
  119. menu->setOpacity(128);
  120. }
  121. voidMyButton::menuCloseCallback2(CCLayer*pSender)
  122. CCMenuItemImage*image=(CCMenuItemImage*)pSender->getParent()->getChildByTag(2);
  123. if(image->isEnabled())
  124. image->setEnabled(false);
  125. true);
  126. voidMyButton::menuCloseCallback3(CCLayer*pSender)
  127. floatkwidth=pSender->getScale();
  128. if(kwidth<=1.0)
  129. pSender->setScale(2*kwidth);
  130. pSender->setScale(kwidth/2);
  131. voidMyButton::menuCloseCallback4(CCLayer*pSender)
  132. floattmpskew=pSender->getSkewX();
  133. if(tmpskew==0)
  134. pSender->setSkewX(-45);
  135. pSender->setSkewX(45);
  136. //pSender->setSkewY(0);
  137. voidMyButton::menuCloseCallback5(CCLayer*pSender)
  138. CCMenuItemFont*newfont=CCMenuItemFont::create("点击继续",(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->addChild(newmenu,3);
  140. CCMenu*menu=(CCMenu*)pSender->getParent();
  141. menu->setEnabled(false);
  142. menu->setOpacity(128);
  143. voidMyButton::menuCloseCallback6(CCLayer*pSender)
  144. CCMenuItemFont*font=(CCMenuItemFont*)pSender;
  145. //font->setColor(ccc3(255,255));
  146. CCMenu*clicked=(CCMenu*)this->getParent()->getChildByTag(20);
  147. clicked->setOpacity(255);
  148. clicked->setEnabled(true);
  149. font->setVisible(//CCDirector::sharedDirector()->resume();
  150. CCScene*MyButton::scene()
  151. CCScene*scene=CCScene::create();
  152. CCLayer*layer=MyButton::create();
  153. scene->addChild(layer);
  154. returnscene;
  155. voidMyButton::menuCloseCallback(cocos2d::CCLayer*pSender)
  156. CCDirector::sharedDirector()->end();
  157. #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
  158. exit(0);
  159. #endif
  160. CCLog("点击过了");
  161. boolmySprite::init()
  162. CCSizesize=CCDirector::sharedDirector()->getWinSize();
  163. int)(CCRANDOM_0_1()*1400/100);
  164. intx=(idx%5)*85;
  165. inty=(idx/5)*121;
  166. CCSprite*sprite=CCSprite::create("grossini_dance_atlas.png",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>



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

原文链接:https://www.f2er.com/cocos2dx/340258.html

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