Quick中的触摸处理和时间调度

本文介绍了Cocos2d-x中如何实现触摸事件处理和时间调度功能。通过实例展示了如何设置精灵的触摸事件监听,并根据不同触摸阶段进行响应。此外,还提供了两种时间调度的方法,包括使用定时器管理器和全局时间调度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、 Quick中的触摸处理
	local sp1 = cc.Sprite:create("fish_1.png")
	sp1:addTo(self,2)    --添加到层
	sp1:setRotation(90)     --设置角度
	sp1:pos(display.cx, display.cy)  --设置位置
	sp1:setTouchEnabled(true)     --设置触摸
	sp1:setTouchSwallowEnabled(false)     --设置吞噬
	---添加监听事件
	sp1:addNodeEventListener(cc.NODE_TOUCH_EVENT, --事件类型
 		function ( event )   --回调函数	
 			dump(event)  --输出日志
 			if event.name == "ended" then
                	print("触摸结束")

            		elseif event.name == "began" then
               	 	print("触摸开始")
                	return true              --触摸开始要返回true,否则不会执行移动等操作
            		elseif event.name == "moved" then
                	print("触摸移动")
            		else
            	 	print("触摸结束")
            		end
 		end
	)	

2、时间调度

第一种方法

	local schedulerID ; --设置ID
	sharedScheduler = cc.Director:getInstance():getScheduler();   --获取定时器管理器
	schedulerID = sharedScheduler:scheduleScriptFunc(function (  )
		print("schedule");
		-- sharedScheduler:unscheduleScriptEntry(schedulerID)    --停止定时器
	end,
	1,  --每隔一秒调用一次,若为零,则表示每帧都调用
	false)

第二种方法

	local scheduler = require(cc.PACKAGE_NAME..".scheduler")   --使用第二种方法要导入需要的模块,它并不会自动载入
	local function onInterval( dt )      ------定义函数
		 dump( dt)
		print("这是回调函数")
	end 
	local handle = scheduler.scheduleGlobal(onInterval,1)   --开启时间调度,1秒一次
	-- local handle = scheduler.scheduleUpdateGlobal(onInterval)   --只有一个参数,默认每帧一次
	-- scheduler.unscheduleGlobal(handle) -----停止定时器


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值