cocos2d-x 3.2的版本。
。。有2种类型:只有确定按钮、有确定和取消按钮
确定和取消按钮中的确定可以执行回调函数,标签可以自动换行,前提是在IOS模拟器上运行,mac下的模拟器看不出效果,功能比较简单,基本功能是实现了,交互上不是很好,还得改进,点击对话框外面的区域,对话框也会消失,可自行修改实现自己的需求。
继承于Layer,用类的方式写的,写的不是很好,望大神们多多指教。
代码奉上,比较简单:
require "Cocos2d"require "Cocos2dConstants"-- 按钮类型-- 1代表只有确定按钮的对话框-- 2代表有确定和取消按钮的对话框local MSG_Box_OK = 1local MSG_Box_OK_CANCEL =2local msgBoxLayer = class("msgBoxLayer",function() return cc.Layer:create()end)function msgBoxLayer:create(dtype,text,callbackfunc) local layer = msgBoxLayer.new(dtype,callbackfunc) return layerend--初始化function msgBoxLayer:ctor(dtype,callbackfunc) print("MsgBox:ctor_dtype=",dtype,"MsgBox:ctor_text=","MsgBox:ctor_callback",callbackfunc) self.dtype = dtype self.text = text self.callbackfunc = callbackfunc self.winSize = cc.Director:getInstance():getWinSize() self:setVisible(true) self:setLocalZOrder(999) self:setContentSize(self.winSize) local Listener = cc.EventListenertouchOneByOne:create() local function ontouchBegan(touch,event) return self:ontouchBegan(touch,event) end local function ontouchmoved(touch,event) self:ontouchmoved(touch,event) end local function ontouchended(touch,event) self:ontouchended(touch,event) end Listener:registerScriptHandler(ontouchBegan,cc.Handler.EVENT_touch_BEGAN) Listener:registerScriptHandler(ontouchmoved,cc.Handler.EVENT_touch_MOVED) Listener:registerScriptHandler(ontouchended,cc.Handler.EVENT_touch_ENDED) Listener:setSwallowtouches(true) local eventdispatcher = self:getEventdispatcher() eventdispatcher:addEventListenerWithSceneGraPHPriority(Listener,self) self:createMsgBox()endfunction msgBoxLayer:createMsgBox() local background = cc.Scale9Sprite:create("trans_yellow.png") self.bg = background local label = ccui.Text:create() label:setTextAreaSize(cc.size(350,0)) label:setFontname("Helvetica") label:setcolor(cc.c3b(0,255,255)) label:setFontSize(20) label:setTextHorizontalAlignment(cc.TEXT_AlignmENT_left) label:setTextVerticalAlignment(cc.VERTICAL_TEXT_AlignmENT_CENTER) label:setString(self.text) local labelContentSize = label:getTextAreaSize(); local msgBoxHeight = labelContentSize.height + 200 background:setContentSize(cc.size(400,msgBoxHeight)) background:setposition(cc.p(self.winSize.wIDth/2,self.winSize.height/2)) self:addChild(background) local contentSize = background:getContentSize() label:setposition(cc.p(labelContentSize.wIDth-150,msgBoxHeight-50)) background:addChild(label,1) --关闭d出框 local function cancelMsgBoxEvent(sender,eventType) if eventType == ccui.touchEventType.ended then self:removeFromParent(true) end end --执行回调函数,并关闭d出框 local function okMsgBoxEvent(sender,eventType) if eventType == ccui.touchEventType.ended then if self.callbackfunc then self.callbackfunc() end self:removeFromParent(true) end end --创建只有确定按钮的d出框 if self.dtype == MSG_Box_OK then local okbutton = ccui.button:create() background:addChild(okbutton) okbutton:loadTextures("public_btn_1_000.png","","") okbutton:setposition(cc.p(contentSize.wIDth/2,50)) okbutton:settouchEnabled(true) okbutton:addtouchEventListener(cancelMsgBoxEvent) local okbuttonSize = okbutton:getContentSize() local okbuttonLabel = cc.Sprite:create() okbuttonLabel:setTexture("confirm.png") okbuttonLabel:setposition(cc.p(okbuttonSize.wIDth/2,okbuttonSize.height/2)) okbutton:addChild(okbuttonLabel) --创建具有确定和取消按钮的d出框 elseif self.dtype == MSG_Box_OK_CANCEL then local okbutton = ccui.button:create() background:addChild(okbutton) okbutton:loadTextures("public_btn_1_000.png","") okbutton:settouchEnabled(true) okbutton:setposition(cc.p(contentSize.wIDth/2-100,50)) okbutton:addtouchEventListener(okMsgBoxEvent) local okbuttonSize = okbutton:getContentSize() local okbuttonLabel = cc.Sprite:create() okbuttonLabel:setTexture("confirm.png") okbuttonLabel:setposition(cc.p(okbuttonSize.wIDth/2,okbuttonSize.height/2)) okbutton:addChild(okbuttonLabel) local cancelbutton = ccui.button:create() background:addChild(cancelbutton) cancelbutton:loadTextures("public_btn_2_000.png","") cancelbutton:settouchEnabled(true) cancelbutton:setposition(cc.p(contentSize.wIDth/2+100,50)) cancelbutton:addtouchEventListener(cancelMsgBoxEvent) local cancelbuttonSize = cancelbutton:getContentSize() local cancelbuttonLabel = cc.Sprite:create() cancelbuttonLabel:setTexture("cancel.png") cancelbuttonLabel:setposition(cc.p(cancelbuttonSize.wIDth/2,cancelbuttonSize.height/2)) cancelbutton:addChild(cancelbuttonLabel) endendfunction msgBoxLayer:ontouchBegan(touch,event) return trueendfunction msgBoxLayer:ontouchended(touch,event) local curLocation = self:convertToNodeSpace(touch:getLocation()) local rect = self.bg:getBoundingBox() if cc.rectContainsPoint(rect,curLocation) then print("point in rect") else self:removeFromParent(true) end endfunction msgBoxLayer:ontouchmoved(touch,event) endreturn msgBoxLayer总结
以上是内存溢出为你收集整理的Cocos2d-x lua 面向对象封装的一个简单d出框全部内容,希望文章能够帮你解决Cocos2d-x lua 面向对象封装的一个简单d出框所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)