查看: 2457|回复: 5

[已经解决] 人物跟随出错

[复制链接]
梦石
0
星屑
3850
在线时间
1899 小时
注册时间
2010-6-19
回帖
917
发表于 2022-7-13 11:11:15 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 黑米馒头 于 2022-7-27 17:10 编辑

RGU了之后,人物跟随脚本出错

1.png

2.png

大佬说这里需要判断@move_list是否为nil,有大神知道怎么弄吗。。。


人物跟随

  1. module Train_Actor

  2. # 是否使用停止跟随的方法,也就是说,这里false改为true的时候,
  3. # 如果 TRANSPARENT_SWITCHES_INDEX

  4. # 开关打开,跟随的人物就消失了(其实只是变成透明而已)

  5. # 开关
  6. TRANSPARENT_SWITCH = true
  7. # 开关编号
  8. TRANSPARENT_SWITCHES_INDEX = 3

  9. # 举例:第一个为true,第二个为3,则打开3号开关,后面的人都没了。

  10. # 跟随人数的最大数目,可以更改为2、3、4什么的。

  11. TRAIN_ACTOR_SIZE_MAX = 4


  12. # 跟随距离,例如0为紧贴,1为分开一个身位

  13. TRAIN_ACTOR_DISTANCE = 1
  14. # 定数
  15. #Input::DOWN = 2
  16. #Input::LEFT = 4
  17. #Input::RIGHT = 6
  18. #Input::UP = 8
  19. DOWN_LEFT = 1
  20. DOWN_RIGHT = 3
  21. UP_LEFT = 7
  22. UP_RIGHT = 9
  23. JUMP = 5

  24. # 插入空角色,造成分开一段距离跟随的假象
  25. TRAIN_ACTOR_SIZE_MAX = TRAIN_ACTOR_SIZE_MAX * (TRAIN_ACTOR_DISTANCE+1) + 1

  26. #==============================================================================
  27. # ■ Game_Party_Actor
  28. #------------------------------------------------------------------------------
  29. # 
  30. #==============================================================================
  31. class Game_Party_Actor < Game_Character
  32. attr_reader  :name, :stop_name, :appellation, :name_color, :character_hue
  33. def initialize
  34.    super()
  35.    @name = ""
  36.    @appellation = ""
  37.    @name_color = 0
  38.    @through = true
  39.    @save = false
  40.    @character_hue
  41. end
  42. def setup(actor)
  43.    @actor = actor
  44.    # 设定角色的文件名和色调
  45.    if actor != nil
  46.      @name = actor.name
  47.      @appellation = actor.appellation
  48.      @name_color = actor.name_color
  49.      @character_name = actor.character_name
  50.      @character_hue = actor.character_hue
  51.      @character_hue = actor.battler_hue
  52.    else
  53.      @character_name = ""
  54.      @character_hue = 0
  55.      @name = @appellation = ""
  56.    end
  57.    # 初始化不透明度和合成方法
  58.    @opacity = 255
  59.    @blend_type = 0
  60. end
  61. def screen_z(height = 0)
  62.    if $game_player.x == @x and $game_player.y == @y
  63.     return $game_player.screen_z(height) - 1
  64.    end
  65.    super(height)
  66. end
  67. attr_writer :move_speed
  68. attr_writer :step_anime
  69. end

  70. #==============================================================================
  71. # ■ Spriteset_Map_Module
  72. #------------------------------------------------------------------------------
  73. #  
  74. #==============================================================================
  75. module Spriteset_Map_Module
  76. def setup_actor_character_sprites?
  77.    return @setup_actor_character_sprites_flag != nil
  78. end
  79. def setup_actor_character_sprites(characters)
  80.    if !setup_actor_character_sprites?
  81.      index_game_player = 0
  82.      @character_sprites.each_index do |i|
  83.        if @character_sprites[i].character.instance_of?(Game_Player)
  84.          index_game_player = i
  85.          break
  86.        end
  87.    end
  88.    for character in characters.reverse
  89.      @character_sprites.unshift(
  90.        Sprite_Character.new(@viewport1, character)
  91.        )
  92.    end
  93.    @setup_actor_character_sprites_flag = true
  94.   end
  95. end
  96. end

  97. #==============================================================================
  98. # ■ Scene_Map_Module
  99. #------------------------------------------------------------------------------
  100. #  
  101. #==============================================================================
  102. module Scene_Map_Module
  103. def setup_actor_character_sprites(characters)
  104.   $spriteset.setup_actor_character_sprites(characters)
  105. end
  106. end

  107. #==============================================================================
  108. # ■ Game_Party_Module
  109. #------------------------------------------------------------------------------
  110. #  
  111. #==============================================================================
  112. module Game_Party_Module
  113. def set_transparent_actors(transparent)
  114.    @transparent = transparent
  115. end
  116. def setup_actor_character_sprites
  117.    if @characters.nil?
  118.      @characters = []
  119.      for i in 1 ...  TRAIN_ACTOR_SIZE_MAX
  120.        @characters.push(Game_Party_Actor.new)
  121.      end
  122.    end
  123. for i in 1 ... TRAIN_ACTOR_SIZE_MAX
  124.   if i % (TRAIN_ACTOR_DISTANCE+1) == 0
  125.     @characters[i-1].setup(actors[i/(TRAIN_ACTOR_DISTANCE+1)])
  126.   end
  127. end
  128.   if $scene.class.method_defined?('setup_actor_character_sprites')
  129.     $scene.setup_actor_character_sprites(@characters)
  130.   end
  131. end
  132. def update_party_actors
  133.    setup_actor_character_sprites
  134.    transparent = $game_player.transparent
  135.   if transparent == false
  136.     if TRANSPARENT_SWITCH
  137.       transparent = $game_switches[TRANSPARENT_SWITCHES_INDEX]
  138.     end
  139.   end
  140.   for character in @characters
  141.     character.transparent = transparent
  142.     character.move_speed = $game_player.move_speed
  143.     character.update
  144.   end
  145. end
  146. def moveto_party_actors( x, y )
  147.    setup_actor_character_sprites
  148.    for character in @characters
  149.      character.moveto( x, y )
  150.    end
  151.    if @move_list == nil
  152.      @move_list = []
  153.    end
  154.    move_list_setup
  155. end
  156. def move_party_actors
  157.   if @move_list == nil
  158.     @move_list = []
  159.     move_list_setup
  160.   end
  161.   
  162. def move_list_setup
  163.   for i in 0 .. TRAIN_ACTOR_SIZE_MAX
  164.     @move_list[i] = nil
  165.   end
  166. end  
  167.   
  168.   @move_list.each_index do |i|
  169.   if @characters[i] != nil
  170.     case @move_list[i].type
  171.     when Input::DOWN
  172.       @characters[i].move_down(@move_list[i].args[0])
  173.     when Input::LEFT
  174.       @characters[i].move_left(@move_list[i].args[0])
  175.     when Input::RIGHT
  176.       @characters[i].move_right(@move_list[i].args[0])
  177.     when Input::UP
  178.       @characters[i].move_up(@move_list[i].args[0])
  179.     when DOWN_LEFT
  180.       @characters[i].move_lower_left
  181.     when DOWN_RIGHT
  182.       @characters[i].move_lower_right
  183.     when UP_LEFT
  184.       @characters[i].move_upper_left
  185.     when UP_RIGHT
  186.       @characters[i].move_upper_right
  187.     when JUMP
  188.       @characters[i].jump(@move_list[i].args[0],@move_list[i].args[1])
  189.     end
  190.   end
  191. end
  192. end

  193. #==============================================================================
  194. # ■ Move_List_Element
  195. #------------------------------------------------------------------------------
  196. #  
  197. #==============================================================================
  198. class Move_List_Element
  199.   def initialize(type,args)
  200.     @type = type
  201.     @args = args
  202.   end
  203.   def type()
  204.     return @type
  205.   end
  206.   def args()
  207.     return @args
  208.   end
  209. end
  210. def move_list_setup
  211.   for i in 0 .. TRAIN_ACTOR_SIZE_MAX
  212.     @move_list[i] = nil
  213.   end
  214. end
  215. def add_move_list(type,*args)
  216.   @move_list.unshift(Move_List_Element.new(type,args)).pop
  217. end
  218. def move_down_party_actors(turn_enabled = true)
  219.   move_party_actors
  220.   add_move_list(Input::DOWN,turn_enabled)
  221. end
  222. def move_left_party_actors(turn_enabled = true)
  223.   move_party_actors
  224.   add_move_list(Input::LEFT,turn_enabled)
  225. end
  226. def move_right_party_actors(turn_enabled = true)
  227.   move_party_actors
  228.   add_move_list(Input::RIGHT,turn_enabled)
  229. end
  230. def move_up_party_actors(turn_enabled = true)
  231.   move_party_actors
  232.   add_move_list(Input::UP,turn_enabled)
  233. end
  234. def move_lower_left_party_actors
  235.   move_party_actors
  236.   add_move_list(DOWN_LEFT)
  237. end
  238. def move_lower_right_party_actors
  239.   move_party_actors
  240.   add_move_list(DOWN_RIGHT)
  241. end
  242. def move_upper_left_party_actors
  243.   move_party_actors
  244.   add_move_list(UP_LEFT)
  245. end
  246. def move_upper_right_party_actors
  247.   move_party_actors
  248.   add_move_list(UP_RIGHT)
  249. end
  250. def jump_party_actors(x_plus, y_plus)
  251.   move_party_actors
  252.   add_move_list(JUMP,x_plus, y_plus)
  253. end
  254. end
  255. module Game_Player_Module
  256. def update
  257.   $game_party.update_party_actors
  258.   super
  259. end
  260. def moveto( x, y )
  261.   $game_party.moveto_party_actors( x, y )
  262.   super( x, y )
  263. end
  264. def move_down(turn_enabled = true)
  265.   if passable?(@x, @y, Input::DOWN)
  266.     $game_party.move_down_party_actors(turn_enabled)
  267.   #..........................................................................
  268.   elsif passable?(@x, @y, Input::LEFT) and passable?(@x - 1, @y, Input::DOWN) and
  269.         can_go?(@x, @y + 1)
  270.     @direction = 1
  271.     $game_party.move_lower_left_party_actors
  272.     increase_steps
  273.   elsif passable?(@x, @y, Input::RIGHT) and passable?(@x + 1, @y, Input::DOWN) and
  274.         can_go?(@x, @y + 1)
  275.     @direction = 3
  276.     $game_party.move_lower_right_party_actors
  277.     increase_steps
  278.   #..........................................................................
  279.   end
  280.   super(turn_enabled)
  281. end
  282. def move_left(turn_enabled = true)
  283.   if passable?(@x, @y, Input::LEFT)
  284.     $game_party.move_left_party_actors(turn_enabled)
  285.   #..........................................................................
  286.   elsif passable?(@x, @y, Input::UP) and passable?(@x, @y - 1, Input::LEFT) and
  287.         can_go?(@x - 1, @y)
  288.     @direction = 7
  289.     $game_party.move_upper_left_party_actors
  290.     increase_steps
  291.   elsif passable?(@x, @y, Input::DOWN) and passable?(@x, @y + 1, Input::LEFT) and
  292.         can_go?(@x - 1, @y)
  293.     @direction = 1
  294.     $game_party.move_lower_left_party_actors
  295.     increase_steps
  296.   #..........................................................................
  297.   end
  298.   super(turn_enabled)
  299. end
  300. def move_right(turn_enabled = true)
  301.   if passable?(@x, @y, Input::RIGHT)
  302.     $game_party.move_right_party_actors(turn_enabled)
  303.   #..........................................................................
  304.   elsif passable?(@x, @y, Input::UP) and passable?(@x, @y - 1, Input::RIGHT) and
  305.         can_go?(@x + 1, @y)
  306.     @direction = 9
  307.     $game_party.move_upper_right_party_actors
  308.     increase_steps
  309.   elsif passable?(@x, @y, Input::DOWN) and passable?(@x, @y + 1, Input::RIGHT) and
  310.         can_go?(@x + 1, @y)
  311.     @direction = 3
  312.     $game_party.move_lower_right_party_actors
  313.     increase_steps
  314.   #..........................................................................
  315.   end
  316.   super(turn_enabled)
  317. end
  318. def move_up(turn_enabled = true)
  319.   if passable?(@x, @y, Input::UP)
  320.     $game_party.move_up_party_actors(turn_enabled)
  321.   #..........................................................................
  322.   elsif passable?(@x, @y, Input::LEFT) and passable?(@x - 1, @y, Input::UP) and
  323.         can_go?(@x, @y - 1)
  324.     @direction = 7
  325.     $game_party.move_upper_left_party_actors
  326.     increase_steps
  327.   elsif passable?(@x, @y, Input::RIGHT) and passable?(@x + 1, @y, Input::UP) and
  328.         can_go?(@x, @y - 1)
  329.     @direction = 9
  330.     $game_party.move_upper_right_party_actors
  331.     increase_steps
  332.   #..........................................................................
  333.   end
  334.   super(turn_enabled)
  335. end
  336. def move_lower_left
  337.   # 下→左、左→下 のどちらかのコースが通行可能な場合
  338.   @direction = 1
  339.   if (passable?(@x, @y, Input::DOWN) and passable?(@x, @y + 1, Input::LEFT)) or
  340.      (passable?(@x, @y, Input::LEFT) and passable?(@x - 1, @y, Input::DOWN))
  341.     $game_party.move_lower_left_party_actors
  342.     increase_steps
  343.   #..........................................................................
  344.   elsif passable?(@x, @y, Input::DOWN) and can_go?(@x - 1, @y + 1)
  345.     $game_party.move_down_party_actors
  346.     @direction = 2
  347.     increase_steps
  348.   elsif passable?(@x, @y, Input::LEFT) and can_go?(@x - 1, @y + 1)
  349.     $game_party.move_left_party_actors
  350.     @direction = 4
  351.     increase_steps
  352.   #..........................................................................
  353.   end
  354.   super
  355. end
  356. def move_lower_right
  357.   # 下→右、右→下 のどちらかのコースが通行可能な場合
  358.   @direction = 3
  359.   if (passable?(@x, @y, Input::DOWN) and passable?(@x, @y + 1, Input::RIGHT)) or
  360.      (passable?(@x, @y, Input::RIGHT) and passable?(@x + 1, @y, Input::DOWN))
  361.     $game_party.move_lower_right_party_actors
  362.     increase_steps
  363.   #..........................................................................
  364.   elsif passable?(@x, @y, Input::DOWN) and can_go?(@x + 1, @y + 1)
  365.     $game_party.move_down_party_actors
  366.     @direction = 2
  367.     increase_steps
  368.   elsif passable?(@x, @y, Input::RIGHT) and can_go?(@x + 1, @y + 1)
  369.     $game_party.move_right_party_actors
  370.     @direction = 6
  371.     increase_steps
  372.   #..........................................................................
  373.   end
  374.   super
  375. end
  376. def move_upper_left
  377.   # 上→左、左→上 のどちらかのコースが通行可能な場合
  378.   @direction = 7
  379.   if (passable?(@x, @y, Input::UP) and passable?(@x, @y - 1, Input::LEFT)) or
  380.      (passable?(@x, @y, Input::LEFT) and passable?(@x - 1, @y, Input::UP))
  381.     $game_party.move_upper_left_party_actors
  382.     increase_steps
  383.   #..........................................................................
  384.   elsif passable?(@x, @y, Input::UP) and can_go?(@x - 1, @y - 1)
  385.     $game_party.move_up_party_actors
  386.     @direction = 8
  387.     increase_steps
  388.   elsif passable?(@x, @y, Input::LEFT) and can_go?(@x - 1, @y - 1)
  389.     $game_party.move_left_party_actors
  390.     @direction = 4
  391.     increase_steps
  392.   #..........................................................................
  393.   end
  394.   super
  395. end
  396. def move_upper_right
  397.   # 上→右、右→上 のどちらかのコースが通行可能な場合
  398.   @direction = 9
  399.   if (passable?(@x, @y, Input::UP) and passable?(@x, @y - 1, Input::RIGHT)) or
  400.      (passable?(@x, @y, Input::RIGHT) and passable?(@x + 1, @y, Input::UP))
  401.     $game_party.move_upper_right_party_actors
  402.     increase_steps
  403.   #..........................................................................
  404.   elsif passable?(@x, @y, Input::UP) and can_go?(@x + 1, @y - 1)
  405.     $game_party.move_up_party_actors
  406.     @direction = 8
  407.     increase_steps
  408.   elsif passable?(@x, @y, Input::RIGHT) and can_go?(@x + 1, @y - 1)
  409.     $game_party.move_right_party_actors
  410.     @direction = 6
  411.     increase_steps
  412.   #..........................................................................
  413.   end
  414.   super
  415. end
  416. def jump(x_plus, y_plus)
  417.   # 新しい座標を計算
  418.   new_x = @x + x_plus
  419.   new_y = @y + y_plus
  420.   # 加算値が (0,0) の場合か、ジャンプ先が通行可能な場合
  421.   if (x_plus == 0 and y_plus == 0) or passable?(new_x, new_y, 0)
  422.     $game_party.jump_party_actors(x_plus, y_plus)
  423.   end
  424.   super(x_plus, y_plus)
  425. end
  426. attr_reader :move_speed
  427. attr_reader :step_anime
  428. end
  429. end # module Train_Actor

  430. #==============================================================================
  431. # ■ Game_Party
  432. #------------------------------------------------------------------------------
  433. #  处理同伴的类。包含金钱以及物品的信息。本类的实例
  434. # 请参考 $game_party。
  435. #==============================================================================
  436. class Game_Party
  437.   include Train_Actor::Game_Party_Module
  438. end

  439. #==============================================================================
  440. # ■ Game_Player
  441. #------------------------------------------------------------------------------
  442. #  处理主角的类。事件启动的判定、以及地图的滚动等功能。
  443. # 本类的实例请参考 $game_player。
  444. #==============================================================================
  445. class Game_Player
  446.   include Train_Actor::Game_Player_Module
  447. end

  448. #==============================================================================
  449. # ■ Spriteset_Map
  450. #------------------------------------------------------------------------------
  451. #  处理地图画面活动块和元件的类。本类在
  452. # Scene_Map 类的内部使用。
  453. #==============================================================================
  454. class Spriteset_Map
  455.   include Train_Actor::Spriteset_Map_Module
  456. end

  457. #==============================================================================
  458. # ■ Scene_Map
  459. #------------------------------------------------------------------------------
  460. #  处理地图画面的类。
  461. #==============================================================================
  462. class Scene_Map
  463.   include Train_Actor::Scene_Map_Module
  464. end
复制代码
梦石
0
星屑
4756
在线时间
389 小时
注册时间
2012-11-8
回帖
235
发表于 2022-7-14 08:56:30 | 显示全部楼层
本帖最后由 qq634488405 于 2022-7-14 08:58 编辑
  1. if @move_list == nil
  2.     @move_list = []
  3.     move_list_setup
  4. end

  5. def move_list_setup
  6.   for i in 0 .. TRAIN_ACTOR_SIZE_MAX
  7.     @move_list[i] = nil
  8.   end
  9. end
  10. 当@move_list为nil时给@move_list塞一堆nil,然后判断时出错。可能加个判断@move_list[i]是否为nil能解决,但不清楚会不会影响其他功能
复制代码

点评

具体该怎么弄。。。  发表于 2022-7-14 12:46
回复

使用道具 举报

梦石
0
星屑
8003
在线时间
1184 小时
注册时间
2007-7-29
回帖
2014
发表于 2022-7-27 22:59:16 | 显示全部楼层
和之前情况类似,我只能从代码上来绕过出错提示,无法保证解决问题。话说其实楼上已经给出思路:
  1.   if @characters[i] != nil
复制代码

将出错提示行改成:
  1.   if @characters[i] != nil and @move_list[i] != nil
复制代码

点评

完美解决!感谢  发表于 2022-7-28 21:55
我看行  发表于 2022-7-28 15:15

评分

参与人数 1星屑 +50 收起 理由
RyanBern + 50 认可答案

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部