

Some cocos2d methods have block support, so you can write the code you'd want to run in-line. Pretty simple, eh? With iOS 4, Apple has also introduced the concept of "blocks" (AKA closures) to Objective-C. Get a reference to the button that was tappedĬCMenuItemFont *button = (CCMenuItemFont *)sender So this means you'll have to write a new method in the layer called "buttonOneAction" and have it do something. In the example above, the button will send a message to "self" (that is, the layer that holds the button) and call the buttonOneAction method. The second two arguments to the CCMenuItemFont constructor specify what will happen when the button is tapped. [CCMenuItemFont you create any button, you will probably want something to happen when a player taps the button. Create a button (aka "menu item"), have it execute the "buttonOneAction" method when tappedĬCMenuItemFont *buttonOne = [CCMenuItemFont Here!" target:self Specify font details However, if you want to just get something in place that actually works, then go back and create a button graphic later, this class is quite useful. This is often most useful for rapid prototyping, since in a "real" game, you'll want to make sure your button looks like it'll do something if tapped. CCMenuItemToggle is perfect for a settings menu where you want to have on/off switches for different options.ĬCMenuItemFont is the class you'll use when you want a text-only button. I'll also demonstrate CCMenuItemToggle, which is a button container that stores multiple buttons, but only one is shown (or "active") at a time. How about their contents (aka menu items)? While there are a number of different options for creating buttons in cocos2d, we're going to focus on the two most useful ones, CCMenuItemFont and CCMenuItemImage, which allow you to easily create text- or image-based buttons. Pretend that buttonOne and buttonTwo are already createdĬCMenu *myMenu = Layout options include a horizontal or vertical layout (with or without padding), as well as alignment in columns and rows. Once you create and populate the menu, you can specify the layout of the buttons. Also, you'll need to make sure you end the list of buttons you send to the constructor with nil. The constructor takes a comma-separated list of menu items that you want to store in your menu, so you'll have to make sure to create your buttons before the menu. You can create a menu just like you would any other cocos2d object. The advantages of using a menu are apparent when you have lots of buttons you need to wrangle, but it's annoying to have to use one for just one button. Well, unfortunately in cocos2d you can't just plop a button down in your scene first you have to add a menu to the scene, then add "menu items" (read: buttons) to the menu. Now say that we have two scenes and want to flip back 'n forth between them, and we'd like to use a button to do it. Take another look at the "scenes" tutorial.

So I'm going to give a brief rundown of cocos2d's buttons, and their containers (appropriately enough, called "menus"). While iOS games might not use buttons for actual gameplay, at the very least your game will have a title screen with a "start playing" button on it. Buttons are obviously a key ingredient in any game's UI. What the what? It's tutorial time again! This time the subject will be a bit smaller in scope than some of my previous entries: menus and buttons.
