Cocos2d-x Lua Learning Notes Popup

Source: Internet
Author: User

The implementation of the game needs to pop up the message at the bottom of the screen popup window, after a certain time fade away. When a new message is prompted, the pop-up window that did not disappear will move up and continue to fade until it disappears.

Reprint statement: http://blog.csdn.net/operhero1990/article/details/47165365

1, the use of Cocostudio to establish a simple window interface, named Hintframe


Set the opacity of the black Bottom to 100/255, which gives you a mask effect. Two labels display the message title and content, respectively.

2. Create a Lua class script that controls pop-up windows, named Ui_hintframe

Local SPACING = 5--initfunction ui_hintframe:init ()--data--===============================--ui--================== =============self.wigetroot = CCS. Guireader:getinstance (): Widgetfromjsonfile (config.dirUI.root. "Hintframe.json") self.x, self.y = Self.wigetRoot:getPosition ()--Save the initial position of the pop-up window and return to the original position after the move is finished self.opacity = {}   -- Save initial transparency for I, V in Ipairs (<span style= "font-family:arial, Helvetica, Sans-serif;" >self.wigetRoot</span>) Doself.opacity[i] = v:getopacity () endself:addccnode (self.wigetroot) endfunction UI _hintframe:setinfo (PARAM_) <pre name= "code" class= "plain" ><span style= "White-space:pre" ></span>
<span style= "White-space:pre" ></span>self.wigetroot:setvisible (false)  --Hide First
--Initialize your label display content endfunction Ui_hintframe:pop (callback_)--Popup window Fade method and pass in callback function local x_, Y_ = Self.wigetRoot:getPosition () Local Trigover_ = false--call backlocal function Onactionover () If Trigover_ = True Thencclog_ ("not possible") RETURNENDTR Igover_ = Trueself.wigetRoot:setVisible (False) Self.wigetRoot:stopAllActions () for I, V in Ipairs (self.component) Dov: Stopallactions () endif callback_ ~= nil thencallback_ () endendlocal function playeranimation () Local delay_ = cc. Delaytime:create (2) Local fadeout_ = cc. Fadeout:create (1) for I, V in Ipairs (self.component) dov:runaction (CC. Sequence:create (Delay_:clone (), Fadeout_:clone (), CC. Callfunc:create (Onactionover))) Endendself.wigetRoot:setVisible (true)--transparency revert for I, V in Ipairs (self.component) Dov: SetOpacity (Self.opacity[i]) end--position restore Self.wigetRoot:setPosition (self.x, Self.y) playeranimation () endfunction ui_ Hintframe:moveup (Pos_)--pop-up window Move method local x_, Y_ = self.wigetRoot:getPosition () Local sz_ = Self.wigetRoot:getSize () Local Newy_ = sz_.height + SPACING + y_self.wigetroot:runaction (CC. Moveto:create (1, CC.P (X_, Newy_))) end

3. Create a popup management script named Hintframemgr.lua

Local hintframemgr = {}local index = take-Home data--==================hintframemgr.hintframeinfolist = {}--new popup requirements are stored first in this hintf Ramemgr.popedhintframe = {}--stores the pop-up window hintframemgr.hintframes = {}--stores the popup entity that can be used to display the local function POPNEXTHINTFR Ame () while hintframemgr.hintframeinfolist[1] ~= nil doif table.getn (hintframemgr.hintframes) > 0 thenlocal function Pophintframeover () Table.insert (Hintframemgr.hintframes, hintframemgr.popedhintframe[1]) Table.remove ( Hintframemgr.popedhintframe, 1) popnexthintframe () endlocal ui_ = Hintframemgr.hintframes[1]if Ui_:setInfo ( HINTFRAMEMGR.HINTFRAMEINFOLIST[1]) Thenfor I, V in Ipairs (hintframemgr.popedhintframe) Do--the second pop-up needs to occur, the first one does the move-up Operation V: MoveUp (Pos_) endui_:pop (pophintframeover) Table.insert (Hintframemgr.popedhintframe, Ui_) Table.remove ( Hintframemgr.hintframes, 1) endtable.remove (hintframemgr.hintframeinfolist, 1) elsebreakendendend--global Method--========= =========function Hintframemgr.pophintframe (PARAM_)--Submit a new pop-up window requirement Table.insert (Hintframemgr.hintframeinfoliSt, PARAM_) popnexthintframe () endfunction hintframemgr.attachhintframe (uis_)--init popup instance hintframemgr.hintframes = Uis_ Hintframemgr.popedhintframe = {}endfunction hintframemgr.detachhintframe () Endreturn hintFrameMgr

3. Call the Ui_hintframe and Hintframemgr classes in scene to initialize

Local hint_max_frames = 3  --shows the maximum number of pop-up windows at a time Self.hintframes = {}for i = 1, hint_max_frames dolocal ui_ = ui_hintframe.new () 
   self:addmsgui (ui_.layer) self.hintframes[i] = Ui_endself.hintframemgr = require ("Obj/hintframemgr") Self.hintFrameMgr.attachHintFrame (self.hintframes)--Call Hintframemgr to initialize Hint_max_frames instance of the popup window


4. Submit the Pop-up window request in scene

Hintframemgr.pophintframe (PARAM_)

5. Code Logic

Ui_hintframe: Read Hintframe.json, get UI entity. and implement the move up and fade methods

Hintframemgr: Provides method attachhintframe to store UI entities in the Hintframes table. Each time a new pop-up window is required, an entity is extracted from hintframes and the display information is initialized

Provides a method Pophintframe receives a new pop-up demand and calls the private method popnexthintframe to implement multiple popup display logic:

1, the new pop-up window needs to be stored in the hintframeinfolist first.

2, extract a requirement from hintframeinfolist, take an instance from Hintframes and initialize the display.

3, the implementation of the fade effect.

4, press into the popedhintframe.

The move-up method is not called because the first pop-up window does not need to be moved. When there is a new window needs to come, repeat steps to implement the Popedhintframe pop-up method, and finally perform step 4. This whole display logic is complete.


6, display effect show




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocos2d-x Lua Learning Notes Popup

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.