Project1
标题:
自己写了一段配方类的脚本,MS有错自己又找不出来求改正
[打印本页]
作者:
lxczzzcxl
时间:
2011-2-9 20:55
标题:
自己写了一段配方类的脚本,MS有错自己又找不出来求改正
话说那个recipe似乎是食谱= =
主代码在下面
class TCItem
attr_accessor :kind
attr_accessor :num
attr_accessor :amount
def initialize(kind, num, amount)
@kind = kind
@num = num
@amount = amount
end
def kind
return @kind
end
def kind=(newkind)
@kind = newkind
end
def num
return @num
end
def num=(newnum)
@num = newnum
end
def amount
return @amount
end
def amount=(newamount)
@amount = newamount
end
def gain
if @kind = 1
$game_party.gain_item(@num, @amount)
elsif @kind = 2
$game_party.gain_weapon(@num, @amount)
elsif @kind = 3
$game_party.gain_armor(@num, @amount)
elsif @kind = 0
$game_party.gain_gold(@amount)
end
end
def lose
if @kind = 1
$game_party.lose_item(@num, @amount)
elsif @kind = 2
$game_party.lose_weapon(@num, @amount)
elsif @kind = 3
$game_party.lose_armor(@num, @amount)
elsif @kind = 0
$game_party.lose_gold(@amount)
end
end
def to_string
if @kind = 1
return $data_item[@num].name + "(物品) × " + @amount.to_s
elsif @kind = 2
return $data_weapon[@num].name + "(武器) × " + @amount.to_s
elsif @kind = 3
return $data_armor[@num].name + "(防具) × " + @amount.to_s
elsif @kind = 0
return "金钱 " + @amount.to_s + " " + $data_system.words.gold
end
end
def is_had
if @kind = 1
return $game_party.item_number(@num) >= @amount
elsif @kind = 2
return $game_party.weapon_number(@num) >= @amount
elsif @kind = 3
return $game_party.armor_number(@num) >= @amount
elsif @kind = 0
return $game_party.gold >= @amount
end
end
end
class TCRecipe
attr_accessor :materialkinds
attr_accessor :material
attr_accessor :productkinds
attr_accessor :product
attr_accessor :name
def initialize(name, ms, ps)
@name = name
@material = ms
@product = ps
@materialkinds = ms.length
@productkinds = ps.length
end
def name
return @name
end
def list_of_material
if @materialkinds = 0
return "无"
elsif @materialkinds = 1
return @material[1].to_string
else
q = ""
@material.each{|unit| q << (unit.to_string + " ")}
return q.strip
end
end
def list_of_product
if @productkinds = 0
return "无"
elsif @productkinds = 1
return @product[1].to_string
else
q = ""
@product.each{|unit| q << (unit.to_string + " ")}
return q.strip
end
end
def is_can_use
q = true
@material.each{|unit| q = q and unit.is_had}
return q
end
def use_it
@material.each{|unit| unit.lose}
@product.each{|unit| unit.gain}
end
end
复制代码
测试时的代码是这样
a1 = TCItem.new(1, 1, 3)
a2 = TCItem.new(1, 2, 1)
a3 = TCItem.new(0, 0, 1000)
b1 = TCItem.new(1, 3, 1)
b2 = TCItem.new(2, 1, 1)
b3 = TCItem.new(3, 1, 1)
a = [a1, a2, a3]
b = [b1, b2, b3]
$reci = TCRecipe.new("test", a, b)
p $reci.name
p $reci.is_can_use
p $reci.list_of_material
p $reci.list_of_product
复制代码
结果打印出来的结果是
"test"
true
"无"
"无"
也就是说"原料"数组和“成品”数组没有被读入
求高手挑刺,到底是哪里出了问题
作者:
Wind2010
时间:
2011-2-9 22:10
本帖最后由 Wind2010 于 2011-2-9 22:11 编辑
既然已经写了
attr_accessor :kind
attr_accessor :num
attr_accessor :amount
复制代码
那个下面的那串
def kind
return @kind
end
def kind=(newkind)
@kind = newkind
end
def num
return @num
end
def num=(newnum)
@num = newnum
end
def amount
return @amount
end
def amount=(newamount)
@amount = newamount
end
复制代码
就不用了
其他的需要再看看XD
作者:
DeathKing
时间:
2011-2-9 23:22
楼主之前是VB的程序员?代码没什么问题,不过有一个很严重,很容易被忽视的错误:
在Ruby里,判断相等应该是 == 方法,而不是 = 操作符。
比如,如果1等于1就应该写作 if 1 == 1 而不是if 1 = 1。否则,无论if后面的condition是什么,都会认为是true而执行then(此关键字通常被省略)后面的语句块
作者:
lxczzzcxl
时间:
2011-2-10 09:23
明白了……ORZ
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1