Project1

标题: 以下代码能用其他内嵌表达式代替一部分吗? [打印本页]

作者: TheRebirth    时间: 2012-4-30 17:14
标题: 以下代码能用其他内嵌表达式代替一部分吗?
本帖最后由 iisnow 于 2012-4-30 18:21 编辑

"#{}"这个倒是知道,但我感觉似乎不能这样写: @player = TShoot_Player"#{$game_variables[5]}".new(228, 336) (目的是:比如五号变量值为2,则加载TShoot_Player2这个类)
我感觉写这个好麻烦啊...↓
  1. #--------------------------------------------------------------------------
  2.   # ● 自機弾の追加処理
  3.   #--------------------------------------------------------------------------
  4.   def add_pbullet(x, y, angle, speed, index, type)
  5.     i = @bullet.index(nil)
  6.     i = @bullet.size if i == nil
  7.     @bullet[i] = TShoot_PBullet1.new(x, y, angle, speed, index, i) if type == 1
  8.     @bullet[i] = TShoot_PBullet2.new(x, y, angle, speed, index, i) if type == 2
  9.     @bullet[i] = TShoot_PBullet3.new(x, y, angle, speed, index, i) if type == 3
  10.     @bullet[i] = TShoot_PBullet4.new(x, y, angle, speed, index, i) if type == 4
  11.     @bullet[i] = TShoot_PBullet5.new(x, y, angle, speed, index, i) if type == 5
  12.     return i
  13.   end
  14.   #--------------------------------------------------------------------------
  15.   # ● 敵機弾の追加処理
  16.   #--------------------------------------------------------------------------
  17.   def add_ebullet(x, y, vx, vy, index, type)
  18.     i = @bullet.index(nil)
  19.     i = @bullet.size if i == nil
  20.     @bullet[i] = TShoot_EBullet1.new(x, y, vx, vy, index, i) if type == 1
  21.     @bullet[i] = TShoot_EBullet2.new(x, y, vx, vy, index, i) if type == 2
  22.     @bullet[i] = TShoot_EBullet3.new(x, y, vx, vy, index, i) if type == 3
  23.     @bullet[i] = TShoot_EBullet4.new(x, y, vx, vy, index, i) if type == 4
  24.     @bullet[i] = TShoot_EBullet5.new(x, y, vx, vy, index, i) if type == 5
  25.     @bullet[i] = TShoot_EBullet6.new(x, y, vx, vy, index, i) if type == 6
  26.     @bullet[i] = TShoot_EBullet7.new(x, y, vx, vy, index, i) if type == 7
  27.     @bullet[i] = TShoot_EBullet8.new(x, y, vx, vy, index, i) if type == 8
  28.     @bullet[i] = TShoot_EBullet9.new(x, y, vx, vy, index, i) if type == 9
  29.     @bullet[i] = TShoot_EBullet10.new(x, y, vx, vy, index, i) if type == 10
  30.     @bullet[i] = TShoot_EBullet11.new(x, y, vx, vy, index, i) if type == 11
  31.     @bullet[i] = TShoot_EBullet12.new(x, y, vx, vy, index, i) if type == 12
  32.     @bullet[i] = TShoot_EBullet13.new(x, y, vx, vy, index, i) if type == 13
  33.     @bullet[i] = TShoot_EBullet14.new(x, y, vx, vy, index, i) if type == 14
  34.     @bullet[i] = TShoot_EBullet15.new(x, y, vx, vy, index, i) if type == 15
  35.     @bullet[i] = TShoot_EBullet16.new(x, y, vx, vy, index, i) if type == 16
  36.     return i
  37.   end

  38.   #--------------------------------------------------------------------------
  39.   # ● アイテムの追加処理
  40.   #--------------------------------------------------------------------------
  41.   def add_item(x, y, vx, vy, type)
  42.     i = @item.index(nil)
  43.     i = @item.size if i == nil
  44.     @item[i] = TShoot_Item1.new(x, y, vx, vy, type) if type == 0
  45.     @item[i] = TShoot_Item2.new(x, y, vx, vy, type) if type == 1
  46.     @item[i] = TShoot_Item3.new(x, y, vx, vy, type) if type == 2
  47.     @item[i] = TShoot_Item4.new(x, y, vx, vy, type) if type == 3
  48.     @item[i] = TShoot_Item5.new(x, y, vx, vy, type) if type == 4
  49.     @item[i] = TShoot_Item6.new(x, y, vx, vy, type) if type == 5
  50.     @item[i] = TShoot_Item7.new(x, y, vx, vy, type) if type == 6
  51.     @item[i] = TShoot_Item8.new(x, y, vx, vy, type) if type == 7
  52.     @item[i] = TShoot_Item9.new(x, y, vx, vy, type) if type == 8
  53.   end
  54.   #--------------------------------------------------------------------------
  55.   # ● エフェクトの追加処理
  56.   #--------------------------------------------------------------------------
  57.   def add_effect(type, x, y, vx, vy)
  58.     i = @popup.index(nil)
  59.     i = @popup.size if i == nil
  60.     if type == 1
  61.       @popup[i] = TShoot_Effect1.new(x, y, vx, vy)
  62.     elsif type == 2
  63.       @popup[i] = TShoot_Effect2.new(x, y, vx, vy)
  64.     elsif type == 3
  65.       @popup[i] = TShoot_Effect3.new(x, y, vx, vy)
  66.     elsif type == 4
  67.       @popup[i] = TShoot_Effect4.new(x, y, vx, vy)
  68.     elsif type == 5
  69.       @popup[i] = TShoot_Effect5.new(x, y, vx, vy)
  70.     elsif type == 6
  71.       @popup[i] = TShoot_Effect6.new(x, y, vx, vy)
  72.     elsif type == 7
  73.       @popup[i] = TShoot_Effect7.new(x, y, vx, vy)
  74.     elsif type == 8
  75.       @popup[i] = TShoot_Effect8.new(x, y, vx, vy)
  76.     elsif type == 9
  77.       @popup[i] = TShoot_Effect9.new(x, y, vx, vy)
  78.     elsif type == 10
  79.       @popup[i] = TShoot_Effect10.new(x, y, vx, vy)
  80.     elsif type == 11
  81.       @popup[i] = TShoot_Effect11.new(x, y, vx, vy)
  82.     elsif type == 12
  83.       @popup[i] = TShoot_Effect12.new(x, y, vx, vy)
  84.     elsif type == 13
  85.       @popup[i] = TShoot_Effect13.new(x, y, vx, vy)
  86.     elsif type == 14
  87.       @popup[i] = TShoot_Effect14.new(x, y, vx, vy)
  88.     elsif type == 15
  89.       @popup[i] = TShoot_Effect15.new(x, y, vx, vy)
  90.     end
  91.   end
复制代码
字会挂掉么...一开始我没看见字...
dsu_plus_rewardpost_czw
作者: iisnow    时间: 2012-4-30 18:27
请使用eval:
比如你中间那么一段其实就是:
eval("@bullet[i] = TShoot_EBullet#{type}.new(x, y, vx, vy, index, i)")
作者: end55rpg    时间: 2012-4-30 18:34
楼上正解
要么用eval要么哈西数组




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1