Forum > Extensions > Slider Menu Grid

Slider Menu Grid

By starfair Posted 2012-03-30 04:46 Comments 27
starfair
  • Posts: 25

Hello!
I copied the source code ported from cocos2D for iphone (http://brandonreynolds.com/blog/2011/01/09/cocos2d-sliding-menu-grid/), based on the source code on thread page K pop(http://www.cocos2d-x.org/boards/6/topics/9803). I corrected several errors and added a few features (tags for each page)

SlidingMenu.zip (3.4 kB)

SlidigMenu.zip (3.8 kB)

emmyc
  • Posts: 73
  • Location: US

#1 RE: 2012-03-31 05:08

wow. this is what I am looking for. got to try it out.

thanks

enjoy work!

starfair
  • Posts: 25

#2 RE: 2012-04-02 05:53

I hope you find useful, Emmy :)

New version! I corrected some errors and changed the logic of adding elements of the array. The first items are added to the first page

emmyc
  • Posts: 73
  • Location: US

#3 RE: 2012-04-04 09:02

Hi Vitaly Shumakov:

I tried Slider Menu Grid. It works very well. Thank you very much providing this wonderful code. I am wondering can you make it as a loop? if I slide menu to the last page if I continue slide the page , can you make it go to first page. same as the first page, if I continue slide the screen, can you make it go to the last page?

Thanks again

Emmy

enjoy work!

starfair
  • Posts: 25

#4 RE: 2012-04-04 10:42

The fact is that at the moment, the transition is carried out using the following code:

void SlidingMenuGrid::moveToCurrentPage()
{
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    // Perform the action
    CCEaseBounce* action =CCEaseBounce::actionWithAction(CCMoveTo::actionWithDuration(fAnimSpeed*0.3f, GetPositionOfCurrentPage()));
    runAction(action);
}

If you simply navigate around the pages, then this code will scroll through all the pages are located between the first and last pages. To loop, I need to change the whole scheme of this piece of software. Of course, I think, but nothing yet I promise.

kbwave
  • Posts: 21
  • Location: S.korea

#5 RE: 2012-04-13 05:59

i fixed it :) buildGrid buildGridVertical

void SlidingMenuGrid::buildGrid(int cols, int rows)
{
CCSize winSize = CCDirector::sharedDirector()~~>getWinSize;
int col = 0, row = 0;
CCArray* child = this~~>getChildren();
CCObject* item;
CCARRAY_FOREACH(child, item)
{
CCMenuItemSprite* getItem = (CCMenuItemSprite**) item;
getItem~~>setPosition.x + col * padding.x + , this~~>getPosition.y - row** padding.y));
// getItem~~>setPosition.x+winSize.width/2,~~winSize.height/2));
**col;
if
{
col = 0;
**row;

if( row == rows )
{
iPageCount**;
CCLog;
col = 0;
row = 0;
}
}
}
if > rows*cols*iPageCount) <— add code for FIX
{
iPageCount**;+
}

}

  1. example there is horisontal menugrid of cols 5 rows 2 allItem count is 22

1 2 3 4 5 11 12 13 14 15 21 22
6 7 8 9 10 16 17 18 19 20

in previous code iPageCount is 2 (must be 3)

so i fix this bug have nice day :) if you use my SlidingMenuGrid please add my Nickname by comment haha

starfair
  • Posts: 25

#6 RE: 2012-04-19 07:46

Fixed a bug where a locked item indicated by the touch as the chosen.
Fixed a bug page counter by Mr. K pop (previous message)
Added the ability to be used as a descriptor page of any object pointer CCNode *
P.S. Mr. K pop, your nickname add everywhere in the code ;)

SlidingMenu (v1.2).zip - Fix a bugs (4 kB)

emmyc
  • Posts: 73
  • Location: US

#7 RE: 2012-05-02 11:34

Thanks for the bug fixing. Good job. I had to work around with this bug. I am so happy that it got fixed.

enjoy work!

starfair
  • Posts: 25

#8 RE: 2012-05-02 17:19

Excellent, Emmy :)

prchakal
  • Posts: 125

#9 RE: 2012-06-19 17:40

Hi,

How to use it?

secretarts
  • Posts: 16

#10 RE: 2012-07-13 15:34

I was super excited to see this class out here, since I think it is pretty close in function to Cocos2d-extension-iPhone’s CCScrollLayer. Thanks Vitaly Shumakov for writing it!

Sadly, it looks like I’m going to have to adjust for the changes in the latest build of Cocos2d-x 2.0 (no more CCMutableArray, etc.).

Has anybody started on such an adjustment? I had thought at first it would be pretty straightforward but as I’ve gone on with it it looks more complicated than I had expected.

prchakal
  • Posts: 125

#11 RE: 2012-07-13 15:47

Already have SliderMenu converted to Cocos2d-2.0:

https://github.com/prsolucoes/cocos2d-x

secretarts
  • Posts: 16

#12 RE: 2012-07-13 18:56

Paulo Coutinho wrote:

Already have SliderMenu converted to Cocos2d-2.0:
>
https://github.com/prsolucoes/cocos2d-x

Incredible! Thanks Paulo Coutinho!

dean_n91
  • Posts: 15
  • Location: England

#13 RE: 2012-10-30 17:05

Hi all,

I’m trying to use this sliding menu for my game and everything is working great. The only problem is that i cannot let the program know which button in the grid has been pressed. e.g If level 4 has been selected i’d like a function to return the value 4.

I’m trying to use the getTag() function but it always seems to return “–1”

Any suggestions?

`void LevelMenu::menuLevels(CCObject* pSender)
{
CCScene* levelSelect = HelloWorld::scene();

gameConfig.levelSelected = slidingMenu->getTag();  // not working :(
CCMenuItem test1 = slidingMenu->getSelectedItem(); //just trying to find the id
CCDirector::sharedDirector()->replaceScene(CCTransitionFade::transitionWithDuration(1.0f,levelSelect));

}`

starfair
  • Posts: 25

#14 RE: 2012-10-30 17:28

Hi Dean!
Before using getTag () you have to set the ID of the tag using the setTag (). –1 means that the identifier for this menu item is not set.

dean_n91
  • Posts: 15
  • Location: England

#15 RE: 2012-10-30 17:41

Thank you very much for the quick reply Vitaly, it’s worked a treat! :D

elr0nd
  • Posts: 43

#16 RE: 2012-12-31 11:50

Hi,

Has someone used this sliding menu in cocos2d-2.0-x-2.0.4? I was able to show in the screen, it slides between pages but when a menu item is clicked the callback function is not called. I was debugging the code and I found that the “error” (I do not know if it is an error or something that I am not doing right) is in SlidingMenuGrid::GetItemWithinTouch method, the local ccpoint and the rect generated do not match…

Do you have also this error?

Thank you for your answers[]()!

prchakal
  • Posts: 125

#17 RE: 2013-05-01 06:44

Hi,

I need it to last cocos2d-x version, anyone have it working with last version?

Thanks.

petrc
  • Posts: 4

#18 RE: 2013-05-28 10:39

I updated the code to work on 2.0 (2.0.3), and I also added the option to set threshold in the constructor.

SlidingMenu.zip (4.9 kB)

Copy
  • Posts: 4

#19 RE: 2013-05-31 14:57

Thanks for this class! very useful!
I just have a question and a suggestion:
Can you give us an example on how to use it?
Also, more comments in cpp file would be much appreciated. Sorry, I come from a school where code comments were mandatory for every line.

Is this integrated in cocos2dx now or not?

-Copy

kcharubin@gmail.com
  • Posts: 7
  • Location: Poland

#20 RE: 2013-06-02 04:46

Example code, assets from HelloWorld cocos2dx project, put it in your scene HelloWorld constructor or logic init function.

    CCArray* menuArray = CCArray::createWithCapacity(24);

    CCSize windowSize = CCDirector::sharedDirector()->getOpenGLView()->getDesignResolutionSize();



    float w= 0;
    float h=0;
    for (int i = 0;i<24;i++)
    {
         CCSprite* spr = CCSprite::create("CloseNormal.png");
        CCSprite* selspr = CCSprite::create("CloseSelected.png");
         CCSprite* dis = CCSprite::create(""CloseSelected.png");

        CCMenuItemSprite* itmspr = CCMenuItemSprite::create(spr, selspr,dis, this, menu_selector(HelloWorld::menuItemCallback));
        itmspr->setTag(i);

        if (i > 3) itmspr->setEnabled(false);

        CCLOG ("tag for item %d" , itmspr->getTag());


        menuArray->addObject(itmspr);



    }
    int row = 3;
    int col = 4;
    CCPoint p = ccp(80,80);

//some trick to center menu 
    float eWidth =  (col-1)*(w+p.x);
    float eHeight = (row-1)*(h+p.y);

    CCPoint menuPosition = ccp(
                               windowSize.width/2.0f -eWidth/2.0f ,
                               windowSize.height/2.0f -eHeight/2.0f
                               );

    SlidingMenuGrid* sliderMenu = SlidingMenuGrid::menuWithArray(
                                                                 menuArray,
                                                                 col,
                                                                 row,
                                                                 menuPosition,
                                                                 p
                                                                 );

    sliderMenu->setAnchorPoint(ccp(0.5, 0.5));
//    sliderMenu->setPosition(100,100);


    this->addChild(sliderMenu);

Here goes the menu selector function:

void HelloWorld::menuItemCallback(CCObject* pSender)
{
    CCLOG ("Item CLICKED: %d",  ((CCMenuItemSprite*)pSender)->getTag());


}
Copy
  • Posts: 4

#21 RE: 2013-06-18 16:09

Thanks!
one small note: w and h variables are unused, stay at value 0, any purpose for them in mind?

-Copy

kcharubin@gmail.com
  • Posts: 7
  • Location: Poland

#22 RE: 2013-06-18 18:02

Joe Merhej wrote:

Thanks!
one small note: w and h variables are unused, stay at value 0, any purpose for them in mind?

This menu was a part of bigger tab menu. Tab menu wes created dynamically and sometimes i had to slightly tighten items to change position, so I calculeted w and h corrections.
You can just skip it :)

dijkstra
  • Posts: 5

#23 RE: 2013-07-09 05:58

this could be lame, but i new to cocos2dx and c++,
I added the

`CCArray* menuArray = CCArray::createWithCapacity(24);

CCSize windowSize = CCDirector::sharedDirector()->getOpenGLView()->getDesignResolutionSize();

float w= 0;
float h=0;
for (int i = 0;i<24;i++)
{
     CCSprite* spr = CCSprite::create("CloseNormal.png");
    CCSprite* selspr = CCSprite::create("CloseSelected.png");
     CCSprite* dis = CCSprite::create(""CloseSelected.png");

... `

and the menu selector function to the helloworldScene.cpp

but when compiled it gives me

@
jni/../../Classes/HelloWorldScene.cpp: In member function ‘virtual bool HelloWorld::init()’:
jni/../../Classes/HelloWorldScene.cpp:121:5: error: ‘SlidingMenuGrid’ was not declared in this scope
jni/../../Classes/HelloWorldScene.cpp:121:22: error: ‘sliderMenu’ was not declared in this scope
jni/../../Classes/HelloWorldScene.cpp:121:35: error: ‘SlidingMenuGrid’ is not a class or namespace

@

error..

what do i do to make SlidingMenuGrid public??

dijkstra
  • Posts: 5

#24 RE: 2013-07-09 07:21

I have also included the header file SlidingMenu.h in the HelloWorldScene.cpp

It still gives me

/game_shared/__/__/Classes/HelloWorldScene.o: in function HelloWorld::init():jni/../../Classes/HelloWorldScene.cpp:130: error: undefined reference to 'SlidingMenuGrid::menuWithArray(cocos2d::CCArray*, int, int, cocos2d::CCPoint, cocos2d::CCPoint)'
collect2: ld returned 1 exit status

Please help!

kcharubin@gmail.com
  • Posts: 7
  • Location: Poland

#25 RE: 2013-07-09 07:36

please check any tutorial how to create my first cocos2dx andorid application :)
TIP:
Probably you are not compilling SlidingMenu.cpp , you have to edit your make build script and add this file.
As far as I remember it is located in android/jni/helloworld/Android.mk, update your LOCAL_SRC_FILES definition to include the newly created cpp files.

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps