官术网_书友最值得收藏!

Adding buttons with CCMenu

Buttons are the primary way to navigate between different scenes. In this section, we will take a look at how to add buttons to the scene.

Getting ready

Buttons are also similar to regular sprites. In the case of buttons, we need two images. One image is shown when the button is normal, which means that it is not pressed, and the other image is shown when the button is pressed.

For this section, the resources are provided in the resources folder of this chapter. There are four images in total for the iPad and iPad HD—two for normal mode and two for button pressed. Copy these four files in the Resource/Publishes-iOS folder on your system.

Also, add the following code at the top of the MenuScene.m file to import the Cocos2d-ui.h file. This is required to add the buttons and layout classes:

#import "cocos2d-ui.h"

How to do it…

Right after where we added the outline in the previous section, we will add the following code:

CCButton *playBtn = [CCButton buttonWithTitle:nil

  spriteFrame:[CCSpriteFrame 
  frameWithImageNamed:@"playBtn_normal.png"]
  highlightedSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"playBtn_pressed.png"]

disabledSpriteFrame:nil];

To create buttons, we will have to use the CCButton class. This will take four parameters: the title of the button, a spriteFrame parameter for the normal mode, a second sprite frame for the highlighted or pressed mode, and a third sprite frame for the disabled mode.

For this example, we will just pass in the image for the normal and pressed modes.

If we run the project now, we will not see anything on the screen; this is because we have not yet added the buttons to the scene. To add buttons to a scene, we need to first add them to a button menu of the CCLayout type and then to the scene.

So, let's create a new variable of the CCLayout type next, as follows:

        CCLayoutBox * btnMenu;
        btnMenu = [[CCLayoutBox alloc] init];
        btnMenu.anchorPoint = ccp(0.5f, 0.5f);
        btnMenu.position = CGPointMake(winSize.width/2,
          winSize.height * 0.5);
        [btnMenu addChild:playBtn];

        [self addChild:btnMenu];

We will create a new variable called btnMenu of the CCLayout type and then allocate and initiate the variable. Next, we will set the anchor point to center because all the buttons will be anchored to the lower-left of the layout otherwise. The layout is then placed at the center of the screen.

Finally, we will add playBtn as a child of btnMenu and then add btnMenu itself to the scene.

Now, we can see the button displayed on the scene. Also, if we press the button, it will change to the pressed image.

For the button to do something, we need to call a function once the button is pressed and released.

We will add the following highlighted code right after the playBtn variable:

CCButton *playBtn = [CCButton buttonWithTitle:nil
  spriteFrame:[CCSpriteFrame frameWithImageNamed:@"playBtn_normal.png"]
  highlightedSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"playBtn_pressed.png"]
  disabledSpriteFrame:nil];


[playBtn setTarget:self selector:@selector(playBtnPressed:)];

Next, we will add the following code, which will get called when the button is pressed and released:

-(void)playBtnPressed:(id)sender{

  CCLOG(@"play button pressed");

}

How it works…

We will assign the playBtnPressed function to playBtn. When the button is pressed and released, the function will get called. As of now, we will only log out a text that will tell us that the button is pressed in the console.

Press and release the play button to the see the console light up with the following text:

主站蜘蛛池模板: 青州市| 深泽县| 明溪县| 通化县| 松阳县| 井冈山市| 镇康县| 博乐市| 托里县| 定边县| 黔西县| 尉氏县| 赫章县| 罗田县| 高平市| 那坡县| 嘉善县| 竹北市| 屏山县| 衡南县| 阿鲁科尔沁旗| 东明县| 孟连| 宁化县| 咸丰县| 西畴县| 南投县| 杭锦旗| 岳池县| 海口市| 石景山区| 济宁市| 呈贡县| 繁峙县| 泗洪县| 正阳县| 和静县| 潞西市| 渝中区| 丽江市| 南阳市|