使用CCLayer::onExit()禁用特定layer层的按钮响应事件

//HelloWorldScene.cpp  此方法太缺德,建议实在没有办法的时候再于考虑!!!!!!
#include "HelloWorldScene.h"
#include "AppMacros.h"
#include "testLayer.h"	
USING_NS_CC;


CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    /
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);





        CCMenuItemImage *pMenuItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCallback));

	pMenuItem->setPosition(ccp(origin.x + visibleSize.width - 100,
                                origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    CCMenu* pMenu1 = CCMenu::create(pMenuItem, NULL);
    pMenu1->setPosition(CCPointZero);
    this->addChild(pMenu1, 1);
    /
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE);
    
    // position the label on the center of the screen
    pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - pLabel->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(pLabel, 1);

    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
    return true;
}


void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
void HelloWorld::menuCallback(CCObject* pSender)
{
	CCLayer::onExit();
        /*添加新的layer时候,将自己layer中的childrenonExit,这时候所有的这个layer上的所有孩子不会响应touch,并且即将添加的layer也不会响应,所以我们需要在TestLayer中调用一次onEnter()*/
	TestLayer* layer = new TestLayer();
	this->addChild(layer,2);
}
void HelloWorld::onEnter()
{
        /*重新定义HelloWold层的onEnter()*/
	CCLayer::onEnter();
}

HelloWorld.h

 

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  
    virtual void onEnter();
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
   void  menuCallback(CCObject* pSender);
    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__


testLayer.cpp

#include "testLayer.h"	
#include "HelloWorldScene.h"
TestLayer::TestLayer()
{
	CCLayer::onEnter();
	CCSprite *bkg = CCSprite::create("HelloWorld.png",CCRect(0,0,100,100));
	this->addChild(bkg);
	bkg->setPosition(ccp(240,160));
        CCMenuItemImage *pMenuItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(TestLayer::removeSelf));

	pMenuItem->setPosition(ccp(240,60));


    // create menu, it's an autorelease object
    CCMenu* pMenu1 = CCMenu::create(pMenuItem, NULL);
    pMenu1->setPosition(CCPointZero);
    this->addChild(pMenu1, 1);
}
TestLayer::~TestLayer()
{}

void TestLayer::removeSelf(CCObject* pSender)
{
	CCMenuItemImage * node = (CCMenuItemImage*)pSender;
	node->getParent()->getParent()->getParent()->onEnter();
        /*删除TestLayer层,并将TestLayer层的父节点层重新注册执行onEnter()*/
	node->getParent()->getParent()->getParent()->removeChild(node->getParent()->getParent());
	//node->getParent()->removeAllChildren();
	//HelloWorld::onEnter();
}


#ifndef TEST_LAYER_H
#define TEST_LAYER_H
#include "cocos2d.h"
using namespace cocos2d;
class TestLayer : public CCLayer
{
public:
	TestLayer();
	~TestLayer();
	void removeSelf(CCObject* pSender);
private:
	
};
#endif


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zanglengyu

敲碗要饭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值