cocos2dx_3.3 lua类的创建

前端之家收集整理的这篇文章主要介绍了cocos2dx_3.3 lua类的创建前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

学习一下cocos2dx 3.3 lua类的创建,记录一下自己的学习方法


local TouchSprite = class("TouchSprite",--继承cc.Sprite
    function(fileName)
        return cc.Sprite:create(fileName) 
    end
)

TouchSprite.__index = TouchSprite   -- 用于访问

TouchSprite.type = 0    -- 自定义属性
TouchSprite.typeAction = 0

function TouchSprite:createMS(fileName,_type)      --自定义构造函数
    local mySprite = TouchSprite.new(fileName)     --这个new相当于执行了上面的class方法
    mySprite:myInit(_type)
    mySprite:Init(_type)
    mySprite:setPosition(100,200)
    return mySprite
end

function TouchSprite:myInit(_type)    --自定义函数
    self.type =_type
    cclog(self.type)
end

function TouchSprite:Init(type)

    local function onTouchBegan(touch,event)
        if cc.rectContainsPoint(self:getBoundingBox(),touch:getLocation() ) then
            cclog("onTouchBegan")
            self.type =type
            cclog(self.type)
            if self.typeAction == 0 then
                self:action(0.5,type)
            end
        end
        return true
    end

    local function onTouchMoved(touch,touch:getLocation() ) then
            cclog("onTouchMoved")
        end
    end

    local function onTouchEnded(touch,touch:getLocation() ) then
            cclog("onTouchEnded")
        end
    end


    local listener = cc.EventListenerTouchOneByOne:create()
    listener:setSwallowTouches(true);
    listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
    listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
    listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
    local eventDispatcher = self:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraPHPriority(listener,self)
end
function TouchSprite:action(time,type)
    self.typeAction = 1
    local actionBy = cc.RotateBy:create(time,360)
    local function functioncall()
        self.typeAction = 0
    end
    local callback = cc.CallFunc:create(functioncall)
    self:runAction(cc.Sequence:create(actionBy,callback))
end

function TouchSprite:removeThis()
    if self then
        self:getParent():removeChild(self,true)
       
    end
end


return TouchSprite

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