赞 | 5 |
VIP | 359 |
好人卡 | 195 |
积分 | 3 |
经验 | 560179 |
最后登录 | 2023-12-30 |
在线时间 | 1373 小时 |
Lv2.观梦者
- 梦石
- 0
- 星屑
- 260
- 在线时间
- 1373 小时
- 注册时间
- 2005-10-16
- 帖子
- 5113
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
不知道以前有没有有人弄过类似的,不管了……发了再说……%_%
[10.18更新]
修正了进位未处理的Bug...-_-b很严重的bug啊……
『使用对象』
RPG Maker XP
(废话-_-|||)
『内容简介』
这个脚本的起因是因为,RGSS的内部时钟隐藏不可见,计时竟然是用帧频率和帧数相除换算出来的。
这个时间,只能是模糊的时间,要做到精确计时,不可能-_-b
而且,帧频率可以变,幅度在0~40之间,这样算出的时间可以说在动作游戏里根本无法使用。
因此调用API做了个高精度计时器。
『加入方法』
在Main之前随便插入一个脚本标题,比如System_Timer,拷贝最下方脚本框内所有内容进入这个标题,然后就可以调用了。
『调用方法』
下面是3个调用方法,针对不同需要
1、输入脚本事件:
$game_variables[变量号码]=$sys_timer.now_s()
将目前时间保存到变量中,单位“秒”
2、输入脚本事件:
$game_variables[变量号码]=$sys_timer.now()
将目前时间保存到变量中,单位“毫秒”
3、输入脚本事件:
$sys_timer.clear()
计时器归零。
PS:详细内容,或者RGSS使用者,参考脚本内注释。
『声明』
本脚本内容均为原创,虽然无多少高深技术含量,但是希望转载时候保留作者信息。
『脚本内容』
- # ------------------------------------------------------------------------
- # 高精度计时器 by FantasyDR
- # ------------------------------------------------------------------------
- # E-mail: [email protected]
- # ------------------------------------------------------------------------
- # 2005.10.18
- # ------------------------------------------------------------------------
- # 美嘁丫?欢ㄒ逦??直淞?$sys_timer
- # 如果只需要精确到毫秒,请设置初始化参数为true
- # decimal属性设置返回时间值的小数位数。
- # ------------------------------------------------------------------------
- # 下面是一些有用的方法列表,调用时写:$sys_timer.方法名
- # 例如 $sys_timer.clear()
- # ------------------------------------------------------------------------
- # clear() :计时器清零
- # now() :获取当前经过的时间,单位毫秒
- # now_s() :获取当前经过的时间,单位秒
- # ------------------------------------------------------------------------
- class SystemTimer
- attr_accessor:decimal #小数位数设定,默认为3
- def initialize(use_GetTime=false)
- # 初始化,根据系统选择不同精度计时器
- @qpFrequency = Win32API.new("kernel32","QueryPerformanceFrequency",'p','L')
- @qpCounter = Win32API.new("kernel32","QueryPerformanceCounter",'p','L')
- @tGetTime = Win32API.new("winmm","timeGetTime",'','L')
- @decimal=3
- @perf_cnt=" " * 8
- @time_start=" " * 8
- @time_now=" " * 8
- result = @qpFrequency.call(@perf_cnt)
- if use_GetTime
- result = 0
- end
- if result!=0
- @perf_flag=true
- else
- @perf_flag=false
- @perf_cnt=[1000,0].pack('LL')
- end
- #设置时间比例因数
- @time_scale=@perf_cnt.unpack('LL')
- @time_scale[0] /= 1000.0
- @time_scale[1] /= 1000.0
- #起始时间清零
- self.clear()
- end
- #-=====================-#
- # 计时器清零
- #-=====================-#
- def clear()
- if @perf_flag
- @qpCounter.call(@time_start)
- else
- @time_start=[@tGetTime.call(),0].pack('LL')
- end
- end
- #-==============================-#
- # 获取当前经过的时间,单位毫秒
- #-==============================-#
- def now()
- now_time = 0.0e1
- now_time += self.timer() - self.start()
- now_time /= self.scale()
- return self.debug(now_time)
- end
- #-==============================-#
- # 获取当前经过的时间,单位秒
- #-==============================-#
- def now_s()
- now_time = 0.0e1
- now_time += self.timer() - self.start()
- now_time /= (self.scale()*1000)
- return self.debug(now_time)
- end
- #-==============================-#
- # 帧错...
- #-==============================-#
- def debug(now_time)
- if @decimal>0
- now_time = (now_time * (10**@decimal)).floor/(10.0**@decimal)
- else
- now_time = now_time.floor
- end
- return now_time
- #以下用于debug模式
- if now_time < 0
- p "Timer Wrong!! Clear...",now_time,\
- @perf_flag,@qpCounter,@tGetTime,
- @time_now.unpack('LL')[0],@time_now.unpack('LL')[1],
- @time_start.unpack('LL')[0],@time_start.unpack('LL')[1]
- self.clear()
- return 0.0
- else
- return now_time
- end
- end
- #-=====================-#
- # 获取时间比例因数
- #-=====================-#
- def scale()
- return @time_scale[0]+\
- @time_scale[1]*0xffffffff
- end
- #-=====================-#
- # 获取起始滴答数
- #-=====================-#
- def start()
- return @time_start.unpack('LL')[0]+\
- @time_start.unpack('LL')[1]*0xffffffff
- end
- #-=====================-#
- # 获取当前的嘀哒数
- #-=====================-#
- def timer()
- if @perf_flag
- @qpCounter.call(@time_now)
- else
- @time_now=[@tGetTime.call(),0].pack('LL')
- end
- return @time_now.unpack('LL')[0]+\
- @time_now.unpack('LL')[1]*0xffffffff
- end
- end
- #-------------------------------------#
- # 初始化自身成一个全局变量
- #-------------------------------------#
- $sys_timer=SystemTimer.new()
- #-------------------------------------#
复制代码
[本贴由 柳柳 于 2005-10-24 7:08:51 最后编辑] |
|