| 赞 | 2 |
| VIP | |
| 好人卡 | |
| 积分 | 1 |
| 经验 | |
| 最后登录 | 2017-8-28 |
| 在线时间 | 171 小时 |
- 梦石
- 0
- 星屑
- 125
- 在线时间
- 171 小时
- 注册时间
- 2014-4-14
- 回帖
- 135
|
发表于 2016-8-29 11:06:55
|
显示全部楼层
最麻烦的就是显示选择项,建议用这个脚本。可以随时调用的是或否选择窗口
教程作者:洛克人SZ
首发网址:点此进入本教学的原始帖
适宜用户:喜欢用的都可以用吧^^
技术通用度:★★★★★
技术应用复杂度:20 (满分150分)
学习的理解难度:35 (满分150分)
--------------------------------------------------------------------------------
作者的话:
在需要向玩家询问 是/否 的时候可以用到的东西。
要显示选项窗口在地图画面或战斗画面中可以用显示对话的选项功能实现,在其他画面中就需要建立一个命令窗口,还要写相关的代码,比较麻烦。这个脚本在只需询问 是/否 两个选项的情况下只用一个简单的判断语句就可以完成,应该比较方便吧。
如果已经有类似的东西就请无视吧……
v 1.3: 在 enghao_lim 大人的提醒下继续精简代码
v 1.2: 除掉了一个BUG
v 1.1: 精简代码
教学正文:
[pre lang="ruby"]
#================================================================
# 可以随时调用的 是/否 选择窗口 by 洛克人SZ
# 用法: v 1.3
# if yes_no("提示信息")
# 选择 是 的情况下
# else
# 选择 否 的情况下
# end
#
# 也可以用在事件中的条件分歧里。
#================================================================
def yes_no(info = "请选择:")
dummy_bitmap = Bitmap.new(608, 32)
dummy_bitmap.font.size = 22
strwidth = dummy_bitmap.text_size(info).width
dummy_bitmap.dispose
dummy_bitmap = nil
strwidth = 88 if strwidth < 88
strwidth = strwidth / 2 + strwidth + 32
strwidth = 640 if strwidth > 640
yes_no_window = Window_YesNoCommand.new(info, strwidth)
yes_no_window.visible = true
yes_no_window.active = true
loop do
Graphics.update
Input.update
yes_no_window.update
if yes_no_window.active != true
break
end
end
fhxx = yes_no_window.fhxx
yes_no_window.dispose
yes_no_window = nil
return fhxx
end
#==============================================================================
# ■ Window_HorizSelectable
#------------------------------------------------------------------------------
# 拥有光标的移动以及滚动功能的窗口类。 (水平命令窗口专用)
#==============================================================================
class Window_HorizSelectable < Window_Base
#--------------------------------------------------------------------------
# ● 定义实例变量
#--------------------------------------------------------------------------
attr_reader :index # 光标位置
attr_reader :help_window # 帮助窗口
attr_reader :help_windowa # 帮助窗口a
#--------------------------------------------------------------------------
# ● 初始化对像
# x : 窗口的 X 坐标
# y : 窗口的 Y 坐标
# width : 窗口的宽
# height : 窗口的高
#--------------------------------------------------------------------------
def initialize(x, y, width, height)
super(x, y, width, height)
@index = -1
end
#--------------------------------------------------------------------------
# ● 设置光标的位置
# index : 新的光标位置
#--------------------------------------------------------------------------
def index=(index)
@index = index
# 刷新帮助文本 (update_help 定义了继承目标)
if self.active and @help_window != nil
update_help
end
if self.active and @help_windowa != nil
update_helpa
end
# 刷新光标矩形
update_cursor_rect
end
#--------------------------------------------------------------------------
# ● 帮助窗口的设置
# help_window : 新的帮助窗口
#--------------------------------------------------------------------------
def help_window=(help_window)
@help_window = help_window
# 刷新帮助文本 (update_help 定义了继承目标)
if self.active and @help_window != nil
update_help
end
end
def help_windowa=(help_windowa)
@help_windowa = help_windowa
# 刷新帮助文本 (update_help 定义了继承目标)
if self.active and @help_windowa != nil
update_helpa
end
end
#--------------------------------------------------------------------------
# ● 更新光标矩形
#--------------------------------------------------------------------------
def update_cursor_rect
# 光标位置不满 0 的情况下
if @index < 0
self.cursor_rect.empty
return
end
# 计算光标的宽度
cursor_width = self.width / @item_max - 32
# 计算光标坐标
x = @index % @item_max * (cursor_width + 32)
y = @index / @item_max * 32 - self.oy
# 更新光标矩形
self.cursor_rect.set(x, y, cursor_width, 32)
end
#--------------------------------------------------------------------------
# ● 刷新画面
#--------------------------------------------------------------------------
def update
super
# 可以移动光标的情况下
if self.active and @item_max > 0 and @index >= 0
# 方向键右被按下的情况下
if Input.repeat?(Input::RIGHT)
# 列数为 2 以上并且、光标位置在最后一个项目上的情况下
if @item_max >= 2 and @index == @item_max - 1 and
Input.trigger?(Input::RIGHT)
# 光标返回第一个项目
$game_system.se_play($data_system.cursor_se)
@index = 0
# 列数为 2 以上并且、光标位置在(项目数 - 1)之前的情况下
elsif @item_max >= 2 and @index < @item_max - 1
# 光标向右移动
$game_system.se_play($data_system.cursor_se)
@index += 1
end
end
# 方向键左被按下的情况下
if Input.repeat?(Input::LEFT)
# 列数为 2 以上并且、光标位置在 0 的情况下
if @item_max >= 2 and @index == 0 and
Input.trigger?(Input::LEFT)
# 光标移动到最后一个项目
$game_system.se_play($data_system.cursor_se)
@index = @item_max - 1
# 列数为 2 以上并且、光标位置在 0 之后的情况下
elsif @item_max >= 2 and @index > 0
# 光标向左移动
$game_system.se_play($data_system.cursor_se)
@index -= 1
end
end
end
# 刷新帮助文本 (update_help 定义了继承目标)
if self.active and @help_window != nil
update_help
end
# 刷新帮助文本 (update_help 定义了继承目标)
if self.active and @help_windowa != nil
update_helpa
end
# 刷新光标矩形
update_cursor_rect
end
end
#==============================================================================
# ■ Window_YesNoCommand
#------------------------------------------------------------------------------
# 选择是或否的命令选择行窗口。
#==============================================================================
class Window_YesNoCommand < Window_HorizSelectable
#--------------------------------------------------------------------------
# ● 定义实例变量
#--------------------------------------------------------------------------
attr_reader :fhxx
#--------------------------------------------------------------------------
# ● 初始化对像
# info : 窗口的提示信息
# width : 窗口的宽度
# height : 窗口的高度
#--------------------------------------------------------------------------
def initialize(info = "欢迎", width = 480, height = 96)
x = 320 - width / 2
y = 240 - height / 2
super(x, y, width, height)
self.visible = false
self.active = false
self.opacity = 200
self.contents = Bitmap.new(width - 32, height - 32)
@commands = ["是", "否"]
@item_max = @commands.size
@woff = (self.width - 32) / 4 - 19
@fhxx = false
refresh(info)
self.index = 0
self.z = 10000
end
#--------------------------------------------------------------------------
# ● 刷新
#--------------------------------------------------------------------------
def refresh(info)
self.contents.clear
self.contents.font.size = 22
self.contents.font.color = system_color
self.contents.draw_text(0, 0, self.width - 32, 32, info, 1)
for i in 0...@item_max
draw_item(i, normal_color)
end
end
#--------------------------------------------------------------------------
# ● 描绘项目
# index : 项目编号
#--------------------------------------------------------------------------
def draw_item(index, color)
self.contents.font.color = color
x = self.width / @item_max * index + @woff
off = self.width / @item_max - 32
self.contents.draw_text(x, 32, 32, 32, @commands[index])
end
#--------------------------------------------------------------------------
# ● 项目无效化
# index : 项目编号
#--------------------------------------------------------------------------
def disable_item(index)
draw_item(index, disabled_color)
end
#--------------------------------------------------------------------------
# ● 更新光标矩形
#--------------------------------------------------------------------------
def update_cursor_rect
# 光标位置不满 0 的情况下
if @index < 0
self.cursor_rect.empty
return
end
# 计算光标的宽度
cursor_width = self.width / @item_max - 32
# 计算光标坐标
x = @index % @item_max * (cursor_width + 32)
y = 32
# 更新光标矩形
self.cursor_rect.set(x, y, cursor_width, 32)
end
#--------------------------------------------------------------------------
# ● 刷新画面
#--------------------------------------------------------------------------
def update
super
# 按下取消键的情况下
if Input.trigger?(Input::B)
# 演奏确定 SE
$game_system.se_play($data_system.decision_se)
@fhxx = false
self.visible = false
self.active = false
return
end
# 按下确定键的情况下
if Input.trigger?(Input::C)
# 演奏确定 SE
$game_system.se_play($data_system.decision_se)
# 命令窗口的光标位置分支
case self.index
when 0 # 是
@fhxx = true
self.visible = false
self.active = false
when 1 # 否
@fhxx = false
self.visible = false
self.active = false
end
return
end
end
end
[/pre]
编辑的话:
嗯,可以算新手脚本的范畴吧。
代码虽有些许的赘余,但是功能非常赞呢。
内容全部为自定义的,基本上没有冲突的可能。
|
评分
-
查看全部评分
|