查看: 2993|回复: 6

[讨论] [九靈研究]trigger?press?repeat?区别实验

[复制链接]
梦石
0
星屑
49
在线时间
157 小时
注册时间
2013-8-14
回帖
155
发表于 2013-9-7 12:50:32 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
为了区分trigger?、 press?、 repeat?
而写了代码实验


2013-09-07_123912.jpg
2013-09-07_123858.jpg
2013-09-07_123853.jpg


[pre lang="ruby" line="1"]#=begin
class InputtTest < Window
  def initialize
    super  
    self.windowskin = Bitmap.new("Graphics/Windowskins/001-Blue01")  
    self.width = 640 - 64  
    self.height = 32 * 10  
    self.x = 64 / 2
    self.y = (480 / 2) - ((32 * 10) / 2)  
    self.z = 100   
    self.back_opacity = 128

    self.contents = Bitmap.new((640 - 32) - 32, (32 * 10) - 32)
    self.contents.font.name = "黑体"
    self.contents.font.color = Color.new(255, 255, 255, 200)
   
    #先声明实例变量,因为要用到 +=
    #为什么是实例变量?因为在update中依然是实例变量的作用域
    @s0 = 0.0
    @s1 = 0.0
    @s2 = 0.0
    @s3 = 0.0   
    #声明空数组
    @input_type = []
    @text_rect = []
   
    #初始化"判定生成Rect"的旗帜
    @text_rect_flag = false
   
    #loop循环,此为操作阶段(Input.update为实现互动关键)
    loop do
      #前进一帧
      Graphics.update
      #捕捉/侦测/获取,并判断,按下的键及按下的类型,随即记录
      Input.update
      #调用自(重)定义update 方法
      update
    end
  end
  
  #剥离loop循环,可精减loop循环的复杂度
  #方便管理/侦错(也可以塞回去,将update展开,只是loop do...end会变得很庞大)
  def update
   
    #按下C键的trigger?判断(根据Input.update捕捉到的)
    if Input.trigger?(Input::C)
      @s0 += 1
    end
    #按下C键的press?判断(根据Input.update捕捉到的)
    if Input.press?(Input::C)
      @s1 += 1
    end
    #按下C键的repeat?判断(根据Input.update捕捉到的)
    if Input.repeat?(Input::C)
      @s2 += 1
    end
    #press除以repeat,观察两者差异
    @s3 = @s1 / @s2
   
    # += 1 后更新/刷新/再赋值@input_type,获取最新数据,to_s要加
    @input_type = ["trigger\s:\s" + @s0.to_s, "press\s:\s" + @s1.to_s, \
    "repeat\s:\s" + @s2.to_s, "press / repeat\s:\s" + @s3.to_s]
   
    #刷新新数据前,先清空旧资料(contents),以免重叠
    self.contents.clear
    #for循环,执行4次,高效处理更新数据
    for i in 0...@input_type.size
      #判断@text_rect_flag是否为false
      if @text_rect_flag == false  #一开始没放东西,所以旗帜为false(初始值)
        #放入Rect,设定一行放一个,这样就不用再set了
        @text_rect = Rect.new(0, 32 * i, (640 - 32) - 32, 32)
      end  
      #if结束后输出文字
      self.contents.draw_text(@text_rect, @input_type)   
    end
   
    if @text_rect_flag == false
      #在@text_rect尾部/后面加进一个Rect,用来放dir4
      @text_rect.push(Rect.new(0, 32 * @input_type.size, \
      (640 - 32) - 32, 32))
      #在@text_rect尾部/后面加进一个Rect,用来放dir8
      @text_rect.push(Rect.new(0, 32 * (@input_type.size + 1), \
      (640 - 32) - 32, 32))
    end

    #for、if结束后,在确信"放入Rect"后,改为true,之后if就不执行了
    @text_rect_flag = true  
   
    #先画上"dir4 : ",让他一直显示,因为定期clear,所以上下左右只闪一下
    self.contents.draw_text(@text_rect[@input_type.size], "dir4 : ")  
    #若左下、右下、左上、右上时不执行
    unless Input.dir8 == 1 or Input.dir8 == 3 or Input.dir8 == 7 or \
      Input.dir8 == 9
      case Input.dir4
      when 2
        self.contents.draw_text(@text_rect[@input_type.size], "       下")
      when 4
        self.contents.draw_text(@text_rect[@input_type.size], "       左")
      when 6
        self.contents.draw_text(@text_rect[@input_type.size], "       右")
      when 8
        self.contents.draw_text(@text_rect[@input_type.size], "       上")  
      end
    end
   
    self.contents.draw_text(@text_rect[@input_type.size + 1], "dir8 : ")   
    case Input.dir8
    when 1
      self.contents.draw_text(@text_rect[@input_type.size + 1], "       左下")
    when 2
      self.contents.draw_text(@text_rect[@input_type.size + 1], "       下")
    when 3
      self.contents.draw_text(@text_rect[@input_type.size + 1], "       右下")
    when 4
      self.contents.draw_text(@text_rect[@input_type.size + 1], "       左")
    when 6
      self.contents.draw_text(@text_rect[@input_type.size + 1], "       右")
    when 7
      self.contents.draw_text(@text_rect[@input_type.size + 1], "       左上")
    when 8
      self.contents.draw_text(@text_rect[@input_type.size + 1], "       上")
    when 9
      self.contents.draw_text(@text_rect[@input_type.size + 1], "       右上")
    end
  end
end
   
@title_moon = Sprite.new
@title_moon.bitmap = Bitmap.new("Graphics/Titles/001-Title01")
@input_test = InputtTest.new

=begin
以上探讨分析
实例变量
Input
旗帜
dir4、dir8
trigger? press? repeat? 之间的关系
=end

exit
[/pre]
梦石
0
星屑
122
在线时间
552 小时
注册时间
2012-8-18
回帖
1392
发表于 2013-9-7 13:00:47 | 显示全部楼层
这说明了什么……
我要填坑!我要背单词!我要学日语!我要每天锻炼!
好吧呵呵= =
回复

使用道具 举报

梦石
0
星屑
49
在线时间
157 小时
注册时间
2013-8-14
回帖
155
 楼主| 发表于 2013-9-7 13:34:36 | 显示全部楼层
无脑之人 发表于 2013-9-7 13:00
这说明了什么……

说明了press / repeat 接近 4
回复

使用道具 举报

梦石
1
星屑
7574
在线时间
2650 小时
注册时间
2013-8-23
回帖
2152

开拓者

发表于 2013-9-7 14:14:14 | 显示全部楼层
这玩意是神马....
回复

使用道具 举报

梦石
0
星屑
85
在线时间
40 小时
注册时间
2012-7-3
回帖
82
发表于 2013-9-18 09:43:57 | 显示全部楼层
鼠标拥有两个状态:Down,Up
还有一个Click
这三个消息产生的顺序是Down=>Up=>Click
down=鼠标按下
up=鼠标抬起
click=鼠标完成了一次点击
↑同理可证,键盘的应该差不多。
回复

使用道具 举报

梦石
0
星屑
76
在线时间
1379 小时
注册时间
2012-7-5
回帖
1643

开拓者

发表于 2013-9-19 21:31:30 | 显示全部楼层
为什么我要来回这个贴,
我发现判断一个键持续按下(比如我要显示几张图实时演出我的操作信息<何弃疗)时用press更好,repeat的使用地方还是比较少的

  -fk: -azogi:
回复

使用道具 举报

梦石
0
星屑
59
在线时间
84 小时
注册时间
2008-4-19
回帖
403
发表于 2014-5-16 22:48:33 | 显示全部楼层
吐槽:参见全键盘脚本的key? press?和trigger?
好吧这是II让我贴的- -
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部