cocos2d-x 2.X demo学习笔记 2 ----controller源码 主要是滚动菜单的操作


#include "controller.h"

#include "testResource.h"
#include "tests.h"


#define LINE_SPACE          40  //总共有40个test场景


static CCPoint s_tCurPos = CCPointZero;//创建一个点初始位置为0点。全局的。


static TestScene* CreateTestScene(int nIdx)
{
    CCDirector::sharedDirector()->purgeCachedData();//清楚所有缓存数据


    TestScene* pScene = NULL;//设置一个场景,后期所有场景可以用此替换


    switch (nIdx)
    {//选中的是哪个场景,然后创建
    case TEST_ACTIONS:
        pScene = new ActionsTestScene(); break;
    case TEST_TRANSITIONS:
        pScene = new TransitionsTestScene(); break;
     case TEST_PROGRESS_ACTIONS:
         pScene = new ProgressActionsTestScene(); break;
    case TEST_EFFECTS:
        pScene = new EffectTestScene(); break;
    case TEST_CLICK_AND_MOVE:
        pScene = new ClickAndMoveTestScene(); break;
    case TEST_ROTATE_WORLD:
        pScene = new RotateWorldTestScene(); break;
    case TEST_PARTICLE:
        pScene = new ParticleTestScene(); break;
    case TEST_EASE_ACTIONS:
        pScene = new ActionsEaseTestScene(); break;
    case TEST_MOTION_STREAK:
        pScene = new MotionStreakTestScene(); break;
    case TEST_DRAW_PRIMITIVES:
        pScene = new DrawPrimitivesTestScene(); break;
    case TEST_COCOSNODE:
        pScene = new CocosNodeTestScene(); break;
    case TEST_TOUCHES:
        pScene = new PongScene(); break;
    case TEST_MENU:
        pScene = new MenuTestScene(); break;
    case TEST_ACTION_MANAGER:
        pScene = new ActionManagerTestScene(); break;
    case TEST_LAYER:
        pScene = new LayerTestScene(); break;
    case TEST_SCENE:
        pScene = new SceneTestScene(); break;
    case TEST_PARALLAX:
        pScene = new ParallaxTestScene(); break;
    case TEST_TILE_MAP:
        pScene = new TileMapTestScene(); break;
    case TEST_INTERVAL:
        pScene = new IntervalTestScene(); break;
    case TEST_CHIPMUNKACCELTOUCH:
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
        pScene = new ChipmunkAccelTouchTestScene(); break;
#else
#ifdef MARMALADEUSECHIPMUNK
#if    (MARMALADEUSECHIPMUNK == 1)
        pScene = new ChipmunkAccelTouchTestScene(); 
#endif
        break;
#endif
#endif
    case TEST_LABEL:
        pScene = new AtlasTestScene(); break;
    case TEST_TEXT_INPUT:
        pScene = new TextInputTestScene(); break;
    case TEST_SPRITE:
        pScene = new SpriteTestScene(); break;
    case TEST_SCHEDULER:
        pScene = new SchedulerTestScene(); break;
    case TEST_RENDERTEXTURE:
        pScene = new RenderTextureScene(); break;
    case TEST_TEXTURE2D:
        pScene = new TextureTestScene(); break;
    case TEST_BOX2D:
        pScene = new Box2DTestScene(); break;
    case TEST_BOX2DBED:
        pScene = new Box2dTestBedScene(); break;
    case TEST_EFFECT_ADVANCE:
        pScene = new EffectAdvanceScene(); break;
    case TEST_ACCELEROMRTER:
        pScene = new AccelerometerTestScene(); break;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
    case TEST_KEYPAD:
        pScene = new KeypadTestScene(); break;
#endif
    case TEST_COCOSDENSHION:
        pScene = new CocosDenshionTestScene(); break;
    case TEST_PERFORMANCE:
        pScene = new PerformanceTestScene(); break;
    case TEST_ZWOPTEX:
        pScene = new ZwoptexTestScene(); break;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
// bada don't support libcurl
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
    case TEST_CURL:
        pScene = new CurlTestScene(); break;
#endif
#endif
    case TEST_USERDEFAULT:
        pScene = new UserDefaultTestScene(); break;
    case TEST_BUGS:
        pScene = new BugsTestScene(); break;
    case TEST_FONTS:
        pScene = new FontTestScene(); break;
    case TEST_CURRENT_LANGUAGE:
        pScene = new CurrentLanguageTestScene(); break;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
    case TEST_TEXTURECACHE: pScene = new TextureCacheTestScene(); break;
#endif
    case TEST_EXTENSIONS:
        {
            pScene = new ExtensionsTestScene();
        }
        break;
    case TEST_SHADER:
        pScene = new ShaderTestScene();
        break;
    case TEST_MUTITOUCH:
        pScene = new MutiTouchTestScene();
        break;
    default:
        break;
    }


    return pScene;
}


TestController::TestController()
: m_tBeginPos(CCPointZero)//开始时预先赋值
{
    // add close menu
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(s_pPathClose, s_pPathClose, this, menu_selector(TestController::closeCallback) );//用图片创建菜单项,资源全在Resource.h内定义、第一个为点击前,第二个为点击后。
    CCMenu* pMenu =CCMenu::create(pCloseItem, NULL);//以上面的菜单项创建一个菜单
    CCSize s = CCDirector::sharedDirector()->getWinSize();


    pMenu->setPosition( CCPointZero );//设置菜单点为0
    pCloseItem->setPosition(CCPointMake( s.width - 30, s.height - 30));//在设置点为右上角


    // add menu items for tests
    m_pItemMenu = CCMenu::create();//创建一个空菜单
    for (int i = 0; i < TESTS_COUNT; ++i)//循环着把所有名字都加进菜单
    {
// #if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
//         CCLabelBMFont* label = CCLabelBMFont::create(g_aTestNames[i].c_str(),  "fonts/arial16.fnt");
// #else
        CCLabelTTF* label = CCLabelTTF::create(g_aTestNames[i].c_str(), "Arial", 24);
// #endif        
        CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestController::menuCallback));


        m_pItemMenu->addChild(pMenuItem, i + 10000);//天价菜单时,菜单项的排序
        pMenuItem->setPosition( CCPointMake( s.width / 2, (s.height - (i + 1) * LINE_SPACE) ));//设置菜单项在菜单里的位置位置为中间
    }


    m_pItemMenu->setContentSize(CCSizeMake(s.width, (TESTS_COUNT + 1) * (LINE_SPACE)));//设置菜单内容的寛高
    m_pItemMenu->setPosition(s_tCurPos);//设置菜单位置
    addChild(m_pItemMenu);//添加到布局


    setTouchEnabled(true);//设置接收触控事件


    addChild(pMenu, 1);添加关闭按钮。


}


TestController::~TestController()
{
}


void TestController::menuCallback(CCObject * pSender)
{
    // get the userdata, it's the index of the menu item clicked
    CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);//菜单项
    int nIdx = pMenuItem->getZOrder() - 10000;//获取菜单项的排序


    // create the test scene and run it
    TestScene* pScene = CreateTestScene(nIdx);//调用上边创建新场景
    if (pScene)//场景不为空
    {
        pScene->runThisTest();//运行这个场景
        pScene->release();//释放资源
    }
}


void TestController::closeCallback(CCObject * pSender)//变比程序的回调方法
{
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}


void TestController::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
    CCSetIterator it = pTouches->begin();//触控对象
    CCTouch* touch = (CCTouch*)(*it);//触控


    m_tBeginPos = touch->getLocation();    //获取触控的位置
}


void TestController::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
    CCSetIterator it = pTouches->begin();//移动时
    CCTouch* touch = (CCTouch*)(*it);//触控对象


    CCPoint touchLocation = touch->getLocation();    //获取移动到的位置
    float nMoveY = touchLocation.y - m_tBeginPos.y;//获取移动到的做表单


    CCPoint curPos  = m_pItemMenu->getPosition();//获取当前菜单
    CCPoint nextPos = ccp(curPos.x, curPos.y + nMoveY);;//获取移动了多少高度
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    if (nextPos.y < 0.0f)//如果移动到最上边就停止
    {
        m_pItemMenu->setPosition(CCPointZero);
        return;
    }


    if (nextPos.y > ((TESTS_COUNT + 1)* LINE_SPACE - winSize.height))//如果移动到最下边
    {
        m_pItemMenu->setPosition(ccp(0, ((TESTS_COUNT + 1)* LINE_SPACE - winSize.height)));
        return;
    }


    m_pItemMenu->setPosition(nextPos);//设置菜单项显示到的项
    m_tBeginPos = touchLocation;//设置触摸到的目前的位置
    s_tCurPos   = nextPos;//存储已经移动到的点

}


这个主要是学习菜单的操作,

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值