Quick-cocos2d-x Study Notes [6] -- make a custom effect button menu, quickcocos2d

Source: Internet
Author: User

Quick-cocos2d-x Study Notes [6] -- make a custom effect button menu, quickcocos2d

As mentioned above, the basic menu is easy to use. However, buttons we often see in commercial products are very nice, and the animation effect is great. Candy Crash has been playing. Look at the buttons. They are really like jelly, and the effect is really good. So let's also do it. Of course it is not the effect, in addition, there is a jitter effect after clicking. Like this,



Now, let's get started. I also moved the button code in coinflip to learn about it.

Create a folder named views and create a MyButton. lua file. This is our custom button class. The implementation principle is relatively simple, In the ui. newImageMenuItem table parameters include images, callback functions, and other parameters. Therefore, after passing these parameters, we can modify some items internally. Quick encapsulates the image button once, and we can encapsulate it ourselves.

local MyButton = {}function MyButton.new(params)local button = nilbutton = ui.newImageMenuItem(params)return buttonendreturn MyButton

This encapsulates the ui. newImageMenuItem in quick. The following is to capture the params parameter, process the effect, and then pass it to the parameter in ui. newImageMenuItem.

function MyButton.new(params)local button = nillocal listener = params.listenerparams.listener = function (tag)                --do things       listener(tag)        endbutton = ui.newImageMenuItem(params)return buttonend

In the above Code, we first save the passed callback function with the listener variable, and then process some things in the listener parameter. Finally, remember to call the previously passed callback function. So no effect code has been added, so you can see it more clearly later.


Next, let's add the previously displayed results.

Params. listener = function (tag) local function zoom1 (offset, time, onComplete) local x, y = button: getPosition () local size = button: getContentSize () local scaleX = button: getScaleX () * (size. width + offset)/size. width local scaleY = button: getScaleY () * (size. height-offset)/size. height transition. moveTo (button, {y = y-offset, time = time}) transition. scaleTo (button, {scaleX = scaleX, scaleY = scaleY, time = time, onComplete = onComplete,}) end local function zoom2 (offset, time, onComplete) local x, y = button: getPosition () local size = button: getContentSize () transition. moveTo (button, {y = y + offset, time = time/2}) transition. scaleTo (button, {scaleX = 1.0, scaleY = 1.0, time = time, onComplete = onComplete,}) end button: getParent (): setEnabled (false) -- disable the function zoom1 (40, 0.08, function () zoom2 (40, 0.09, function () zoom1 (20, 0.10, function () zoom2 (20, function () zoom2 (20, 0.11, function () button: getParent (): setEnabled (true) listener (tag) end

Zoom1 and zoom2 are two effect functions, mainly move and scale. before performing the effect, disable the menu function of the parent class, this prevents the menu item from being clicked by the player before it completes the action. After executing a series of scaling effects, enable the menu function and finally execute the previous callback function, such an animation button is released.


Put it in the menu to test,

Local Button = import (".. views. myButton ") -- import Buttonlocal MyScene = class (" MyScene ", function () return display. newScene ("myscene") end) function MyScene: ctor () local button = Button. new ({image = "icon.png", listener = function () print ("click") end, x = display. cx, y = display. cy}) local menu = ui. newMenu ({button}) self: addChild (menu) endreturn MyScene

In this way, you can add them and put them in the menu Manager together with other buttons in quick encapsulation.

All right, you can do what you like.




Java jdk6 Study Notes-complete Chinese Simplified Version

Has been, 765747369@qq.com

How to customize the buttons in the right-click menu in the workspace form in vc ++ 60

I also encountered a VC6 Bug several times.
You can add/* to the View header file and mark it as a comment before deleting it;
Or,
Delete the header file in FileView and add it again;
Furthermore,
You can press enter to save it anywhere (but sometimes it doesn't seem to work ).


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.