Project1
标题:
为什么一个变量等于一个数组的单元数会出错
[打印本页]
作者:
876加几
时间:
2013-10-4 14:22
标题:
为什么一个变量等于一个数组的单元数会出错
本帖最后由 876加几 于 2013-10-4 15:50 编辑
单元数判断:
数组名称.size
复制代码
方法:
@数组名称_size = @数组名称.size
复制代码
用数组新建任务就会总是提示.size出错,帮助文本本来就这么写的呀,怎么会出错呢?
原脚本:
#==============================================================================
# ■ Window_Task
#------------------------------------------------------------------------------
# 显示任务的选择行窗口。
#==============================================================================
class Window_Task < Window_Selectable
#--------------------------------------------------------------------------
# ● 初始化对像
#--------------------------------------------------------------------------
def initialize
super(0, 0, 640, 480 - 64)
@name = nil
@task_help = nil
[url=home.php?mod=space&uid=236945]@gold[/url] = nil
@items = nil
@weapons = nil
@armors = nil
@item = nil
[url=home.php?mod=space&uid=36793]@weapon[/url] = nil
@armor = nil
if @name == nil
@item_max = 1
else
@item_max = @name.size
end
difficulty = nil
self.contents = Bitmap.new(width - 32, height - 32)
refresh
self.index = -1
end
#--------------------------------------------------------------------------
# ● 刷新
#--------------------------------------------------------------------------
def refresh
self.contents.clear
if @name == nil
@item_max = 1
else
@item_max = @name.size
end
end
#--------------------------------------------------------------------------
# ● 报偿设置
# item_id/weapon_id/armor_id : 报偿物品在数据库中的位置,nil等于没报偿
#--------------------------------------------------------------------------
def reward_items(item_id = nil)
@item[@item.size] = item_id
end
def reward_weapons(weapon_id = nil)
@weapon[@weapon.size] = weapon_id
end
def reward_armor(armor_id = nil)
@armor[@armor.size] = armor_id
end
#--------------------------------------------------------------------------
# ● 新建任务
# new_name : 新任务的名称
# task_help : 新任务的简介
# difficulty : 新任务的难度
# gold : 金钱报偿,0等于没报偿
#--------------------------------------------------------------------------
def new_task(new_name, task_help, difficulty = 1, gold = 0)
@name_size = @name.size
@task_help_size = @task_help.size
difficulty_size = difficulty.size
@gold_size = @gold.size
@items_size = @items.size
@weapons_size = @weapons.size
@armors_size = @armors.size
@name[@name_size] = new_name
@name2[@name_size] = new_name
@task_help[@task_help_size] = task_help
difficulty[difficulty_size] = difficulty
@gold[@gold_size] = gold
@items[@items_size] = @item
@weapons[@weapons_size] = @weapon
@armors[@armors_size] = @armor
end
#--------------------------------------------------------------------------
# ● 完成任务
# bbb : 任务ID
#--------------------------------------------------------------------------
def final_task(bbb)
[url=home.php?mod=space&uid=110690]@BBB[/url] = bbb - 1
unless @name[@bbb].include?("(已失败)")
@name[@bbb] = @name[@bbb] + "(已完成)"
@task_help[@bbb] = @task_help[@bbb] + "(已完成)"
$game_party.gain_gold(@gold[bbb])
for i in
[email protected]
if @item != nil
$game_party.gain_item(@item[i], 1)
end
end
for i in
[email protected]
if @weapon != nil
$game_party.gain_weapon(@weapon[i], 1)
end
end
for i in
[email protected]
if @armor != nil
$game_party.gain_armor(@armor[i], 1)
end
end
end
end
#--------------------------------------------------------------------------
# ● 任务失败
# ccc : 任务ID
#--------------------------------------------------------------------------
def lose_task(ccc)
[url=home.php?mod=space&uid=9267]@CCC[/url] = ccc - 1
unless @name[@bbb].include?("(已完成)")
@name[@ccc] = @name[@ccc] + "(已失败)"
@task_help[@ccc] = @task_help[@ccc] + "(已失败)"
end
end
#--------------------------------------------------------------------------
# ● 描绘项目
# index : 项目编号
# color : 文字色
#--------------------------------------------------------------------------
def draw_item(index,color)
self.contents.font.color = color
rect = Rect.new(4, 32 * index, 640 / 2 - 8, 32)
self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
self.contents.draw_text(rect, @name[index])
end
#--------------------------------------------------------------------------
# ● 项目无效化
# index : 项目编号
#--------------------------------------------------------------------------
def disable_item(index)
draw_item(index, disabled_color)
end
#--------------------------------------------------------------------------
# ● 刷新光标矩形
#--------------------------------------------------------------------------
def update_cursor_rect
if [url=home.php?mod=space&uid=370741]@Index[/url] < 0
self.cursor_rect.empty
else
self.cursor_rect.set(0, @index * 56, 320 - 8, 32)
end
end
#--------------------------------------------------------------------------
# ● 拆分数组并显示内容
#--------------------------------------------------------------------------
def update
for i in 0...@item_max
x = 320 + 16
if self.index == i and $t == true
refresh
self.contents.font.color = system_color
self.contents.draw_text(x, 0, 100, 32, "任务名:")
self.contents.draw_text(x, 64, 100, 32, "难度:")
self.contents.draw_text(x, 112, 100, 32, "说明:")
if @gold[index] != 0
self.contents.draw_text(x, 176, 100, 32, "奖励金钱:")
y = 240
else
y = 208
end
if @items[index] != nil
self.contents.draw_text(x, y, 100, 32, "奖励物品:")
end
self.contents.font.color = normal_color
self.contents.draw_text(x, 32, 320 - 32, 32, @name2[index])
self.contents.draw_text(x, 144, 280, 32, @task_help[index])
if @gold[index] != 0
self.contents.draw_text(x, 208, 100, 32, @gold[index])
end
if @items[index] != nil and @item != nil
for j in
[email protected]
@y = y + @item[j] * 32
end
for g in
[email protected]
draw_item_name($data_items[@item[g]], x, @y)
end
for g in
[email protected]
draw_item_name($data_weapons[@weapon[g]], x, @y + @item.size * 32)
end
for g in
[email protected]
draw_item_name($data_armors[@armor[g]], x, @y + @item.size * 32 + @weapon.size * 32)
end
end
#Moon = difficulty[index] / DIFFICULTY::System
#Star = difficulty[index] % DIFFICULTY::System
#Sun = Moon / DIFFICULTY::System
#Moon %= DIFFICULTY::System
#Space = DIFFICULTY::Space
#rect = Rect.new(0, 0, DIFFICULTY::Size, DIFFICULTY::Size)
#while Sun>0
# self.contents.blt(x + Space, 96, Sun, rect)
# Space += DIFFICULTY::Size + DIFFICULTY::Space;Sun -= 1
#end
#while Moon>0
# self.contents.blt(x + Space, 97, Moon, rect)
# Space += DIFFICULTY::Size + DIFFICULTY::Space;Moon -= 1
#end
#while Star>0
# self.contents.blt(x + Space, 98, Star, rect)
# Space += DIFFICULTY::Size + DIFFICULTY::Space;Star -= 1
#end
case difficulty[index]
when 1
self.contents.draw_text(x + 60, 64, 280, 32, "★☆☆☆☆")
when 2
self.contents.draw_text(x + 60, 64, 280, 32, "★★☆☆☆")
when 3
self.contents.draw_text(x + 60, 64, 280, 32, "★★★☆☆")
when 4
self.contents.draw_text(x + 60, 64, 280, 32, "★★★★☆")
when 5
self.contents.draw_text(x + 60, 64, 280, 32, "★★★★★")
end
end
end
end
end
复制代码
作者:
恐惧剑刃
时间:
2013-10-4 15:24
本帖最后由 恋′挂机 于 2013-10-4 15:31 编辑
可能报错因为什么什么for::NilClass
原因是你那个实例变量没有定义
@array = ["1", "2", "3", "4"]
@array_size = @array.size
p @array_size
复制代码
再说简单点,就是给你 数组名称.size前边加个@,@数组名称.size
作者:
紫英晓狼1130
时间:
2013-10-4 15:59
需要定义实例变量,而且我觉得LZ的描绘星星的部分也有些麻烦…
作者:
芯☆淡茹水
时间:
2013-10-4 16:17
麻烦了就该简化一下。
改前:
case difficulty[index]
when 1
self.contents.draw_text(x + 60, 64, 280, 32, "★☆☆☆☆")
when 2
self.contents.draw_text(x + 60, 64, 280, 32, "★★☆☆☆")
when 3
self.contents.draw_text(x + 60, 64, 280, 32, "★★★☆☆")
when 4
self.contents.draw_text(x + 60, 64, 280, 32, "★★★★☆")
when 5
self.contents.draw_text(x + 60, 64, 280, 32, "★★★★★")
end
复制代码
改后:
text1 = "★" * difficulty[index]
text2 = "☆" * (5 - difficulty[index])
text = text1 + text2
self.contents.draw_text(x + 60, 64, 280, 32, txt)
复制代码
作者:
876加几
时间:
2013-10-4 16:17
紫英晓狼1130 发表于 2013-10-4 15:59
需要定义实例变量,而且我觉得LZ的描绘星星的部分也有些麻烦…
追问:
一定义实例变量语法就出错了。
作者:
chd114
时间:
2013-10-4 16:18
1、尽量别用中文做变量单元模块之类的名称
2、需要调用前记得定义
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1