Project1
标题: 如何对应数组 [打印本页]
作者: 1390959782 时间: 2021-3-29 13:31
标题: 如何对应数组
end
def jiuge( xyz)
case xyz
when 0
return [ 120 ,50 ,50 ]
when 1
return [ 120 ,50 ,50 ]
when 2
return [ 120 ,50 ,50 ]
when 3
return [ 120 ,50 ,50 ]
when 4
return [ 120 ,50 ,50 ]
when 5
return [ 120 ,50 ,50 ]
when 6
return [ 120 ,50 ,50 ]
when 7
return [ 120 ,50 ,50 ]
when 8
return [ 120 ,50 ,50 ]
when 9
return [ 120 ,50 ,50 ]
end
end
def set_pos( kind)
case kind
when 0
return [ 0 ,1 ,2 ,3 ,4 ]
end
end
def jiuge( xyz)
case xyz
when 0
return [ 120 ,50 ,50 ]
when 1
return [ 120 ,50 ,50 ]
when 2
return [ 120 ,50 ,50 ]
when 3
return [ 120 ,50 ,50 ]
when 4
return [ 120 ,50 ,50 ]
when 5
return [ 120 ,50 ,50 ]
when 6
return [ 120 ,50 ,50 ]
when 7
return [ 120 ,50 ,50 ]
when 8
return [ 120 ,50 ,50 ]
when 9
return [ 120 ,50 ,50 ]
end
end
def set_pos( kind)
case kind
when 0
return [ 0 ,1 ,2 ,3 ,4 ]
end
end
这段如何使得set_pos里面的五个数字分别对应xyz,从而获取jiuge的三个数值,怎么说,我都快晕了 ,不知道各位大佬能不能理解
作者: alexncf125 时间: 2021-3-29 14:08
本帖最后由 alexncf125 于 2021-3-29 14:16 编辑
kind = 0
for i in set_pos(kind)
array ||= []
array.push(jiuge(i))
end
作者: KB.Driver 时间: 2021-3-30 00:11
这种完全属于数据的内容应该尽量避免硬编码。
提供几个一般般的方案吧
方案一
POS_LIST = {
0 => [ 0 , 1 , 2 , 3 , 4 ] ,
# 继续按这个格式往下加
}
XYZ_LIST = {
0 => [ 120 , 50 , 50 ] ,
1 => [ 120 , 50 , 50 ] ,
2 => [ 120 , 50 , 50 ] ,
3 => [ 120 , 50 , 50 ] ,
4 => [ 120 , 50 , 50 ] ,
# 继续按这个格式往下加
}
def get_xyz( kind)
POS_LIST[ kind] .map { |e| XYZ_LIST[ e] }
end
POS_LIST = {
0 => [ 0 , 1 , 2 , 3 , 4 ] ,
# 继续按这个格式往下加
}
XYZ_LIST = {
0 => [ 120 , 50 , 50 ] ,
1 => [ 120 , 50 , 50 ] ,
2 => [ 120 , 50 , 50 ] ,
3 => [ 120 , 50 , 50 ] ,
4 => [ 120 , 50 , 50 ] ,
# 继续按这个格式往下加
}
def get_xyz( kind)
POS_LIST[ kind] .map { |e| XYZ_LIST[ e] }
end
方案二
POS_LIST = {
0 => [ 0 , 1 , 2 , 3 , 4 ] ,
# 继续按这个格式往下加
}
XYZ_LIST = {
0 => [ 120 , 50 , 50 ] ,
1 => [ 120 , 50 , 50 ] ,
2 => [ 120 , 50 , 50 ] ,
3 => [ 120 , 50 , 50 ] ,
4 => [ 120 , 50 , 50 ] ,
# 继续按这个格式往下加
}
POS_LIST.each { |k, v| v.map ! { |e| XYZ_LIST[ e] } }
# 直接用POS_LIST[kind]就行
POS_LIST = {
0 => [ 0 , 1 , 2 , 3 , 4 ] ,
# 继续按这个格式往下加
}
XYZ_LIST = {
0 => [ 120 , 50 , 50 ] ,
1 => [ 120 , 50 , 50 ] ,
2 => [ 120 , 50 , 50 ] ,
3 => [ 120 , 50 , 50 ] ,
4 => [ 120 , 50 , 50 ] ,
# 继续按这个格式往下加
}
POS_LIST.each { |k, v| v.map ! { |e| XYZ_LIST[ e] } }
# 直接用POS_LIST[kind]就行
作者: 1390959782 时间: 2021-3-30 13:06
谢谢大佬 ,谢谢小哥哥?小姐姐?
作者: 1390959782 时间: 2021-3-30 13:07
谢谢小姐姐
作者: 1390959782 时间: 2021-3-30 13:17
那个,大佬,如果调用的话怎么调用呢,就是说如果调用POS_LIST[0]获取对应的九宫格阵型1号角色的xzy坐标,那么是不是应该写成
def screen_x
case self.index
when 0
POS_LIST[0]
end
还是
def screen_x
case self.index
when 0
XZY_LIST[0]
end
作者: 1390959782 时间: 2021-3-30 13:49
懂了懂了,push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度(来自百度java),从而使得kind对应的每个set_pos里面的每个xyz对应的数组加进去,我说的对不对,小姐姐 ,搜了个百度才看清楚个小姐姐push这个核心
作者: guoxiaomi 时间: 2021-3-30 15:37
ruby里的Array#push是返回数组本身:https://ruby-doc.org/core-3.0.0/Array.html#method-i-push
作者: 1390959782 时间: 2021-3-31 13:40
啊这,好吧
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1