This class is the main class of the game. The MainMenu of our game BubbleBurst look like For click event of these three item, three function are added for handling the click event. And added to the layer for displaying on the screen. Init() function will create a menu with 3 menu items( CCMenuItemFont). Scene() function will create a scene and return the pointer to the director.Īnd MainMenu::create() will create a MainMenu layer and you want to add it to the scene. Void MainMenu::onOptions(CCObject* pSender) Void MainMenu::onNewGame(CCObject* pSender) combine to form a menu and allign VerticallyĬCMenu* menu = CCMenu::create( item1, item2, item3, NULL ) on “init” you need to initialize your instanceĬCMenuItemFont* item1 = CCMenuItemFont::create( “New Game”, this, menu_selector(MainMenu::onNewGame) ) ĬCMenuItemFont* item2 = CCMenuItemFont::create( “Options”, this, menu_selector(MainMenu::onOptions) ) ĬCMenuItemFont* item3 = CCMenuItemFont::create( “Quit”, this, menu_selector(MainMenu::onQuit) ) If you remember this is exactly similar to the HelloWorldScene class which is created automatically by the cocos2dx template.įollowing is the contents of MainmenuScene.cpp And assigned three function for 3 buttons in the menu as designed by us in the beginning of this chapter. We had created MainMenu layer from CCLayer. implement the “static node()” method manually there’s no ‘id’ in cpp, so we recommand to return the exactly class pointer Method ‘init’ in cocos2d-x returns bool, instead of returning ‘id’ in cocos2d-iphone Create 2 files MainmenuScene.h and MainmenuScene.cpp.ĭownload the code here for this tutorial.įollowing is the contents of MainmenuScene.h For this we have to create a main menu layer and attach it to a main menu scene. We can set the window title by function setViewName.I hope this helps in your code development. I’ve been able to test it for 12 items, 6 per page but if you want a custom amount (say 2-4 per page) you must adjust the numbers in the code above to match your expectations. The page gets added to the pageArray (a list of pages) which are used by the scroller object.įinally, we create a clipping node and add the scroller as a child of the clipping node’s object before we add the clipping node to self (or it could be a layer). Each page has it menu aligned to 3 columns. Viewport *cn = initWithRect:CGRectMake(75,75, 300, 180)] īecause we need a grid of 6 items, the `count` variable must reach 6 before “creating” a new page. Self.scroller = initWithLayers:pageArray widthOffset:250] autorelease] Now create the scroller and pass-in the pages (set widthOffset to 0 for fullscreen pages) NSString *fileName = ĪvtButton = ĬCMenuItemSprite *btnWG = We want a grid of 3x2, this means count must List would be your array of custom objects ie: Apples or whatever It may not necessarily work for you, but should give you some indications of what I did. I used Viewport rather than ClippingNode because I couldn’t quite get it to work, whereas I found Viewport used similar code and seemed to work a bit more straightforwardly.Īnyway, my code now follows. + (Optional) CCMenuAdvanced (See: cocos2d-iphone-extensions) + Viewport () or ClippingNode (via cocos2d website) + CCScrollLayer (part of the cocos2d-iphone-extensions in github) + cocos2d-iphone v2.x (I did not test for 1.x) ( cocos2d website) Imagine we have a modal pop-up window, or something where we do not want the visuals to “spill over” a certain boundary this is what we use the clipping node for.įor all of this to work, you will need the following: By a clipping node I mean something which clips the viewport in which the grid appears in. To make this work, I would need to use a CCScrollLayer to paginate between each page, each having 3×2 items on them.įurthermore, I need to put them in a clipping node. This is a menu of 12 items displayed in a grid of 3×2, that is 6 items per page. Here is a visual representation from my game, In a cocos2d iOS game I am currently writing, one of the things I wanted was to display a menu of items using a grid style, display the said menu in a scrollview so I would have multiple items per page, and then finally - put the items in a clipping node.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |