设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 2222|回复: 3
打印 上一主题 下一主题

[已经解决] 怎样制作滚动条

[复制链接]

Lv3.寻梦者

梦石
0
星屑
3846
在线时间
1966 小时
注册时间
2013-1-3
帖子
9536
跳转到指定楼层
1
发表于 2016-2-4 13:25:57 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

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

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

x
怎样制作非鼠标系统的滚动条?
按上或下就改变滚动条位置
就拿物品栏举例…
本人尝试过修改Window_Item,建了个Sprite,但坐标始终有问题
然后又修改Window_Selectable,但取整后位置不对
求大大解答…

评分

参与人数 1星屑 +35 收起 理由
RyanBern + 35 手动认可奖励

查看全部评分

《宿愿·寻剑篇》正式版已经发布!快去看看!点击进入论坛发布贴

Lv5.捕梦者 (版主)

遠航の猫咪

梦石
3
星屑
23119
在线时间
2387 小时
注册时间
2005-10-15
帖子
1166

开拓者

2
发表于 2016-2-5 10:52:12 | 只看该作者
在Window_Selectable中有一些属性与滚动条的计算有关
例如你的窗口能显示5条信息,一共有15条信息,那么不考虑上下箭头的话,滚动条的高度就是5
滚动粒的高度(以及它的位置)和窗口的总状态数有关,窗口最上面显示0-4条,最下面显示10-14条, 一共有11个位置
所以滚动粒的高度就是5/11,这样它的11个位置分别是:
当窗口显示0-4时,滚动粒的顶部为0,占据0到5/11的位置
当窗口显示1-5时,滚动粒的顶部为5/11,占据5/11到10/11的位置
……
当窗口显示10-14时,滚动粒的顶部为4 6/11,占据4 6/11到5的位置

而窗口总条数由@item_max决定,显示哪些条是由self.top_row(可读写属性)决定的
滚动粒的位置这么计算
其高度= @height / (@item_max - (@height / 32) + 1)
其位置(顶端)= @height * (self.top_row) / (@item_max - (@height / 32) + 1)

这里面32是默认的条目高度
另外滚动条要自己建一个bitmap,不要放在content_bitmap上,不然它会随内容上下滚动。

评分

参与人数 2星屑 +240 梦石 +1 收起 理由
RyanBern + 1 认可答案
紫英晓狼1130 + 240 精品文章

查看全部评分

SailCat (小猫子·要开心一点) 共上站 24 次,发表过 11 篇文章 上 次 在: [2006年01月28日11:41:18 星期六] 从 [162.105.120.91] 到本站一游。
回复 支持 反对

使用道具 举报

Lv3.寻梦者

梦石
0
星屑
3846
在线时间
1966 小时
注册时间
2013-1-3
帖子
9536
3
 楼主| 发表于 2016-2-5 14:39:31 | 只看该作者
本帖最后由 紫英晓狼1130 于 2016-2-5 14:43 编辑
SailCat 发表于 2016-2-5 10:52
在Window_Selectable中有一些属性与滚动条的计算有关
例如你的窗口能显示5条信息,一共有15条信息,那么不 ...


意思明白了~
游戏里滚动粒大小不变,该怎样计算坐标(如下图)
按我这么写Window_Selectable计算坐标有问题
条长为168,粒长为7
如果总行数不能被168整除,在倒数第二行时,坐标就会有问题,应该是取整的问题…(还是那张图)
求猫酱帮我看看,喵~
  1. #==============================================================================
  2. # ■ Window_Selectable
  3. #------------------------------------------------------------------------------
  4. #  拥有光标的移动以及滚动功能的窗口类。
  5. #   配有滚动条
  6. #==============================================================================

  7. class Window_Selectable4 < Window_Base
  8.   #--------------------------------------------------------------------------
  9.   # ● 定义实例变量
  10.   #--------------------------------------------------------------------------
  11.   attr_reader   :index                    # 光标位置
  12.   attr_reader   :help_window              # 帮助窗口
  13.   #--------------------------------------------------------------------------
  14.   # ● 初始画对像
  15.   #     x      : 窗口的 X 坐标
  16.   #     y      : 窗口的 Y 坐标
  17.   #     width  : 窗口的宽
  18.   #     height : 窗口的高
  19.   #--------------------------------------------------------------------------
  20.   def initialize(x, y, width, height)
  21.     super(x, y, width, height)
  22.     @scroll = Sprite.new
  23.     @scroll.bitmap = Bitmap.new("Graphics/System/滚动条.png")
  24.     @scroll.x = 579
  25.     @scroll.y = 200
  26.     @scroll.z = 200
  27.     @scrollnumber = 0
  28.     @item_max = 1
  29.     @column_max = 1
  30.     @index = -1
  31.   end
  32.   #--------------------------------------------------------------------------
  33.   # ● 设置光标的位置
  34.   #     index : 新的光标位置
  35.   #--------------------------------------------------------------------------
  36.   def index=(index)
  37.     @index = index
  38.     # 刷新帮助文本 (update_help 定义了继承目标)
  39.     if self.active and @help_window != nil
  40.       update_help
  41.     end
  42.     # 刷新光标矩形
  43.     update_cursor_rect
  44.   end
  45.   #--------------------------------------------------------------------------
  46.   # ● 获取行数
  47.   #--------------------------------------------------------------------------
  48.   def row_max
  49.     # 由项目数和列数计算出行数
  50.     return (@item_max + @column_max - 1) / @column_max
  51.   end
  52.   #--------------------------------------------------------------------------
  53.   # ● 获取开头行
  54.   #--------------------------------------------------------------------------
  55.   def top_row
  56.     # 将窗口内容的传送源 Y 坐标、1 行的高 32 等分
  57.     return self.oy / 26
  58.   end
  59.   #--------------------------------------------------------------------------
  60.   # ● 设置开头行
  61.   #     row : 显示开头的行
  62.   #--------------------------------------------------------------------------
  63.   def top_row=(row)
  64.     # row 未满 0 的场合更正为 0
  65.     if row < 0
  66.       row = 0
  67.     end
  68.     # row 超过 row_max - 1 的情况下更正为 row_max - 1
  69.     if row > row_max - 1
  70.       row = row_max - 1
  71.     end
  72.     # row 1 行高的 32 倍、窗口内容的传送源 Y 坐标
  73.     self.oy = row * 26
  74.   end
  75.   #--------------------------------------------------------------------------
  76.   # ● 获取 1 页可以显示的行数
  77.   #--------------------------------------------------------------------------
  78.   def page_row_max
  79.     # 窗口的高度,设置画面的高度减去 32 ,除以 1 行的高度 32
  80.     return (self.height - 32) / 26
  81.   end
  82.   #--------------------------------------------------------------------------
  83.   # ● 获取 1 页可以显示的项目数
  84.   #--------------------------------------------------------------------------
  85.   def page_item_max
  86.     # 将行数 page_row_max 乘上列数 @column_max
  87.     return page_row_max * @column_max
  88.   end
  89.   #--------------------------------------------------------------------------
  90.   # ● 帮助窗口的设置
  91.   #     help_window : 新的帮助窗口
  92.   #--------------------------------------------------------------------------
  93.   def help_window=(help_window)
  94.     @help_window = help_window
  95.     # 刷新帮助文本 (update_help 定义了继承目标)
  96.     if self.active and @help_window != nil
  97.       update_help
  98.     end
  99.   end
  100.   #--------------------------------------------------------------------------
  101.   # ● 更新光标举行
  102.   #--------------------------------------------------------------------------
  103.   def update_cursor_rect
  104.     # 光标位置不满 0 的情况下
  105.     if @index < 0
  106.       self.cursor_rect.empty
  107.       return
  108.     end
  109.     # 获取当前的行
  110.     row = @index / @column_max
  111.     # 当前行被显示开头行前面的情况下
  112.     if row < self.top_row
  113.       # 从当前行向开头行滚动
  114.       self.top_row = row
  115.     end
  116.     # 当前行被显示末尾行之后的情况下
  117.     if row > self.top_row + (self.page_row_max - 1)
  118.       # 从当前行向末尾滚动
  119.       self.top_row = row - (self.page_row_max - 1)
  120.     end
  121.     # 计算光标的宽
  122.     cursor_width = self.width / @column_max - 32
  123.     # 计算光标坐标
  124.     x = @index % @column_max * (cursor_width + 32)
  125.     y = @index / @column_max * 26 - self.oy
  126.     # 更新国标矩形
  127.     self.cursor_rect.set(x, y, cursor_width, 26)
  128.   end
  129.   #--------------------------------------------------------------------------
  130.   # ● 刷新画面
  131.   #--------------------------------------------------------------------------
  132.   def update
  133.     super
  134.     # 可以移动光标的情况下
  135.     if self.active and @item_max > 0 and @index >= 0
  136.       # 方向键下被按下的情况下
  137.       if Input.repeat?(Input::DOWN)
  138.         # 列数不是 1 并且方向键的下的按下状态不是重复的情况、
  139.         # 或光标位置在(项目数-列数)之前的情况下
  140.         if (@column_max == 1 and Input.trigger?(Input::DOWN)) or
  141.            @index < @item_max - @column_max
  142.           # 光标向下移动
  143.           if @item_max % 3 != 0
  144.             @scroll.y += 168 /(@item_max / @column_max)
  145.             if @scrollnumber != @item_max / @column_max
  146.               @scrollnumber += 1
  147.             end
  148.           else
  149.             @scroll.y += 168 /(@item_max / @column_max)
  150.             if @scrollnumber != @item_max / @column_max
  151.               @scrollnumber += 1
  152.             end
  153.           end
  154.           $game_system.se_play($data_system.cursor_se)
  155.           @index = (@index + @column_max) % @item_max
  156.         end
  157.         if @item_max % 3 != 0
  158.           if @scrollnumber == @item_max / @column_max
  159.             @scroll.y = 361
  160.           end
  161.         else
  162.           if @scrollnumber == @item_max / @column_max - 1
  163.             @scroll.y = 361
  164.           end
  165.         end
  166.       end
  167.       # 方向键上被按下的情况下
  168.       if Input.repeat?(Input::UP)
  169.         # 列数不是 1 并且方向键的下的按下状态不是重复的情况、
  170.         # 或光标位置在列之后的情况下
  171.         if (@column_max == 1 and Input.trigger?(Input::UP)) or
  172.            @index >= @column_max
  173.           # 光标向上移动
  174.           if @item_max % 3 != 0
  175.             @scroll.y -= 168 /(@item_max / @column_max)
  176.             if @scrollnumber != 0
  177.               @scrollnumber -= 1
  178.             end
  179.           else
  180.             @scroll.y -= 168 /(@item_max / @column_max)
  181.             if @scrollnumber != 0
  182.               @scrollnumber -= 1
  183.             end
  184.           end
  185.           $game_system.se_play($data_system.cursor_se)
  186.           @index = (@index - @column_max + @item_max) % @item_max
  187.         end
  188.         if @scrollnumber == 0
  189.           @scroll.y = 200
  190.         end
  191.       end
  192.       # 方向键右被按下的情况下
  193.       if Input.repeat?(Input::RIGHT)
  194.         # 列数为 2 以上并且、光标位置在(项目数 - 1)之前的情况下
  195.         if @column_max >= 2 and @index < @item_max - 1
  196.           # 光标向右移动
  197.           $game_system.se_play($data_system.cursor_se)
  198.           @index += 1
  199.         end
  200.       end
  201.       # 方向键左被按下的情况下
  202.       if Input.repeat?(Input::LEFT)
  203.         # 列数为 2 以上并且、光标位置在 0 之后的情况下
  204.         if @column_max >= 2 and @index > 0
  205.           # 光标向左移动
  206.           $game_system.se_play($data_system.cursor_se)
  207.           @index -= 1
  208.         end
  209.       end
  210.       # R 键被按下的情况下
  211.       if Input.repeat?(Input::R)
  212.         # 显示的最后行在数据中最后行上方的情况下
  213.         if self.top_row + (self.page_row_max - 1) < (self.row_max - 1)
  214.           # 光标向后移动一页
  215.           $game_system.se_play($data_system.cursor_se)
  216.           @index = [@index + self.page_item_max, @item_max - 1].min
  217.           self.top_row += self.page_row_max
  218.         end
  219.       end
  220.       # L 键被按下的情况下
  221.       if Input.repeat?(Input::L)
  222.         # 显示的开头行在位置 0 之后的情况下
  223.         if self.top_row > 0
  224.           # 光标向前移动一页
  225.           $game_system.se_play($data_system.cursor_se)
  226.           @index = [@index - self.page_item_max, 0].max
  227.           self.top_row -= self.page_row_max
  228.         end
  229.       end
  230.     end
  231.     # 刷新帮助文本 (update_help 定义了继承目标)
  232.     if self.active and @help_window != nil
  233.       update_help
  234.     end
  235.     # 刷新光标矩形
  236.     update_cursor_rect
  237.   end
  238.   def dispose
  239.     super
  240.     @scroll.dispose
  241.   end
  242. end
复制代码
回复 支持 反对

使用道具 举报

Lv5.捕梦者 (版主)

遠航の猫咪

梦石
3
星屑
23119
在线时间
2387 小时
注册时间
2005-10-15
帖子
1166

开拓者

4
发表于 2016-2-5 15:03:09 | 只看该作者
紫英晓狼1130 发表于 2016-2-5 14:39
意思明白了~
游戏里滚动粒大小不变,该怎样计算坐标(如下图)
按我这么写Window_Selectable计 ...

你里面有一堆这样的代码
其一:            @scroll.y -= 168 /(@item_max / @column_max)
理论上说,这个168应是161(168-7)
其二:连续整数除法的取整舍入误差太大了
应该先更新@index,然后让然后你再去直接算新的@scroll.y
其三:滚动条的跟随有问题
还有,你的@scroll.y跟着光标走,这是错的,你去看系统的滚动条,都是跟滚动屏(top_row,或者window_base里的oy)走的,哪有跟光标走的啊……

点评

谢谢  发表于 2016-2-5 15:24
SailCat (小猫子·要开心一点) 共上站 24 次,发表过 11 篇文章 上 次 在: [2006年01月28日11:41:18 星期六] 从 [162.105.120.91] 到本站一游。
回复 支持 反对

使用道具 举报

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

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-9-22 17:22

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表