Cocos2d menu Tutorial: Part 3 (end)

Source: Internet
Author: User

Original article link: http://www.iphonegametutorials.com/2010/09/07/cocos2d-menu-tutorial-part-3/

 

Now, we have made a lotArticle. Maybe you will ask, what else can we do? Be patient, my friend! Today, we will contact the image of menu item. Looking forward to it? Haha

Honestly, if you don't have image resources, you can't do anything well. You really need some tutorials to teach you how to customize images for menus.

The complete source code of this tutorial is provided here.

Well, what should we do this time? The following is today's calendar:

    1. Add a baselayer as the background for all existing layers.
    2. Add the "on" and "off" statuses to the "Start game" and "Credits" buttons. That is, the normal and selected statuses.

Okay. Let's get started!

We will complete the work on the basis of the previous tutorial, so make sure you have the project of the previous tutorial. First, create a "baselayer". h and. M file. Now this is a piece of cake for you. However, don't be so careful. Remember to right-click the "classes" group and select "add" and then "New fild".

After adding, first open baselayer. h:

# Import"Cocos2d. h"
@ Interface baselayer: cclayer {

}

@ End

Then modify the baselayer. M file:

 # Import  "  Baselayer. h  "  

@ Implementation baselayer
- (ID) Init {
Self = [Super init];
If (Nil = Self ){
Return Nil;
}

Self. istouchenabled = Yes;

Ccsprite * BG = [Ccsprite spritewithfile: @" Backgroud.png " ];
BG. Position = CCP ( 160 , 240 );
[Self addchild: bg z: 0 ];

Return Self;
}
@ End

 

You can see that “background.png is used to create the genie.CodeTo work, you need to add the background image to the project. Right-click the "Resources" group, select add, and click "existing files". Then, select the PNG file you need from the pop-up dialog box, as shown in:

 

If the file is not in the actual resources directory, check "copy ..." Option to copy the file. If it is already under the resources directory, it will not be used.

Then, we import baselayer in each layer.

Menulayer. h:

# Import"Baselayer. h"
@ Interface menulayer: baselayer {

Playlayer. h:

 
# Import"Baselayer. h"
@ Interface playlayer: baselayer {

Creditslayer. h:

 
# Import"Baselayer. h"
@ Interface creditslayer: baselayer {

 

After you finish, you will see the following picture in the background of each layer: (credits, menu, and play screen)

 

Well, now we have a background. What about the menu button?

We need to create them first. You can use Photoshop or fireworks to complete them, but today, this method is a bit outdated. Today, I will introduce you to a tool for creating button images online:

Button Generator

I used the above site to create four images. Each menu item corresponds to two images: (two statuses)

    • Newgamebtn.png
    • Newgamebtn_over.png
    • Creditsbtn.png
    • Creditsbtn_over.png

One is the image displayed when the button is idle, and the other is the state in which you hold down the menu item. We will upload these four images to the resourcefolder, just like the background.png image.

Therefore, we have images in our project. What should we do next?

First, find the menulayer. M file and extract startnew from the text format:

Ccmenuitemfont*Startnew=[Ccmenuitemfont itemfromstring:@"New game"Target: Self selector: @ selector (onnewgame :)];

Change to image format:

Ccmenuitemimage*Startnew=[Ccmenuitemimage itemfromnormalimage:@"Newgamebtn.png"Selectedimage:@"Newgamebtn_over.png"Disabledimage:@"Newgamebtn.png"Target: Self selector: @ selector (onnewgame :)];

 

Disabledimage is optional. If you have a disabled image, you can enter a nil for others. However, here I will show you how to use it.

 

Compile and run the code! :)

See the next tutorial!

PS: If you have any comments or suggestions, please leave a message below. Thank you!

 

Copyright statement: This article consistsHttp://www.cnblogs.com/andyqueTranslation. You are welcome to share it with me. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.