cocos2d-x 新手引導

新手引導是遊戲開發過程中必須要有的模塊。以下的代碼實現了整個遊戲界面只有一個按鈕可以點擊,這剛好是做新手引導所必須的功能

首先自定義一個按鈕,這個按鈕的參數有優先級,方法實現的代理,優先級等:

//
//  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);
    addChild(normalS,1);
    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 + getPositionX(),r.origin.y + 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 + getPositionX(),r.origin.y + 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));
    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));
    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));
    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));
    
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

這樣總是隻能保證只有一個按鈕相應事件,即使他們的優先級相同。基於上面的實現,我們可以很方便的實現新手引導模塊的開發。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章