- //
- //button.cpp
- //buttontest
- //
- #include"MyButtonScene.h"
- #include"SimpleAudioEngine.h"
- usingnamespacecocos2d;
- namespaceCocosDenshion;
- boolMyButton::init()
- {
- if(!CCLayer::init())
- {
- returnfalse;
- }
- CCSizesize=CCDirector::sharedDirector()->getWinSize();//获得屏幕的大小
- CCMenuItemImage*menuImage1=CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,menu_selector(MyButton::menuCloseCallback1));
- menuImage1->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width-20,20));
- CCMenu*menu=CCMenu::create(menuImage1,NULL);
- menu->setPosition(CCPointZero);
- this->addChild(menu,2);
- CCSprite*nsprite=CCSprite::create("HelloWorld.png");
- nsprite->setPosition(ccp(size.width/2,size.height/2));
- this->addChild(nsprite,0);
- //CCLabelTTF*pttf=CCLabelTTF::create("hah","Thonburi",50);
- //pttf->setPosition(CCDirector::sharedDirector()->getWinSize().width/2,120);
- CCMenuItemFont*fontpttf=CCMenuItemFont::create("pttf",153); font-weight:bold; background-color:inherit">this->addChild(fontpttf,1);
- this->setScale(0.5);
- //实现横排列图标及透明度切换
- for(inti=0;i<2;i++){
- 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));
- CCMenuItemImage*menuImage4=CCMenuItemImage::create("btn-play-normal.png","btn-play-selected.png",menuImage2,menu_selector(MyButton::menuCloseCallback2));
- menuImage2->setTag(2);//添加标签,以便后期调用
- menuImage2->setEnabled(false);//设置按钮不可用
- CCMenu*menu2=CCMenu::create(menuImage2,menuImage3,menuImage4,248)"> CCSizesize=CCDirector::sharedDirector()->getWinSize();
- menu2->setPosition(ccp(size.width/4,size.height/4));
- menu2->alignItemsHorizontally();
- menu2->setOpacity(128);//设置透明度
- this->addChild(menu2,1,100+i);
- //实现图标的竖排排列
- CCMenuItemImage*menuImage8=CCMenuItemImage::create("btn-about-normal.png",menu_selector(MyButton::menuCloseCallback3));
- CCMenuItemImage*menuImage9=CCMenuItemImage::create("btn-highscores-normal.png",menu_selector(MyButton::menuCloseCallback4));
- CCMenuItemImage*menuImage10=CCMenuItemImage::create("btn-play-normal.png",menu_selector(MyButton::menuCloseCallback5));
- CCMenu*menu4=CCMenu::create(menuImage8,menuImage9,menuImage10,NULL);
- menu4->alignItemsVerticallyWithPadding(200);
- //menu3->setPosition(ccp()
- menu4->setTag(20);
- this->addChild(menu4,0); background-color:inherit">//实现文字之间的切换(回调函数实现透明度的改变)
- CCMenuItemFont::setFontSize(34);
- CCMenuItemToggle*toggle=CCMenuItemToggle::createWithTarget(
- menu_selector(MyButton::menuCloseCallback1),CCMenuItemFont::create("on"),
- CCMenuItemFont::create("off"),248)"> NULL);
- toggle->setSelectedIndex(0);
- toggle->setPosition(ccp(size.width/4*3,size.height/4*3));
- CCMenu*menu5=CCMenu::create(toggle,153); font-weight:bold; background-color:inherit">this->addChild(menu5,2);
- //实现图片之间点击切换
- CCMenuItemImage*image1=CCMenuItemImage::create("btn-about-normal.png","btn-about-selected.png");
- CCMenuItemImage*image2=CCMenuItemImage::create("btn-highscores-normal.png","btn-highscores-selected.png");
- CCMenuItemImage*image3=CCMenuItemImage::create("btn-play-normal.png","btn-play-selected.png");
- CCMenuItemToggle*togglee=CCMenuItemToggle::createWithTarget( menu_selector(MyButton::menuCallback),
- image1,
- 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);//实现三者之间的顺序切换
- ccmenuu->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2-120,CCDirector::sharedDirector()->getWinSize().height/2));
- this->addChild(ccmenuu,0); background-color:inherit">//精灵相关
- intidx=(int)(CCRANDOM_0_1()*1400/100);//CCRANDOM_0_1()为系统定义的宏,实现产生0-1之间的随机数
- intx=(idx%5)*85;
- inty=(idx/5)*121;
- CCSprite*sprite=CCSprite::create("grossini_dance_atlas.png",CCRectMake(x,y,85,121));//从图片中截取部分,构成精灵
- this->addChild(sprite,248)"> sprite->setPosition(ccp(size.width/2+70,size.height/2));
- CCActionInterval*newaction=CCMoveTo::create(13,ccp(size.width/2-120,size.height/2));//设置动作,移动到指定位置
- sprite->runAction(CCRepeatForever::create(newaction));//执行动作
- setTouchEnabled(true);//设置接收触摸事件
- CCSpriteBatchNode*batchsprite=CCSpriteBatchNode::create("grossini_dance_atlas.png",50);//设置精灵批处理,预计产生50个精灵
- this->addChild(batchsprite,2,11);
- batchsprite->setPosition(ccp(size.width/2,248)"> CCSprite*sprite1=CCSprite::createWithTexture(batchsprite->getTexture(),121));
- CCSprite*sprite2=CCSprite::createWithTexture(batchsprite->getTexture(),CCRectMake(0,121));
- this->addChild(sprite1);
- this->addChild(sprite2);
- sprite2->setPosition(ccp(size.width/2,0); background-color:inherit">//sprite1->
- CCActionInterval*action;
- floatrandom=CCRANDOM_0_1();
- if(random<0.20)
- action=CCScaleBy::create(3,2);
- elseif(random<0.40)
- action=CCRotateBy::create(3,360);
- if(random<0.60)
- action=CCBlink::create(1,3);
- if(random<0.8)
- action=CCTintBy::create(2,-255,-255);
- else
- action=CCFadeOut::create(2);
- //sprite1->runAction(action);
- CCFiniteTimeAction*actionback=CCSequence::create(action,action->reverse(),0); background-color:inherit">//设置动作集合,顺序依次执行
- sprite1->runAction(CCRepeat::create(actionback,6));//重复6次执行动作
- CCActionInterval*action1=CCFadeIn::create(2);
- CCActionInterval*action2=CCJumpTo::create(2,CCPointMake(300,0),80,5);
- sprite1->runAction(CCSequence::create(action1,action1->reverse(),NULL));
- sprite2->runAction(CCRepeatForever::create(action2));
- CCSprite*sprite3=CCSprite::create("grossini_dance_atlas.png",121));
- addChild(sprite3);
- CCPointArray*array=CCPointArray::create(20);//创建点集合
- array->addControlPoint(ccp(0,0));
- array->addControlPoint(ccp(size.width/2-30,0));
- array->addControlPoint(ccp(size.width/2-30,size.height-80));
- array->addControlPoint(ccp(0,size.height-80));
- CCCardinalSplineBy*action3=CCCardinalSplineBy::create(3,array,2);//在三秒内在这几个点之间来回,最后一个参数为张力,1为正方形,大于1为内陷的正方形,小于1为外凸的正方形(内陷外凸是针对直线来说的)
- CCActionInterval*reverserv=action3->reverse();
- 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));
- 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);
- CCAction*action4=CCSpawn::create(CCJumpBy::create(9,50,50),CCRotateBy::create(9,720),0); background-color:inherit">//动作集合,同时执行
- sprite4->runAction(action4);
- true;
- voidMyButton::menuCallback(CCLayer*pSender)
- voidMyButton::menuCloseCallback1(CCLayer*pSender)
- CCMenu*menu=(CCMenu*)(((CCNode*)(pSender))->getParent());
- GLubyteopacity=menu->getOpacity();
- if(opacity==128)
- menu->setOpacity(255);
- else
- menu->setOpacity(128);
- }
- voidMyButton::menuCloseCallback2(CCLayer*pSender)
- CCMenuItemImage*image=(CCMenuItemImage*)pSender->getParent()->getChildByTag(2);
- if(image->isEnabled())
- image->setEnabled(false);
- true);
- voidMyButton::menuCloseCallback3(CCLayer*pSender)
- floatkwidth=pSender->getScale();
- if(kwidth<=1.0)
- pSender->setScale(2*kwidth);
- pSender->setScale(kwidth/2);
- voidMyButton::menuCloseCallback4(CCLayer*pSender)
- floattmpskew=pSender->getSkewX();
- if(tmpskew==0)
- pSender->setSkewX(-45);
- pSender->setSkewX(45);
- //pSender->setSkewY(0);
- voidMyButton::menuCloseCallback5(CCLayer*pSender)
- CCMenuItemFont*newfont=CCMenuItemFont::create("点击继续",(CCMenu*)pSender->getParent(),menu_selector(MyButton::menuCloseCallback6));
- 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);
- CCMenu*menu=(CCMenu*)pSender->getParent();
- menu->setEnabled(false);
- menu->setOpacity(128);
- voidMyButton::menuCloseCallback6(CCLayer*pSender)
- CCMenuItemFont*font=(CCMenuItemFont*)pSender;
- //font->setColor(ccc3(255,255));
- CCMenu*clicked=(CCMenu*)this->getParent()->getChildByTag(20);
- clicked->setOpacity(255);
- clicked->setEnabled(true);
- font->setVisible(//CCDirector::sharedDirector()->resume();
- CCScene*MyButton::scene()
- CCScene*scene=CCScene::create();
- CCLayer*layer=MyButton::create();
- scene->addChild(layer);
- returnscene;
- voidMyButton::menuCloseCallback(cocos2d::CCLayer*pSender)
- CCDirector::sharedDirector()->end();
- #if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
- exit(0);
- #endif
- CCLog("点击过了");
- boolmySprite::init()
- CCSizesize=CCDirector::sharedDirector()->getWinSize();
- int)(CCRANDOM_0_1()*1400/100);
- intx=(idx%5)*85;
- inty=(idx/5)*121;
- CCSprite*sprite=CCSprite::create("grossini_dance_atlas.png",121));
- 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