cocos2d-x 新手引导

10 篇文章 0 订阅

新手引导是游戏开发过程中必须要有的模块。以下的代码实现了整个游戏界面只有一个按钮可以点击,这刚好是做新手引导所必须的功能

首先自定义一个按钮,这个按钮的参数有优先级,方法实现的代理,优先级等:

//
//  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

这样总是只能保证只有一个按钮相应事件,即使他们的优先级相同。基于上面的实现,我们可以很方便的实现新手引导模块的开发。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值