// // button.cpp // buttontest // // // #include "MyButtonScene.h" #include "SimpleAudioEngine.h" using namespace cocos2d; using namespace CocosDenshion; bool MyButton::init() { if(!CCLayer::init()) { return false; } CCSize size=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",this,menu_selector(MyButton::menuCloseCallback1)); this->addChild(fontpttf,1); this->setScale(0.5); //实现横排列图标及透明度切换 for (int i=0; i<2; i++) { CCMenuItemImage *menuImage2=CCMenuItemImage::create("btn-about-normal.png", "btn-about-selected.png",this,menu_selector(MyButton::menuCloseCallback1)); CCMenuItemImage *menuImage3=CCMenuItemImage::create("btn-highscores-normal.png", "btn-highscores-selected.png",this,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,NULL); CCSize size=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", "btn-about-selected.png",this,menu_selector(MyButton::menuCloseCallback3)); CCMenuItemImage *menuImage9=CCMenuItemImage::create("btn-highscores-normal.png", "btn-highscores-selected.png",this,menu_selector(MyButton::menuCloseCallback4)); CCMenuItemImage *menuImage10=CCMenuItemImage::create("btn-play-normal.png", "btn-play-selected.png",this,menu_selector(MyButton::menuCloseCallback5)); CCMenu *menu4=CCMenu::create(menuImage8,menuImage9,menuImage10,NULL); menu4->alignItemsVerticallyWithPadding(200); //menu3->setPosition(ccp() menu4->setTag(20); this->addChild(menu4,1); //实现文字之间的切换(回调函数实现透明度的改变) CCMenuItemFont::setFontSize(34); CCMenuItemToggle *toggle= CCMenuItemToggle::createWithTarget( this, menu_selector(MyButton::menuCloseCallback1),CCMenuItemFont::create("on"), CCMenuItemFont::create("off"), NULL); toggle->setSelectedIndex(0); toggle->setPosition(ccp(size.width/4*3,size.height/4*3)); CCMenu *menu5=CCMenu::create(toggle,NULL); 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(this, menu_selector(MyButton::menuCallback), image1, image2, image3, NULL); CCMenu *ccmenuu=CCMenu::create(togglee,NULL); //实现三者之间的顺序切换 ccmenuu->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2-120,CCDirector::sharedDirector()->getWinSize().height/2)); this->addChild(ccmenuu,2); //精灵相关 int idx=(int )(CCRANDOM_0_1()*1400/100); //CCRANDOM_0_1()为系统定义的宏,实现产生0-1之间的随机数 int x=(idx%5)*85; int y= (idx/5)*121; CCSprite *sprite=CCSprite::create("grossini_dance_atlas.png",CCRectMake(x, y, 85, 121)); //从图片中截取部分,构成精灵 this->addChild(sprite,2); 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,size.height/2)); CCSprite *sprite1=CCSprite::createWithTexture(batchsprite->getTexture(),CCRectMake(x, y, 85, 121)); CCSprite *sprite2=CCSprite::createWithTexture(batchsprite->getTexture(),CCRectMake(0, 0, 85, 121)); this->addChild(sprite1); this->addChild(sprite2); sprite2->setPosition(ccp(size.width/2, size.height/2)); //sprite1-> CCActionInterval* action; float random = CCRANDOM_0_1(); if( random < 0.20 ) action = CCScaleBy::create(3, 2); else if(random < 0.40) action = CCRotateBy::create(3, 360); else if( random < 0.60) action = CCBlink::create(1, 3); else if( random < 0.8 ) action = CCTintBy::create(2, 0, -255, -255); else action = CCFadeOut::create(2); //sprite1->runAction(action); CCFiniteTimeAction *actionback=CCSequence::create(action,action->reverse(),NULL); //设置动作集合,顺序依次执行 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",CCRectMake(0, 0, 85, 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)); array->addControlPoint(ccp(0,0)); CCCardinalSplineBy *action3=CCCardinalSplineBy::create(3 ,array, 2); //在三秒内在这几个点之间来回,最后一个参数为张力,1为正方形,大于1为内陷的正方形,小于1为外凸的正方形(内陷外凸是针对直线来说的) CCActionInterval *reverserv=action3->reverse(); CCFiniteTimeAction *seq=CCSequence::create(action3,reverserv,NULL); sprite3->runAction(CCRepeat::create(seq,5)); CCSprite *sprite4=CCSprite::create("grossini_dance_atlas.png",CCRectMake(0, 0, 85, 121)); addChild(sprite4,2); CCAction *action4=CCSpawn::create(CCJumpBy::create(9,CCPointMake(300, 0) ,50 , 50),CCRotateBy::create(9, 720),NULL);//动作集合,同时执行 sprite4->runAction(action4); return true; } void MyButton::menuCallback(CCLayer *pSender) { } void MyButton::menuCloseCallback1(CCLayer *pSender) { CCMenu* menu = (CCMenu*)(((CCNode*)(pSender))->getParent()); GLubyte opacity = menu->getOpacity(); if( opacity == 128 ) menu->setOpacity(255); else menu->setOpacity(128); } void MyButton::menuCloseCallback2(CCLayer *pSender) { CCMenuItemImage *image=(CCMenuItemImage*)pSender->getParent()->getChildByTag(2); if(image->isEnabled()) { image->setEnabled(false); } else { image->setEnabled(true); } } void MyButton::menuCloseCallback3(CCLayer *pSender) { float kwidth=pSender->getScale(); if(kwidth<=1.0) { pSender->setScale(2*kwidth); } else { pSender->setScale(kwidth/2); } } void MyButton::menuCloseCallback4(CCLayer *pSender) { float tmpskew=pSender->getSkewX(); if(tmpskew==0) { pSender->setSkewX(-45); } else { pSender->setSkewX(45); // pSender->setSkewY(0); } } void MyButton::menuCloseCallback5(CCLayer *pSender) { CCMenuItemFont *newfont=CCMenuItemFont::create("点击继续",(CCMenu*)pSender->getParent(),menu_selector(MyButton::menuCloseCallback6) ); newfont->setColor(ccc3(255, 0, 0)); newfont->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height/2)); CCMenu *newmenu=CCMenu::create(newfont,NULL); this->addChild(newmenu,3); CCMenu *menu=(CCMenu*)pSender->getParent(); menu->setEnabled(false); menu->setOpacity(128); } void MyButton::menuCloseCallback6(CCLayer *pSender) { CCMenuItemFont *font=(CCMenuItemFont*)pSender; //font->setColor(ccc3(255, 255, 255)); CCMenu *clicked=(CCMenu*)this->getParent()->getChildByTag(20); clicked->setOpacity(255); clicked->setEnabled(true); font->setVisible(false); //CCDirector::sharedDirector()->resume(); } CCScene* MyButton::scene() { CCScene *scene=CCScene::create(); CCLayer *layer=MyButton::create(); scene->addChild(layer); return scene; } void MyButton::menuCloseCallback(cocos2d::CCLayer *pSender) { CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif CCLog("点击过了"); } bool mySprite::init() { CCSize size=CCDirector::sharedDirector()->getWinSize(); int idx=(int )(CCRANDOM_0_1()*1400/100); int x=(idx%5)*85; int y= (idx/5)*121; CCSprite *sprite=CCSprite::create("grossini_dance_atlas.png",CCRectMake(x, y, 85, 121)); this->addChild(sprite,2); sprite->setPosition(ccp(size.width/2,size.height/2)); } //欢迎转载,转载请注明出处:http://blog.csdn.net/somestill/article/details/9412111