Cocos2d-x lua 面向对象封装的一个简单弹出框
来源:互联网 发布:劳丽诗淘宝店地址 编辑:程序博客网 时间:2024/06/06 01:22
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,text,callbackfunc) return layerend--初始化function msgBoxLayer:ctor(dtype,text,callbackfunc) print("MsgBox:ctor_dtype=",dtype,"MsgBox:ctor_text=",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) --关闭弹出框 local function cancelMsgBoxEvent(sender, eventType) if eventType == ccui.TouchEventType.ended then self:removeFromParent(true) end end --执行回调函数,并关闭弹出框 local function okMsgBoxEvent(sender, eventType) if eventType == ccui.TouchEventType.ended then if self.callbackfunc then self.callbackfunc() end self:removeFromParent(true) end end --创建只有确定按钮的弹出框 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) --创建具有确定和取消按钮的弹出框 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)endendfunction msgBoxLayer:onTouchMoved(touch, event)endreturn msgBoxLayer
0 0
- Cocos2d-x lua 面向对象封装的一个简单弹出框
- 【cocos2d-x 3.5】Lua的简单面向对象
- Cocos2d-Lua之面向对象的封装
- cocos2d-x lua 面向对象
- cocos2d-x lua 弹出框
- COCOS2D-X Lua面向对象编程
- COCOS2D-X Lua 面向对象编程
- COCOS2D-X Lua 面向对象编程
- Cocos2d-x 脚本语言Lua中的面向对象
- Cocos2d-x 脚本语言Lua中的面向对象
- COCOS2D-X Lua面向对象编程
- Lua的面向对象封装
- Cocos2d-x Lua游戏开发之Lua 面向对象编程
- lua面向对象的2种私密性封装
- Cocos2d-Lua之面向对象的继承
- cocos2d-x lua 类对象的使用
- cocos2d-x lua脚本简单的了解
- Cocos2d-x简单的Log类封装
- Linux编译错误:‘cout’在此作用域中尚未声明
- 缓冲流理解~
- eclipse:File->New没有Android Application Project的解决办法
- 常州隆胸手术整形医院
- 判断空值
- Cocos2d-x lua 面向对象封装的一个简单弹出框
- 对工厂模式的理解
- crackme.chm之figugegl_2b
- C# webBrowser 截取验证码截图的两种方式
- OpenGL学习——二维几何变换
- DIV元素响应键盘事件
- 字符串匹配算法
- Objective-C中的instancetype和id关键字
- 常州隆胸整形有哪些医院