how to implement scrollView in cocos2d-x v3 using c++ (should work on iOS and Android)

  • abhishek
    Likes 0

    Problem Description

    I want to implement a scrollView in my game that should scroll horizontally. In this scrollView, I want to add button images that will launch different games.
    So can you please help me with the code to setup a scrollView and add it to a scene.

    I tried searching on Google, but couldnt really find a solution that clearly explains how this can be done.
    Any kind of help will be appreciated.

    Thanks

  • Sonar Systems admin
    Likes 0

  • abhishek
    Likes 0

    Hi Sonar Systems,
    Thank you so much for your quick reply.
    I did check out the abovementioned link and implemented the code for ScrollView.
    But I am not getting anything on the screen at all.

    Here is the code:

    i added this on the top of the .cpp file:
    ----------------------------------------------------------------------------------------------------
    #include "extensions/cocos-ext.h"
    #include "ui/CocosGUI.h"

    USING_NS_CC_EXT;
    USING_NS_CC;
    using namespace ui;
    ----------------------------------------------------------------------------------------------------

     

    //create menu
    //--------------------------------------------------
    menu = Menu::create();
    menu->setPosition(Point(0, 0));
    //this->addChild(menu);
        
    bn_play = MenuItemImage::create("bn_prev.png", "bn_prev.png",CC_CALLBACK_0(SelectionScene::bn_play_tapped, this));
    bn_play->setPosition(bn_play->getBoundingBox().size.width/1.5, visibleSize.height - bn_play->getBoundingBox().size.height/1.5);
    menu->addChild(bn_play);    

    bn_more = MenuItemImage::create("bn_prev.png", "bn_prev.png",CC_CALLBACK_0(SelectionScene::bn_more_tapped, this));
    bn_more->setPosition(bn_more->getBoundingBox().size.width/1.5, visibleSize.height - bn_more->getBoundingBox().size.height/1.5);
    menu->addChild(bn_more);
    //--------------------------------------------------

    //create scrollView
    //--------------------------------------------------
    ui::ScrollView *scrollView = ui::ScrollView::create();
    scrollView->setDirection(SCROLLVIEW_DIR_HORIZONTAL);
    scrollView->setContentSize(Size(800.0, 600.0));
    scrollView->setTouchEnabled(true);
    scrollView->setBounceEnabled(true);

    //set bg image for scrollView
    scrollView->setBackGroundImage("mainMenu/bg_mainMenu.png");

    //total scroll view size
    scrollView->setInnerContainerSize(Size(800, 600));

    scrollView->setAnchorPoint(Point(0.5, 0.5));
    scrollView->addChild(menu);
    scrollView->addChild(bg);

    this->addChild(scrollView);
    //--------------------------------------------------

    so when i run the build, i simply get a black screen.
    the setBackGroundImage does not show any image on screen.
    am i doing something wrong?

    and i am not able to use this line (because the code does not recognize Vec2) 
    scrollView->setAnchorPoint( Vec2( 0.5, 0.5 ) );


  • Sonar Systems admin
    Likes 0

    how big is your main menu image?

  • abhishek
    Likes 0

    its a bg image..
    so as you said in your c++ version tutorials..
    it will either be:
    2272 x 1536
    1136 x 768
    568 x 384

    so in short, its the bg image that will cover up the screen

  • Sonar Systems admin
    Likes 0

    Do you get any errors/warnings in the console saying it couldn’t load any of the images?

  • abhishek
    Likes 0

    can i send you a sample source code for the project?
    will you be able to check that and try to figure out whats wrong?

  • Sonar Systems admin
    Likes 0

    We are very busy so it’s not very practicle but im sure we can figure it out on here

  • abhishek
    Likes 0

    sure..
    please do try and help me out here if possible
    thanks

  • Sonar Systems admin
    Likes 0

    Try swiping on the screen to see if you can see anything coming in from the sides.

  • abhishek
    Likes 0

    i tried swiping on the screen, but nothing happens.
    and also wanted to ask you, we do have to add the scrollView to the scene like this right?

    this->addChild(scrollView); 

  • abhishek
    Likes 0

    Hi,
    I already checked the abovementioned link before, but it didnt help in any way.
    I am not able to implement a scrollView yet.

    I know you guys are busy, but if and when you have time, can you please make a scrollView tutorial for C++ version of cocos2d-x?
    You have a tutorial for the java version, so I would request you to make a video tutorial for C++ also if possible.

    Thank you for your time.
    :)    

  • Sonar Systems admin
    Likes 0

    Have you watched the JS version and compared as that may help.

  • barabasandras1
    Likes 0

    Dude it won’t work if you don’t add the scrollview node to your layer..

    this->addChild(scrollViewPointer);


  • abhishek
    Likes 1

    ok i will try it out..
    thanks for your suggestion..
    are you into cocos2d-x as well?
    i would like to connect with you as i did with sonar systems
    :)

Login to reply