Quick Cocos2dx-lua (V3.3R1) Learning notes (v)------Create a Sprite menu, let's make the simplest Click menu Display Wizard

Source: Internet
Author: User

Started the 5th note, feel the previous few write too little, quick cocos2dx front plus a quick, that is, we can quickly get started, rapid development ... Balabala

One, let's create the first sprite.

Today we are going to create the Sprite, the alien picture we showed earlier is an elf.

Quick provides us with the Display.newsprite function to create sprites.

Let's go into the Display.lua and look at the description of the function.

Wow, so long, how do I know where the function is located (there is no search, not ctrl+f search +_+).

Let's change the method, we don't have to search, after all, we want to see quicks give us the encapsulation of those functions, I just look at the function list on the line. Sublime text gives us this shortcut, shows a list of functions, let's quickly navigate to the function we want to find

The-----ctrl+r>> of <<ctrl Dafa

See, so we can see that the quick actually encapsulates those functions to us, after all, now that the quick document is not so comprehensive, we can locate the past, by the way, to see the implementation of the function, so that better use of the function

The following is a description of the function's parameters

Creates and returns a Sprite display object. --@function [parent= #display] newsprite--@param mixed image name or Spriteframe object--@param number x--@param number y--@param tab Le params--@return sprite#sprite ret (return VALUE:CC. Sprite)--@see sprite--[[--creates and returns a sprite display object. Display.newsprite () Three ways to create a display object:-Create from   picture file-Create from   cached image frame-Create   from Spriteframe object we create the function in the first way  Mainscene:ctor () display.newsprite ("Hello.png"): Align (display. CENTER, Display.cx, display.cy)   --set anchor point and picture coordinate point: addto (self)   --Adds the resulting picture sprite to the Mainscene's child node end

  


Go into the simulator and run for a look at the effect.

Show success. OK, we'll create the wizard in a second way.

The second way is to call the Plist file loaded into the wizard cache, we put a bunch of small images into a large map, generate plist file, this file contains the small image we want to use the coordinates of the large map position

As for how to package please search Texturepacker

This tool uses the technique to use Google Baidu Dafa on its own

function Mainscene:ctor () display.addspriteframes ("Lqfrolewalk.plist", "Lqfrolewalk.png") Display.newsprite ("# Lqfrightstop.png "): Align (display. Center,display.cx, display.cy): AddTo (self): Setscale (3)     --magnification 3 times times end

  

Note: Unlike the first example above, we load the file into the sprite cache, calling the Display.newsprite parameter with a # symbol, which is the # symbol that tells the function to load the wizard from the cache.

Let's look at the display effect (I admit, because my figure is too small, so I zoomed in 3 times times)

Second, create the menu originally I want to use the Menuitemfont function, the results after using, various hints nil value, go to forum Search, just know, quick now recommend with UIPushButton to create the menu, in fact, I still miss with the text menu, is not found to use the method, if there are children's shoes know, please tell, thank you well, now basically no game menu or text, we use UIPushButton this button control to do the menu Cc.ui.UIPushButton create a button for a chestnut to see
function Mainscene:ctor () Local button1 = Cc.ui.UIPushButton.new ({normal = "button01.png", pressed = " Button01pressed.png "}) button1:align (display. center,display.cx,display.cy) button1:addto (self) End

  

The menu button is created so we can look at the effect first
--@param table images Pictures of various states--@param table options parameter table where SCALE9 is the scale state value:-   normal state-   pressed pressed state-   

  

We see in the second parameter above we have a scale9 parameter, know the child shoes know this is nine Gongge, do not know the principle of Baidu to do so and the advantages of doing so, we use two pieces of code comparison can see the effect of
function Mainscene:ctor () Local button1 = Cc.ui.UIPushButton.new ({normal = "button01.png", pressed = " Button01pressed.png "}) button1:align (display. center,display.cx,display.cy) button1:addto (self) button1:setbuttonsize (400,200) End

  

We enlarge the menu size
Assi, it's ugly. &......&¥#¥@ (I can still think of the language) we will pass a nine Gongge parameter to see the effect
function Mainscene:ctor () Local button1 = Cc.ui.UIPushButton.new ({normal = "button01.png", pressed = " Button01pressed.png "},{scale9 = True}" Button1:align (display. center,display.cx,display.cy) button1:addto (self) button1:setbuttonsize (400,200) End

  

Run effect

Very straightforward to see the difference! Three, enter the theme, add a callback function to the button to show hidden pictures so much to enter the theme, my Chinese teacher will not hit me Ah! O (∩_∩) o~ (Chinese teacher: I say to you, composition, the beginning of the humorous, batch of teachers who have time to you all read Ah ... Balabala) button We showed out, now how to add processing function to the button, or light click, no effect, play a ball ah
----------------------------------Registered User Click Listen-@function [parent= #UIButton] onbuttonclicked--@param function callback Listener Function--@return Uibutton#uibutton--End--function uibutton:onbuttonclicked (callback) Self:    Addbuttonclickedeventlistener (callback) return Selfendfunction Uibutton:addbuttonpressedeventlistener (callback) Return Self:addeventlistener (Uibutton.pressed_event, callback) end--start------------------------------------ Registered users Press Listen-@function [parent= #UIButton] onbuttonpressed--@param function Callback listener functions--@return Uibutton#uibutton-en  D--function uibutton:onbuttonpressed (callback) Self:addbuttonpressedeventlistener (callback) return selfendfunction Uibutton:addbuttonreleaseeventlistener (callback) return Self:addeventlistener (Uibutton.release_event, callback) end--start------------------------------------Registered user release monitoring-@function [parent= #UIButton] onbuttonrelease--@param function callback listener functions--@return Uibutton#uibutton--End--function Uibutton:onbuttonreLease (callback) Self:addbuttonreleaseeventlistener (callback) return selfendfunction UIButton: Addbuttonstatechangedeventlistener (callback) return Self:addeventlistener (Uibutton.state_changed_event, callback) End (This is not a function of UIButton, is not UIPushButton ah?) We will see in the Uipushbutton.lua file, actually this button inherits the UIButton, so we can call UIButton functions function Mainscene:ctor () Local button1 = Cc.ui.UIPushButton.new ({normal = "button01.png", pressed = "Button01pressed.png"},{scale9 = True}) button1:align ( Display. center,display.cx,display.cy) button1:addto (self) button1:setbuttonsize (200,100) button1:onbuttonclicked (function () print ("Welcome to XXXX") print ("Hello, I am Xiao Mo") print ("I am Little Mo") end)

  




See the effect, we started to do the click button to display the picture, very simple
function Mainscene:ctor () Local sprite = Display.newsprite ("Hello.png") sprite:align (display. CENTER, Display.cx, display.cy) sprite:addto (self) sprite:setvisible (false) Local button1 = Cc.ui.UIPushButton.new ({ normal = "Button01.png", pressed = "Button01pressed.png"},{scale9 = True}) button1:align (display. CENTER,DISPLAY.CX,100) button1:addto (self) button1:setbuttonsize (200,100) Button1.showspriestate_ = 1button1: Setbuttonlabel ("Normal", cc.ui.UILabel.new ({uilabeltype = 2,text = "Show Picture", size =)) Button1:setbuttonlabel ("pressed ", cc.ui.UILabel.new ({uilabeltype = 2,text =" Show Picture ", size = ()) button1:onbuttonclicked (function () if Button1.showspriestate_ = = 1 Thenbutton1.showspriestate_ = 2sprite:setvisible (True) Button1:setbuttonlabel ("normal", Cc.ui.UILabel.new ({uilabeltype = 2,text = "Hide Picture", size = ())) Button1:setbuttonlabel ("Pressed", Cc.ui.UILabel.new ({ Uilabeltype = 2,text = "Hidden picture", size = ())) Elsebutton1.showspriestate_ = 1sprite:setvisible (false) Button1: Setbuttonlabel ("normal", Cc.ui.UILabel.New ({Uilabeltype = 2,text = "Show Picture", size = ())) Button1:setbuttonlabel ("Pressed", cc.ui.UILabel.new ({uilabeltype = 2, Text = "Show Picture", size = ()) endend) end

  


Quick Cocos2dx-lua (V3.3R1) Learning notes (v)------Create a Sprite menu, let's make the simplest Click menu Display Wizard

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.