Cocos2d-x lua 面向对象封装的一个简单弹出框

前端之家收集整理的这篇文章主要介绍了Cocos2d-x lua 面向对象封装的一个简单弹出框前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2d-x 3.2的版本。

。。有2种类型:只有确定按钮、有确定和取消按钮

确定和取消按钮中的确定可以执行回调函数标签可以自动换行,前提是在IOS模拟器上运行,mac下的模拟器看不出效果功能比较简单,基本功能是实现了,交互上不是很好,还得改进,点击对话框外面的区域,对话框也会消失,可自行修改实现自己的需求。

继承于Layer,用类的方式写的,写的不是很好,望大神们多多指教。

代码奉上,比较简单:

require "Cocos2d"
require "Cocos2dConstants"

-- 按钮类型
-- 1代表只有确定按钮的对话框
-- 2代表有确定和取消按钮的对话框
local MSG_Box_OK = 1
local MSG_Box_OK_CANCEL =2


local msgBoxLayer = class("msgBoxLayer",function()
    return cc.Layer:create()
end)


function msgBoxLayer:create(dtype,text,callbackfunc)
    local layer = msgBoxLayer.new(dtype,callbackfunc)
    return layer
end

--初始化
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()
end

function 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)
    end
end

function msgBoxLayer:onTouchBegan(touch,event)
    return true
end

function 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
	
end

function msgBoxLayer:onTouchMoved(touch,event)
	
end

return msgBoxLayer
原文链接:https://www.f2er.com/cocos2dx/346327.html

猜你在找的Cocos2d-x相关文章