新手引导是游戏开发过程中必须要有的模块。以下的代码实现了整个游戏界面只有一个按钮可以点击,这刚好是做新手引导所必须的功能。
首先自定义一个按钮,这个按钮的参数有优先级,方法实现的代理,优先级等:
// // B_Button.h // HelloCpp // // Created by blary on 14-8-16. // // #ifndef __HelloCpp__B_Button__ #define __HelloCpp__B_Button__ #include <iostream> #include "cocos2d.h" using namespace cocos2d; class B_Button : public CCLayer, public CCTargetedTouchDelegate { public: virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); bool initWithImageAndMethodAndPriority(const char* normal, const char* selected, int n, void(*f)(int), CCObject* tar, int priority = kCCMenuHandlerPriority); private: CCSprite* normalS; CCSprite* selectedS; CCObject* target; int pid; void (*handler)(int); }; #endif /* defined(__HelloCpp__B_Button__) */
下面是实现部分:
// // B_Button.cpp // HelloCpp // // Created by blary on 14-8-16. // // #include "B_Button.h" bool B_Button::initWithImageAndMethodAndPriority(const char* normal, const char* selected, int n, void(*f)(int), CCObject* tar, int priority) { if (!CCLayer::init()) { return false; } normalS = CCSprite::create(normal); selectedS = CCSprite::create(selected); target = tar; pid = n; normalS->setVisible(true); selectedS->setVisible(false); this->addChild(normalS,1); this->addChild(selectedS,1); handler=f; CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate((CCTargetedTouchDelegate*)this, priority, true); return true; } bool B_Button::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCPoint p = pTouch->getLocation(); p = CCDirector::sharedDirector()->convertToUI(p); CCRect r = selectedS->boundingBox(); CCRect rect(r.origin.x+this->getPositionX(),r.origin.y+this->getPositionY(),r.size.width,r.size.height); if (rect.containsPoint(p)) { normalS->setVisible(false); selectedS->setVisible(true); } return true; } void B_Button::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { if (normalS->isVisible()==false&&selectedS->isVisible()==true) { normalS->setVisible(false); selectedS->setVisible(true); } CCPoint p = pTouch->getLocation(); p = CCDirector::sharedDirector()->convertToUI(p); CCRect r = selectedS->boundingBox(); CCRect rect(r.origin.x+this->getPositionX(),r.origin.y+this->getPositionY(),r.size.width,r.size.height); if (rect.containsPoint(p)) { (handler)(pid); normalS->setVisible(true); selectedS->setVisible(false); } }
下面是如何使用的部分:
#pragma once #include "cocos2d.h" class HelloWorld : public cocos2d::CCLayer { public: virtual bool init(); static cocos2d::CCScene* scene(); CREATE_FUNC(HelloWorld); }; #ifdef __cplusplus extern "C" { #endif void shutdownGame(int a); #ifdef __cplusplus } #endif
#include "HelloWorldScene.h" #include "B_Button.h" using namespace cocos2d; // 枚举值定义 一般都是layer名称+作用 enum { HELLO_LAYER_BOTTON_A, HELLO_LAYER_BUTTON_AA, HELLO_LAYER_BUTTON_B, HELLO_LAYER_BUTTON_BB, }; CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { scene = CCScene::create(); HelloWorld *layer = HelloWorld::create(); scene->addChild(layer); } while (0); return scene; } bool HelloWorld::init() { if (!CCLayer::init()) { return false; } CCSize win = CCDirector::sharedDirector()->getWinSize(); // 使用自定义按钮 你可以使用默认的优先级按钮 B_Button* a = new B_Button; a->initWithImageAndMethodAndPriority("CloseNormal.png", "CloseSelected.png", HELLO_LAYER_BOTTON_A, shutdownGame, this, 0); a->setPosition(CCPoint(win.width/2, win.height/2)); this->addChild(a, 1); a->autorelease(); B_Button* b = new B_Button; b->initWithImageAndMethodAndPriority("CloseNormal.png", "CloseSelected.png", HELLO_LAYER_BUTTON_B, shutdownGame, this, 1); b->setPosition(CCPoint(win.width/2, win.height/2)); this->addChild(b, 1); b->autorelease(); B_Button* aa = new B_Button; aa->initWithImageAndMethodAndPriority("CloseNormal.png", "CloseSelected.png", HELLO_LAYER_BUTTON_AA, shutdownGame, this, 0); aa->setPosition(CCPoint(win.width/2 + 100, win.height/2)); this->addChild(aa, 2); aa->autorelease(); B_Button* bb = new B_Button; bb->initWithImageAndMethodAndPriority("CloseNormal.png", "CloseSelected.png", HELLO_LAYER_BUTTON_BB, shutdownGame, this, 1); bb->setPosition(CCPoint(win.width/2 + 100, win.height/2)); this->addChild(bb, 2); bb->autorelease(); return true; } #ifdef __cplusplus extern "C" { #endif void shutdownGame(int a) { switch (a) { case HELLO_LAYER_BUTTON_BB: { printf(" BB !\n"); } break; case HELLO_LAYER_BUTTON_B: { printf(" B !\n"); } break; case HELLO_LAYER_BUTTON_AA: { printf(" AA !\n"); } break; case HELLO_LAYER_BOTTON_A: { printf(" A !\n"); } break; default: break; } } #ifdef __cplusplus } #endif
这样总是只能保证只有一个按钮相应事件,即使他们的优先级相同。基于上面的实现,我们可以很方便的实现新手引导模块的开发。
时间: 2024-10-23 07:01:59