关闭

Cocos2d-X Scrollview与CCMenu的结合使用在屏幕滑动实例

标签: Cocos2d-XScrollview与CCMenu的结合
799人阅读 评论(0) 收藏 举报

1、myMenu.h

#include <iostream>

#include"cocos2d.h"


USING_NS_CC;


class MyMenu:public CCMenu

{

public:

    MyMenu(){};

    ~MyMenu(){};

    

    static MyMenu* create();

    

    virtual void registerWithTouchDispather()

    {

        CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, false);

    }

 

    bool moved;

    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);

    

};

2.myMenu.cpp

#include "myMenu.h"


MyMenu* MyMenu::create()

{

    MyMenu *pRet=new MyMenu();

    if( pRet && pRet->init() )

    {

        pRet->autorelease();

        return pRet;

    }

    //安全释放内存

    CC_SAFE_DELETE(pRet);

    return NULL;

}


bool MyMenu:: ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)

{

    moved=false;

    return CCMenu::ccTouchBegan(pTouch, pEvent);

}

void MyMenu::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)

{

    CCLog("-------------ghviuebgvrft-------------");

    moved=true;

    CCMenu::ccTouchMoved(pTouch, pEvent);

}

void MyMenu::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)

{

    if( !moved )

    {

        CCMenu::ccTouchBegan(pTouch, pEvent);

    }

    else

    {

        m_eState=kCCMenuStateWaiting;

    }

}

void MyMenu::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)

{



}

3、HelloWorldScene.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();  


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

    

    // implement the "static node()" method manually

    CREATE_FUNC(HelloWorld);

    

    

    

    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);

    virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);

    

    //void callBackClick();

};

4、HelloWorldScene.cpp

#include "HelloWorldScene.h"

#include "myMenu.h"

//位置

#define POS_CENTER  ccp(winSize.width/2, winSize.height/2)

//尺寸

#define SIZE_SCROLL (CCSizeMake(340,290))

#define SIZE_SCROLL_LAYER (CCSizeMake(7*winSize.width,224))


const char *const NAME_CHAPTER[7]={"HelloWorld.png","HelloWorld.png","HelloWorld.png","HelloWorld.png","HelloWorld.png","HelloWorld.png","HelloWorld.png"};

#define PATH_CHAPTER  (CCFileUtils::sharedFileUtils()->getWritablePath()+"CHAPTER.XML").c_str()


//速度宏

#define SPEEND_SCROLL 800

#define degressToAngle(x) (M_PI *(x)/180.0 )


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;

    }

    //不论优先级后面是true还是false  都不影响

    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);

    CCSize winSize=CCDirector::sharedDirector()->getWinSize();

    

    CCScrollView *scrollView=CCScrollView::create(CCSizeMake(340,290));

    CCLayer *contentLayer=CCLayer::create();

    contentLayer->setContentSize(CCSizeMake(7*winSize.width, 224));

    

    MyMenu *menu=MyMenu::create();

    //contentLayer->setContentSize(CCSizeMake(7*winSize.width, winSize.height));

    for(int i=0;i<7;i++)

    {

        CCMenuItemImage *sp=CCMenuItemImage::create(NAME_CHAPTER[i], NAME_CHAPTER[i]);

        sp->setTarget(this, menu_selector(HelloWorld::menuCloseCallback));

        sp->setScale(0.5);

        sp->setPosition(ccp(80+240*i,0));

        sp->setAnchorPoint(CCPointZero);

        menu->addChild(sp);

    }

    menu->setPosition(CCPointZero);

    menu->setAnchorPoint(CCPointZero);

    contentLayer->addChild(menu);

    scrollView->setContainer(contentLayer);

    scrollView->setDirection(kCCScrollViewDirectionHorizontal);

    scrollView->setContentOffset(ccp(-80,0));

    scrollView->setPosition(ccp(100,80));

    

    this->addChild(scrollView);

    

    return true;

}



void HelloWorld::menuCloseCallback(CCObject* pSender)

{

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)

CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");

#else

    CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    exit(0);

#endif

#endif

}



bool HelloWorld:: ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)

{

    return true;

}

void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)

{

    

}

void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)

{

    

}

void HelloWorld::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)

{

    

}




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:215143次
    • 积分:4963
    • 等级:
    • 排名:第5972名
    • 原创:284篇
    • 转载:68篇
    • 译文:3篇
    • 评论:10条
    最新评论