Quick-cocos2d-x Study Notes [5] -- create menu, quickcocos2d

Source: Internet
Author: User

Quick-cocos2d-x Study Notes [5] -- create menu, quickcocos2d

Menus are also an indispensable element in the game. There are two types of menuItem encapsulation in quick: Picture menus and text menus.


I. Image menu ui. newImageMenuItem (params)

Available parameters:

  • Image: normal button image
  • ImageSelected: The image when the button is pressed (optional)
  • ImageDisabled: The image when the button is disabled (optional)
  • Listener: callback function
  • Tag: The Tag of the button, which is passed into the callback function. When multiple buttons use the same callback function, you can identify which button is pressed by Tag (optional)
  • X, y: coordinates (optional)
  • Sound: Specifies the audio effect to play when the button is pressed (optional)
The parameter name of params cannot be written incorrectly. It is the same as the label in the preceding section. Therefore, you need to repeat it several times. Tags are used together with multiple items to share one callback function. Therefore, if you write a function separately, remember to have a tag parameter.


Simply write an image button

local item1 = ui.newImageMenuItem({    image = "CloseNormal.png",    imageSelected = "CloseSelected.png",    listener = onClicked,    x = display.cx,    y = display.height*0.7,    tag = 1})

Ii. Text menu ui. newTTFLabelMenuItem (params) 

In addition to some basic menuitem parameters, you can also use parameters in ui. newTTFLabel (), such as text content and size.

Write another text menu

local item2 = ui.newTTFLabelMenuItem({    text = "MenuItem",    size = 50,    aligh = ui.TEXT_ALIGN_CENTER,    listener = onClicked,    x = display.cx,    y = display.height*0.3,    tag = 2})

 

Like Cocos2dx, we still need a Menu manager to manage these menuitems. If we use the original lua method, we need addChild to reencapsulate the menu here for every item, it is much easier to use the same method as c ++.

local menu = ui.newMenu({item1, item2})self:addChild(menu)

This completes the addition. We haven't mentioned the callback function yet. Let's look at it again.

local function onClicked(tag)    if tag == 1 then    print("item1 clicked")    elseif tag == 2 then    print("item2 clicked")    endend


Because this function is local, it must be placed before menuItem, like C. Otherwise, the program will not recognize this function. Of course, you can also write the callback function directly inside the listener and create another item,

local item3 = ui.newTTFLabelMenuItem({    text = "MenuItem2",    size = 30,    aligh = ui.TEXT_ALIGN_CENTER,    listener = function ()    print("item3 clicked")    end,    x = display.cx,    y = display.cy,})

Is it so easy! The basic usage is as follows. Finally, a complete code and effect are provided.

function MyScene:ctor()local function onClicked(tag)    if tag == 1 then    print("item1 clicked")    elseif tag == 2 then    print("item2 clicked")    endendlocal item1 = ui.newImageMenuItem({    image = "CloseNormal.png",    imageSelected = "CloseSelected.png",    listener = onClicked,    x = display.cx,    y = display.height*0.7,    tag = 1})local item2 = ui.newTTFLabelMenuItem({   text = "MenuItem",   size = 50,   aligh = ui.TEXT_ALIGN_CENTER,    listener = onClicked,    x = display.cx,    y = display.height*0.3,    tag = 2})local item3 = ui.newTTFLabelMenuItem({   text = "MenuItem2",   size = 30,   aligh = ui.TEXT_ALIGN_CENTER,    listener = function ()    print("item3 clicked")    end,    x = display.cx,    y = display.cy,})local menu = ui.newMenu({item1, item2, item3})self:addChild(menu)end

The effect is as follows,




How to open cs from the CS folder (that is, not from the shortcut-desktop, Start Menu ...... [Add 510 points after solution]

In the CS folder, there are usually several people (gray people with guns, but few people forgot) who see their suffixes as one is. if exe does not have this suffix, you can remove the Suffix in the tool ------- folder option -------- before hiding the option of the known file type extension.


Related Article

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.