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, event)
        if cc.rectContainsPoint(self:getBoundingBox(),touch:getLocation() ) then
            cclog("onTouchMoved")
        end
    end

    local function onTouchEnded(touch, event)
        if cc.rectContainsPoint(self:getBoundingBox(),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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值