![]() In the same file (HelloWorldScene.m), replace the init method with the following code:ĬGSize winSize = winSize] ĭimensions:CGSizeMake(320, 50) fontSize:32.0] retain] Then before we forget go to HelloWorldScene.m and add the cleanup code to the dealloc method: Open up HelloWorldScene.h under Classes and add a member variable to the HelloWorld class, we’ll need this in a bit: Once you have the images, drag them into your resources folder and make sure “Copy items into destination group’s folder (if needed)” is checked. So let’s give it a shot and try to create a simple button the Cocos2D way! Creating A Simple ButtonĬreate a new project in XCode using the cocos2d Application template, and name your project “CCButtons.” Next you need some images of buttons to work with – you can either use your own, or download some button images I have created. Menu items can be either text or images, and the menu system contains logic for arranging menu items, highlighting items when they are tapped, toggling items, and more. The Cocos2D menu system consists of a menu that has a number of menu items inside it. Although this is definitely possible, there’s a much easier way to create buttons in Cocos2D – by using the Cocos2D menu system. When I first started trying to add a button with Cocos2D, I thought the best way to was to just create a sprite representing the button and check for when the button was tapped. This Cocos2D buttons tutorial assumes that you’ve already gone through the tutorial on how to create a simple game with Cocos2D, or have equivalent knowledge. This Cocos2D buttons tutorial will show you how to create buttons in Cocos2D step by step, starting with simple buttons and then covering toggle and radio buttons too. For click event of these three item, three function are added for handling the click event.When you are making a game in Cocos2D, likely one of the first things you’ll find yourself needing is buttons. 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |