查看: 6015|回复: 3

[已经解决] 禁止卷动脚本更新Scene_Map时报错

[复制链接]
梦石
0
星屑
30103
在线时间
5741 小时
注册时间
2016-3-8
回帖
1662
发表于 2021-2-1 00:00:25 | 显示全部楼层 |阅读模式
悬赏125星屑已解决
本帖最后由 alexncf125 于 2021-2-1 00:27 编辑

这脚本用F9开啟3号变量, 回地图, 再用F9关闭3号变量, 最后回地图报错disposed window/disposed tilemap
我只找到原因出自这两三句:
scene = SceneManager.scene
scene.update_basic
scene.instance_variable_get(:@spriteset).update

平常用事件开关3,4号变量是会没事, 我就只想了解一下F9的BUG能修好的不?
脚本
  1. #==============================================================================
  2. # ★スクロール禁止 ver1.0 by USK
  3. #------------------------------------------------------------------------------
  4. # ・特定のスイッチがオンのときスクロールを禁止します。
  5. #==============================================================================
  6. =begin
  7. ・21行目のFixDisplayFlagVで設定した番号のスイッチがオンのとき
  8.   縦スクロールが禁止されます。
  9. ・22行目のFixDisplayFlagHで設定した番号のスイッチがオンのとき
  10.   横スクロールが禁止されます。
  11. =end

  12. #==============================================================================
  13. # ■ Game_Map
  14. #==============================================================================

  15. class Game_Map
  16.   #--------------------------------------------------------------------------
  17.   # ● 設定
  18.   #--------------------------------------------------------------------------
  19.   FixDisplayFlagV = 3 #禁止Y轴卷动的开关编号
  20.   FixDisplayFlagH = 4 #禁止X轴卷动的开关编号
  21.   #--------------------------------------------------------------------------
  22.   # ●
  23.   #--------------------------------------------------------------------------
  24.   alias :fix_display_scroll_down  :scroll_down
  25.   alias :fix_display_scroll_left  :scroll_left
  26.   alias :fix_display_scroll_right :scroll_right
  27.   alias :fix_display_scroll_up    :scroll_up
  28.   alias :fix_display_refresh      :refresh
  29.   alias :fix_display_valid?       :valid?
  30.   #--------------------------------------------------------------------------
  31.   # ●
  32.   #--------------------------------------------------------------------------
  33.   def fix_display?
  34.     $game_switches[FixDisplayFlagV] || $game_switches[FixDisplayFlagH]
  35.   end
  36.   #--------------------------------------------------------------------------
  37.   # ●
  38.   #--------------------------------------------------------------------------
  39.   def need_centering?
  40.     ret = !fix_display? &&
  41.           SceneManager.scene.is_a?(Scene_Map) &&
  42.           @on_fix_display
  43.     @on_fix_display = fix_display?
  44.     ret
  45.   end
  46.   #--------------------------------------------------------------------------
  47.   # ●
  48.   #--------------------------------------------------------------------------
  49.   def centering
  50.     div = 20
  51.     x = @display_x
  52.     dx = $game_player.x - (Graphics.width / 32 - 1) / 2.0 - x
  53.     dx /= div
  54.     y = @display_y
  55.     dy = $game_player.y - (Graphics.height / 32 - 1) / 2.0 - y
  56.     dy /= div
  57.     div.times do
  58.       set_display_pos(x, y)
  59.       x += dx
  60.       y += dy
  61.       scene = SceneManager.scene
  62.       scene.update_basic
  63.       scene.instance_variable_get(:@spriteset).update
  64.     end
  65.   end
  66.   #--------------------------------------------------------------------------
  67.   # ● リフレッシュ
  68.   #--------------------------------------------------------------------------
  69.   def refresh
  70.     centering if need_centering?
  71.     fix_display_refresh
  72.   end
  73.   #--------------------------------------------------------------------------
  74.   # ● 下にスクロール
  75.   #--------------------------------------------------------------------------
  76.   def scroll_down(distance)
  77.     fix_display_scroll_down(distance) unless $game_switches[FixDisplayFlagV]
  78.   end
  79.   #--------------------------------------------------------------------------
  80.   # ● 左にスクロール
  81.   #--------------------------------------------------------------------------
  82.   def scroll_left(distance)
  83.     fix_display_scroll_left(distance) unless $game_switches[FixDisplayFlagH]
  84.   end
  85.   #--------------------------------------------------------------------------
  86.   # ● 右にスクロール
  87.   #--------------------------------------------------------------------------
  88.   def scroll_right(distance)
  89.     fix_display_scroll_right(distance) unless $game_switches[FixDisplayFlagH]
  90.   end
  91.   #--------------------------------------------------------------------------
  92.   # ● 上にスクロール
  93.   #--------------------------------------------------------------------------
  94.   def scroll_up(distance)
  95.     fix_display_scroll_up(distance) unless $game_switches[FixDisplayFlagV]
  96.   end
  97.   #--------------------------------------------------------------------------
  98.   # ● 有効座標判定
  99.   #--------------------------------------------------------------------------
  100.   def valid?(x, y)
  101.     ret = fix_display_valid?(x, y)
  102.     if $game_switches[FixDisplayFlagV]
  103.       ret &&= y >= @display_y && y < @display_y + (Graphics.height >> 5)
  104.     end
  105.     if $game_switches[FixDisplayFlagH]
  106.       ret &&= x >= @display_x && x < @display_x + (Graphics.width >> 5)
  107.     end
  108.     ret
  109.   end
  110. end
复制代码

最佳答案

查看完整内容

这波是脚本语言的灵活性以及脚本作者的偷懒二者共同背锅。 因为不到运行时不会检查方法能否运行,所以如果不出现预想以外的使用场景,就没事。 但是这也为脚本制作者提供了一个偷懒的机会,那就是——我可以少加异常处理、错误判断, 反正我只保证在xx条件下不出错,使用脚本出错了就怪使用者操作太骚。 在这里就很明显,大部分的Scene是没有Spriteset的(实际上,也就Scene_Map和Scene_Battle是有的) 所以取@spriteset这个 ...
梦石
0
星屑
30103
在线时间
5741 小时
注册时间
2016-3-8
回帖
1662
 楼主| 发表于 2021-2-1 00:22:48 | 显示全部楼层
本帖最后由 alexncf125 于 2021-2-1 23:18 编辑

试著插入这段
  1. module SceneManager

  2.   @last_scene = nil
  3.   
  4.   def self.last_scene
  5.     @last_scene
  6.   end

  7.   class << self
  8.     alias last_scene_call call
  9.   end
  10.   def self.call(scene_class)
  11.     @last_scene = @scene
  12.     last_scene_call(scene_class)
  13.   end

  14.   def self.return
  15.     @last_scene = @scene
  16.     @scene = @stack.pop
  17.   end

  18. end
复制代码

和把那段改成
last_scene = SceneManager.last_scene
return if last_scene.is_a?(Scene_Debug)
scene = SceneManager.scene
scene.update_basic
scene.instance_variable_get(:@spriteset).update

好像能解決掉问题

能请大佬们指教一下, 原先那两句有啥用途??是必须写的么??
我这样用return跳过它们没问题吧
回复

使用道具 举报

梦石
10
星屑
39550
在线时间
1919 小时
注册时间
2010-11-14
回帖
3214

R考场第七期纪念奖

发表于 2021-2-1 00:00:26 | 显示全部楼层
本帖最后由 KB.Driver 于 2021-2-1 22:48 编辑
alexncf125 发表于 2021-2-1 00:22
[fold=试著插入这段]module SceneManager

  @last_scene = nil


这波是脚本语言的灵活性以及脚本作者的偷懒二者共同背锅。

因为不到运行时不会检查方法能否运行,所以如果不出现预想以外的使用场景,就没事。

但是这也为脚本制作者提供了一个偷懒的机会,那就是——我可以少加异常处理、错误判断,
反正我只保证在xx条件下不出错,使用脚本出错了就怪使用者操作太骚。

在这里就很明显,大部分的Scene是没有Spriteset的(实际上,也就Scene_Map和Scene_Battle是有的)
所以取@spriteset这个违反OOP的投机写法(虽然ruby就是很灵活,就是可以这样写,但不代表它是好的)
如果不用if语句控制条件或者用begin...end的异常处理包裹,它就是很脆弱的(不具健壮性,robust)

至于出错的原因,个人推理如下:
修改$game_variables(开关也是一样),会触发on_change方法,
从而使$game_map.need_refresh = true,令Game_Map触发refresh方法。
QQ截图20210201224116.jpg

在Scene_Map或者Scene_Battle中这样做没有影响,因为它们有@spriteset变量

但是在Scene_Debug里修改变量,一样触发Game_Map的refresh,
你所用的脚本又修改了这个refresh,让它对当前场景的@spriteset进行更新
这个变量在Scene_Debug里并不存在,这就产生错误了。

===================================================================

这给我们带来什么启示呢?
1、写法投机的脚本往往不具备健壮性,建议自己用或者给身边人玩玩就好,拿出来大规模分享就不必了。
2、如果真要大规模分享,请多从使用者的角度考虑,提高兼容性,减少代码出错的可能。


评分

参与人数 2星屑 +60 赞 +1 收起 理由
fux2 + 60 认可答案
alexncf125 + 1 万分感谢!!!!!!!!!!.

查看全部评分

用头画头像,用脚写脚本
回复

使用道具 举报

梦石
0
星屑
30103
在线时间
5741 小时
注册时间
2016-3-8
回帖
1662
 楼主| 发表于 2021-2-2 00:44:41 | 显示全部楼层
本帖最后由 alexncf125 于 2021-2-26 17:00 编辑

万分感谢!!!!!!!!!!

经过日历大佬的指点, 终于改到没bug了~

  1. #==============================================================================
  2. # ★スクロール禁止 ver1.0 by USK
  3. #------------------------------------------------------------------------------
  4. # ・特定のスイッチがオンのときスクロールを禁止します。
  5. #==============================================================================
  6. =begin
  7. ・21行目のFixDisplayFlagVで設定した番号のスイッチがオンのとき
  8.   縦スクロールが禁止されます。
  9. ・22行目のFixDisplayFlagHで設定した番号のスイッチがオンのとき
  10.   横スクロールが禁止されます。
  11. =end
  12. ##########
  13. module SceneManager

  14.   @last_scene = nil
  15.   
  16.   def self.last_scene
  17.     @last_scene
  18.   end
  19.    
  20.   class << self; alias last_scene_call call; end
  21.   def self.call(scene_class)
  22.     @last_scene = @scene
  23.     last_scene_call(scene_class)
  24.   end
  25.    
  26.   def self.return
  27.     @last_scene = @scene
  28.     @scene = @stack.pop
  29.   end
  30.   
  31.   def self.refresh_scene
  32.     @last_scene = @scene
  33.   end
  34.    
  35. end
  36. ##########
  37. #==============================================================================
  38. # ■ Game_Map
  39. #==============================================================================

  40. class Game_Map
  41.   #--------------------------------------------------------------------------
  42.   # ● 設定
  43.   #--------------------------------------------------------------------------
  44.   FixDisplayFlagV = 3 #禁止Y轴卷动的开关编号
  45.   FixDisplayFlagH = 4 #禁止X轴卷动的开关编号
  46.   #--------------------------------------------------------------------------
  47.   # ●
  48.   #--------------------------------------------------------------------------
  49.   alias :fix_display_scroll_down  :scroll_down
  50.   alias :fix_display_scroll_left  :scroll_left
  51.   alias :fix_display_scroll_right :scroll_right
  52.   alias :fix_display_scroll_up    :scroll_up
  53.   alias :fix_display_refresh      :refresh
  54.   alias :fix_display_valid?       :valid?
  55.   #--------------------------------------------------------------------------
  56.   # ●
  57.   #--------------------------------------------------------------------------
  58.   def fix_display?
  59.     $game_switches[FixDisplayFlagV] || $game_switches[FixDisplayFlagH]
  60.   end
  61.   #--------------------------------------------------------------------------
  62.   # ●
  63.   #--------------------------------------------------------------------------
  64.   def need_centering?
  65.     ret = !fix_display? &&
  66.           SceneManager.scene.is_a?(Scene_Map) &&
  67.           @on_fix_display
  68.     @on_fix_display = fix_display?
  69.     ret
  70.   end
  71.   #--------------------------------------------------------------------------
  72.   # ●
  73.   #--------------------------------------------------------------------------
  74.   def centering
  75.     div = 20
  76.     x = @display_x
  77.     dx = $game_player.x - (Graphics.width / 32 - 1) / 2.0 - x
  78.     dx /= div
  79.     y = @display_y
  80.     dy = $game_player.y - (Graphics.height / 32 - 1) / 2.0 - y
  81.     dy /= div
  82.     div.times do
  83.       set_display_pos(x, y)
  84.       x += dx
  85.       y += dy
  86.       scene = SceneManager.scene
  87.       scene.update_basic
  88.       scene.instance_variable_get(:@spriteset).update
  89.     end
  90.   end
  91.   #--------------------------------------------------------------------------
  92.   # ● リフレッシュ
  93.   #--------------------------------------------------------------------------
  94.   def refresh
  95.     ##########
  96.     last_scene = SceneManager.last_scene
  97.     if last_scene.is_a?(Scene_Debug)
  98.       SceneManager.refresh_scene
  99.       return fix_display_refresh
  100.     end
  101.     ##########
  102.     centering if need_centering?
  103.     fix_display_refresh
  104.   end
  105.   #--------------------------------------------------------------------------
  106.   # ● 下にスクロール
  107.   #--------------------------------------------------------------------------
  108.   def scroll_down(distance)
  109.     fix_display_scroll_down(distance) unless $game_switches[FixDisplayFlagV]
  110.   end
  111.   #--------------------------------------------------------------------------
  112.   # ● 左にスクロール
  113.   #--------------------------------------------------------------------------
  114.   def scroll_left(distance)
  115.     fix_display_scroll_left(distance) unless $game_switches[FixDisplayFlagH]
  116.   end
  117.   #--------------------------------------------------------------------------
  118.   # ● 右にスクロール
  119.   #--------------------------------------------------------------------------
  120.   def scroll_right(distance)
  121.     fix_display_scroll_right(distance) unless $game_switches[FixDisplayFlagH]
  122.   end
  123.   #--------------------------------------------------------------------------
  124.   # ● 上にスクロール
  125.   #--------------------------------------------------------------------------
  126.   def scroll_up(distance)
  127.     fix_display_scroll_up(distance) unless $game_switches[FixDisplayFlagV]
  128.   end
  129.   #--------------------------------------------------------------------------
  130.   # ● 有効座標判定
  131.   #--------------------------------------------------------------------------
  132.   def valid?(x, y)
  133.     ret = fix_display_valid?(x, y)
  134.     if $game_switches[FixDisplayFlagV]
  135.       ret &&= y >= @display_y && y < @display_y + (Graphics.height >> 5)
  136.     end
  137.     if $game_switches[FixDisplayFlagH]
  138.       ret &&= x >= @display_x && x < @display_x + (Graphics.width >> 5)
  139.     end
  140.     ret
  141.   end
  142. end
  143. ##########
  144. class Scene_Map < Scene_Base
  145.   alias :fix_display_start :start
  146.   def start
  147.     fix_display_start
  148.     $game_map.centering if $game_map.need_centering?
  149.   end
  150. end
  151. ##########
复制代码

用两个##########包裹的是新增的內容
回复

使用道具 举报

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

本版积分规则

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

在本版发帖返回顶部