加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx 3.3 + lua 学习笔记(03)--- 触摸事件、定时器、进度条

发布时间:2020-12-14 16:59:40 所属栏目:百科 来源:网络整理
导读:—多点触摸 functionMenuLayer:addTouches() localtouch1,touch2=cc.Touch,cc.Touch localfunctiononTouchesBegan(touches,event) print(“TouchesBegan”) touch1=touches[1]—第一个触点,下标从1开始 touch2=touches[2]—第二个触点 localpos1=touch1:get
—多点触摸
functionMenuLayer:addTouches()
localtouch1,touch2=cc.Touch,cc.Touch
localfunctiononTouchesBegan(touches,event)
print(“TouchesBegan”)
touch1=touches[1]—第一个触点,下标从1开始
touch2=touches[2]—第二个触点
localpos1=touch1:getLocation()—获取触点1的位置
localpos2=touch2:getLocation()—获取触点2的位置
localdelta={
x=pos2.x–pos1.x,
y=pos2.y–pos1.y
}
end

localfunctiononTouchesMoved(touches,sans-serif; font-size:15px; line-height:26px; text-indent:30px">print(“TouchesMoved”)
localfunctiononTouchesEnded(touches,sans-serif; font-size:15px; line-height:26px; text-indent:30px">print(“TouchesEnded”)
—注册多点触摸
locallistener=cc.EventListenerTouchAllAtOnce:create() --单点EventListenerTouchOneByOne():create()
listener:registerScriptHandler(onTouchesBegan,cc.Handler.EVENT_TOUCHES_BEGAN)
listener:registerScriptHandler(onTouchesMoved,cc.Handler.EVENT_TOUCHES_MOVED)

listener:registerScriptHandler(onTouchesEnded,cc.Handler.EVENT_TOUCHES_ENDED)

--获取层的事件派发器

localdispatcher=cc.Director:getInstance():getEventDispatcher()

dispatcher:addEventListenerWithSceneGraphPriority(listener,self)

end

— 定时器

--开启定时器,一般是两种方式

第一种:自定义的形式,使用之后记得关闭

schedulerID = cc.Director:getInstance():getScheduler():scheduleScriptFunc(update,false) --调用的function,调用频率(秒),是否暂停(true,false)
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(schedulerID) -- 取消定时器

第二种:类似c++中的scheduleUpdate()
self:scheduleUpdateWithPriorityLua(update,0)

self:unscheduleUpdate() --取消定时器

调用的函数原型如下

local function update(delta)

end


— 进度条

--血条 local bloodBody = cc.Sprite:create(bloodPic) --创建进度条 local bloodProgress = cc.ProgressTimer:create(bloodBody) bloodProgress:setType(cc.PROGRESS_TIMER_TYPE_BAR) --设置为条形 type:cc.PROGRESS_TIMER_TYPE_RADIAL bloodProgress:setMidpoint(cc.p(0,0)) --设置起点为条形坐下方 bloodProgress:setBarChangeRate(cc.p(1,0)) --设置为水平方向 bloodProgress:setPercentage(100) -- 设置初始进度为0 bloodProgress:setPosition(cc.p(0,0)) self:addChild(bloodProgress)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读