查看: 3951|回复: 8

[已经解决] 走一步才刷新一下的脚本如何实现?

[复制链接]
梦石
0
星屑
208
在线时间
420 小时
注册时间
2020-1-11
回帖
175
发表于 2020-10-11 17:56:50 | 显示全部楼层 |阅读模式

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

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

×
想要实现一个,在行走时会消耗能源的功能。
而且是要直接反应在窗口上。窗口写好了。但是这个监听功能绑在帧更新上好像太浪费内存了。(一秒60次)
所以想角色行走一步监听一次。扣一次能源并在画面窗口显示出来。

PS 窗口,显示、描画的问题我都解决了。就是刷新要绑那里这个问题。多谢多谢。
梦石
0
星屑
42345
在线时间
6022 小时
注册时间
2006-11-10
回帖
6975
发表于 2020-10-11 18:03:31 | 显示全部楼层
if 记录变量 != 当前步数
    刷新
    记录变量 = 当前步数
end
回复 1 0

使用道具 举报

梦石
0
星屑
30103
在线时间
5741 小时
注册时间
2016-3-8
回帖
1662
发表于 2020-10-11 18:26:41 | 显示全部楼层
本帖最后由 alexncf125 于 2020-10-11 21:12 编辑

參考一下下面的脚本好了,  跑步跑一步扣7次, 原地停留每10帧加1次
208行: return unless moving?
209行: return unless dash?
222行: return unless Graphics.frame_count % YES::DASH::RESTORE_AFTER == 0
  1. #==============================================================================
  2. #
  3. # ▼ Yami Engine Symphony - Dash Stamina
  4. # -- Last Updated: 2013.02.27
  5. # -- Level: Easy
  6. # -- Requires: n/a
  7. #
  8. #==============================================================================

  9. $imported = {} if $imported.nil?
  10. $imported["YES-DashStamina"] = true

  11. #==============================================================================
  12. # ▼ Updates
  13. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  14. # 2013.02.27 - Added recover method.
  15. #            - Added rest area feature.
  16. # 2012.11.18 - Fixed Enable Window option.
  17. # 2012.11.17 - Added recovery frames.
  18. #            - Added stamina variable.
  19. #            - Fixed recovery problem when holding Shift.
  20. # 2012.11.15 - Started and Finished Script.
  21. #
  22. #==============================================================================
  23. # ▼ Introduction
  24. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  25. # This script provides stamina feature for dashing on map. Dashing will comsume
  26. # stamina and be disable if run our of stamina.
  27. #
  28. #==============================================================================
  29. # ▼ Instructions
  30. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  31. # To install this script, open up your script editor and copy/paste this script
  32. # to an open slot below ▼ Materials/素材 but above ▼ Main. Remember to save.
  33. #
  34. # -----------------------------------------------------------------------------
  35. # Script Calls - These commands are used with script calls.
  36. # -----------------------------------------------------------------------------
  37. # YES.recover_stamina
  38. # Fully recover stamina for player.
  39. #
  40. # YES.recover_stamina(X)
  41. # Recover X stamina for player.
  42. #
  43. #==============================================================================
  44. # ▼ Compatibility
  45. # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  46. # This script is made strictly for RPG Maker VX Ace. It is highly unlikely that
  47. # it will run with RPG Maker VX without adjustments.
  48. #
  49. #==============================================================================

  50. #==============================================================================
  51. # ■ Configuration
  52. #==============================================================================

  53. module YES
  54.   module DASH
  55.    
  56.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  57.     # - General Settings -
  58.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  59.     STAMINA_DEFAULT    = 200    # Default Stamina for player.
  60.     STAMINA_PER_FRAME  = 1      # Default Stamina cost per frame while dashing.
  61.     MOVE_RESTORE       = false  # Set this to false to disable restoring
  62.                                 # stamina while moving.
  63.     RECOVER_FRAMES     = 120    # Start recovering after X frames.
  64.     STAMINA_RESTORE    = 1      # Restoring Stamina per X frames.
  65.     RESTORE_AFTER      = 10     # Restore Stamina after X frames.
  66.     REST_AREA_RATE     = 1      # Restore rate when standing on rest area.
  67.                                 # Set this to 1 to disable rest area function.
  68.     REST_AREA_REGION   = 19     # Region ID of rest area.
  69.    
  70.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  71.     # - Switches and Variables Settings -
  72.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  73.     STAMINA_VARIABLE   = 1100      # Variable to control stamina.
  74.     DISABLE_SWITCH     = 98      # Switch to toggle Stamina Feature.
  75.                                  # Set to true to disable Stamina Feature.
  76.    
  77.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  78.     # - Windows Settings -
  79.     #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  80.     ENABLE_WINDOW      = true   # Toggle stamina window.
  81.     AUTO_HIDE_WINDOW   = false   # Toggle auto-hide function for Stamina Window.
  82.     HIDE_AFTER_FRAMES  = 300    # Hide after X frames if not dashing.
  83.     STAMINA_TEXT       = "耐力"
  84.     WINDOW_WIDTH       = 180
  85.     BAR_COLORS         = { # Settings for stamina bar colors.
  86.       :color1          => 28,
  87.       :color2          => 29,
  88.     } # Do not remove this.
  89.    
  90.   end # DASH
  91. end # YES

  92. #==============================================================================
  93. # ▼ Editting anything past this point may potentially result in causing
  94. # computer damage, incontinence, explosion of user's head, coma, death, and/or
  95. # halitosis so edit at your own risk.
  96. #==============================================================================

  97. #==============================================================================
  98. # ■ Module Yami Engine Symphony
  99. #==============================================================================

  100. module YES
  101.   
  102.   #--------------------------------------------------------------------------
  103.   # self.recover_stamina
  104.   #--------------------------------------------------------------------------
  105.   def self.recover_stamina(amount = :full)
  106.     $game_player.recover_stamina(amount)
  107.   end
  108.   
  109. end # YES

  110. #==============================================================================
  111. # ■ Game_Player
  112. #==============================================================================

  113. class Game_Player < Game_Character
  114.   
  115.   #--------------------------------------------------------------------------
  116.   # * Public Instance Variables
  117.   #--------------------------------------------------------------------------
  118.   attr_accessor :stamina_backup
  119.   
  120.   #--------------------------------------------------------------------------
  121.   # alias method: initialize
  122.   #--------------------------------------------------------------------------
  123.   alias dash_initialize initialize
  124.   def initialize
  125.     dash_initialize
  126.     #---
  127.     @stamina = YES::DASH::STAMINA_DEFAULT
  128.     $game_variables[YES::DASH::STAMINA_VARIABLE] = YES::DASH::STAMINA_DEFAULT
  129.     #---
  130.     @recover_frames = 0
  131.     @stamina_backup = {}
  132.   end
  133.    
  134.   #--------------------------------------------------------------------------
  135.   # new method: stamina
  136.   #--------------------------------------------------------------------------
  137.   def stamina
  138.     return @stamina
  139.   end
  140.    
  141.   #--------------------------------------------------------------------------
  142.   # new method: stamina_max
  143.   #--------------------------------------------------------------------------
  144.   def stamina_max
  145.     $game_variables[YES::DASH::STAMINA_VARIABLE]
  146.   end
  147.   
  148.   #--------------------------------------------------------------------------
  149.   # new method: stamina_rate
  150.   #--------------------------------------------------------------------------
  151.   def stamina_rate
  152.     stamina.to_f / stamina_max.to_f
  153.   end
  154.   
  155.   #--------------------------------------------------------------------------
  156.   # new method: stamina_cost
  157.   #--------------------------------------------------------------------------
  158.   def stamina_cost
  159.     [YES::DASH::STAMINA_PER_FRAME, @stamina].min
  160.   end
  161.   
  162.   #--------------------------------------------------------------------------
  163.   # new method: recover_stamina
  164.   #--------------------------------------------------------------------------
  165.   def recover_stamina(amount = :full)
  166.     if amount.is_a?(Integer)
  167.       @stamina += amount
  168.       correct_stamina
  169.     else
  170.       @stamina = stamina_max
  171.     end
  172.   end
  173.   
  174.   #--------------------------------------------------------------------------
  175.   # alias method: update
  176.   #--------------------------------------------------------------------------
  177.   alias dash_update update
  178.   def update
  179.     dash_update
  180.     update_dash_stamina
  181.     #---
  182.     correct_stamina
  183.     #---
  184.     update_stamina_recover
  185.   end
  186.   
  187.   #--------------------------------------------------------------------------
  188.   # new method: correct_stamina
  189.   #--------------------------------------------------------------------------
  190.   def correct_stamina
  191.     @stamina = stamina_max if @stamina > stamina_max
  192.     @stamina = 0 if @stamina < 0
  193.   end
  194.   
  195.   #--------------------------------------------------------------------------
  196.   # new method: update_dash_stamina
  197.   #--------------------------------------------------------------------------
  198.   def update_dash_stamina
  199.     return unless moving?
  200.     return unless dash?
  201.     @stamina = @stamina - stamina_cost
  202.     @stamina = 0 if @stamina < 0
  203.     @recover_frames = YES::DASH::RECOVER_FRAMES
  204.   end
  205.   
  206.   #--------------------------------------------------------------------------
  207.   # new method: update_stamina_recover
  208.   #--------------------------------------------------------------------------
  209.   def update_stamina_recover
  210.     @recover_frames -= 1
  211.     return if @recover_frames > 0
  212.     return if moving? && YES::DASH::MOVE_RESTORE
  213.     return unless Graphics.frame_count % YES::DASH::RESTORE_AFTER == 0
  214.     return correct_stamina if @stamina >= stamina_max
  215.     @stamina += stamina_recover
  216.     @stamina = stamina_max if @stamina > stamina_max
  217.   end
  218.    
  219.   #--------------------------------------------------------------------------
  220.   # alias method: dash?
  221.   #--------------------------------------------------------------------------
  222.   alias stamina_dash? dash?
  223.   def dash?
  224.     return false if @stamina <= 0 && !$game_switches[YES::DASH::DISABLE_SWITCH]
  225.     return stamina_dash?
  226.   end
  227.   
  228.   #--------------------------------------------------------------------------
  229.   # new method: stamina_recover
  230.   #--------------------------------------------------------------------------
  231.   def stamina_recover
  232.     YES::DASH::STAMINA_RESTORE * rest_area_rate
  233.   end
  234.   
  235.   #--------------------------------------------------------------------------
  236.   # new method: on_rest_area?
  237.   #--------------------------------------------------------------------------
  238.   def on_rest_area?
  239.     region_id == YES::DASH::REST_AREA_REGION
  240.   end
  241.   
  242.   #--------------------------------------------------------------------------
  243.   # new method: rest_area_rate
  244.   #--------------------------------------------------------------------------
  245.   def rest_area_rate
  246.     on_rest_area? ? YES::DASH::REST_AREA_RATE : 1
  247.   end
  248.   
  249. end # Game_Player

  250. #==============================================================================
  251. # ■ Window_Stamina
  252. #==============================================================================

  253. class Window_Stamina < Window_Base

  254.   #--------------------------------------------------------------------------
  255.   # initialize
  256.   #--------------------------------------------------------------------------
  257.   def initialize
  258.     super(24, Graphics.height - 74, YES::DASH::WINDOW_WIDTH, 50)
  259.     @time = YES::DASH::HIDE_AFTER_FRAMES
  260.     refresh
  261.     #---
  262.     $game_player.stamina_backup[:x] ||= self.x
  263.     $game_player.stamina_backup[:time] ||= @time
  264.     self.x = $game_player.stamina_backup[:x]
  265.     @time = $game_player.stamina_backup[:time]
  266.   end
  267.   
  268.   #--------------------------------------------------------------------------
  269.   # refresh
  270.   #--------------------------------------------------------------------------
  271.   def refresh
  272.     return if @stamina && @stamina == $game_player.stamina
  273.     contents.clear
  274.     #---
  275.     draw_stamina_bar(0, 0, YES::DASH::WINDOW_WIDTH - 24)
  276.     #---
  277.     @stamina = $game_player.stamina
  278.   end
  279.   
  280.   #--------------------------------------------------------------------------
  281.   # stamina_bar_color1
  282.   #--------------------------------------------------------------------------
  283.   def stamina_bar_color1
  284.     text_color(YES::DASH::BAR_COLORS[:color1])
  285.   end
  286.   
  287.   #--------------------------------------------------------------------------
  288.   # stamina_bar_color2
  289.   #--------------------------------------------------------------------------
  290.   def stamina_bar_color2
  291.     text_color(YES::DASH::BAR_COLORS[:color2])
  292.   end
  293.   
  294.   #--------------------------------------------------------------------------
  295.   # draw_stamina_bar
  296.   #--------------------------------------------------------------------------
  297.   def draw_stamina_bar(x, y, width = 156)
  298.     draw_gauge(x, y, width,$game_player.stamina_rate, stamina_bar_color1, stamina_bar_color2)
  299.     change_color(system_color)
  300.     draw_text(x, y, contents.width, line_height, YES::DASH::STAMINA_TEXT)
  301.     draw_current_and_max_values(x, y, width, $game_player.stamina,
  302.       $game_player.stamina_max, normal_color, normal_color)
  303.   end
  304.    
  305.   #--------------------------------------------------------------------------
  306.   # update
  307.   #--------------------------------------------------------------------------
  308.   def update
  309.     super
  310.     update_hide
  311.     update_show
  312.     update_input
  313.     refresh
  314.     $game_switches[YES::DASH::DISABLE_SWITCH] ? self.hide : self.show
  315.     self.hide unless YES::DASH::ENABLE_WINDOW
  316.   end
  317.   
  318.   #--------------------------------------------------------------------------
  319.   # update_hide
  320.   #--------------------------------------------------------------------------
  321.   def update_hide
  322.     return unless YES::DASH::AUTO_HIDE_WINDOW
  323.     return if $game_player.dash?
  324.     @time -= 1
  325.     return unless @time <= 0
  326.     #---
  327.     self.x -= 9 if self.x > -self.width
  328.   end
  329.   
  330.   #--------------------------------------------------------------------------
  331.   # update_show
  332.   #--------------------------------------------------------------------------
  333.   def update_show
  334.     return unless YES::DASH::AUTO_HIDE_WINDOW
  335.     return unless @show
  336.     @time = YES::DASH::HIDE_AFTER_FRAMES
  337.     self.x += 8 if self.x < 24
  338.     @show = false if self.x >= 24
  339.   end
  340.   
  341.   #--------------------------------------------------------------------------
  342.   # update_input
  343.   #--------------------------------------------------------------------------
  344.   def update_input
  345.     return unless $game_player.dash?
  346.     return unless $game_player.moving?
  347.     @show = true
  348.   end
  349.   
  350.   #--------------------------------------------------------------------------
  351.   # dispose
  352.   #--------------------------------------------------------------------------
  353.   def dispose
  354.     $game_player.stamina_backup[:x] = self.x
  355.     $game_player.stamina_backup[:time] = @time
  356.     super
  357.   end
  358.   
  359. end # Window_Stamina

  360. #==============================================================================
  361. # ■ Scene_Map
  362. #==============================================================================

  363. class Scene_Map < Scene_Base
  364.   
  365.   #--------------------------------------------------------------------------
  366.   # alias method: create_all_windows
  367.   #--------------------------------------------------------------------------
  368.   alias dash_create_all_windows create_all_windows
  369.   def create_all_windows
  370.     dash_create_all_windows
  371.     create_dash_window
  372.   end
  373.   
  374.   #--------------------------------------------------------------------------
  375.   # new method: create_dash_window
  376.   #--------------------------------------------------------------------------
  377.   def create_dash_window
  378.     @stamina_window = Window_Stamina.new
  379.   end
  380.   
  381. end # Scene_Map

  382. #==============================================================================
  383. #
  384. # ▼ End of File
  385. #
  386. #==============================================================================
复制代码
回复 1 0

使用道具 举报

老鹰

梦石
40
星屑
38044
在线时间
7127 小时
注册时间
2012-5-26
回帖
3306

极短24评委极短23参与极短22参与极短21评委老司机慢点开短篇十吟唱者组别冠军开拓者剧作品鉴家

发表于 2020-10-11 19:01:38 | 显示全部楼层
那就只有在数值变动时才重绘窗口啊
存一个当前已经绘制了的值(如果窗口dispose了就置nil,防止再开时不绘制)
如果当前值和已经绘制了的值相同,就不重绘

效率损耗都是在绘制上的,其他数值更新没啥
回复 1 0

使用道具 举报

梦石
0
星屑
208
在线时间
420 小时
注册时间
2020-1-11
回帖
175
 楼主| 发表于 2020-10-11 19:03:47 | 显示全部楼层
灯笼菜刀王 发表于 2020-10-11 18:03
if 记录变量 != 当前步数
    刷新
    记录变量 = 当前步数

我试了下这个脚本。虽然可能没有描绘那么多次了。
但是我 p 了那个反映到窗口的数值,还是一秒60次在那儿蹦………
那电脑内存根本就没少多少事嘛!
也许在 class Scene_Base
#--------------------------------------------------------------------------
  # ● 更新所有窗口
  #--------------------------------------------------------------------------
  def update_all_windows
    instance_variables.each do |varname|
      ivar = instance_variable_get(varname)
      ivar.update if ivar.is_a?(Window)
    end
  end
即使是没有 update 的 继承 Window_Base 的新建窗口中,吊在那里准备描画的数值同样被1秒60次的调用。
我试著把 refresh 去掉都不行。
好吧,电脑这点996实在太过九牛一毛了,我们人类就不要瞎费心了。

点评

会拖累的是重复描绘窗口, 而你是步数产生变化的时候才需要更新, 那 当新步数 != 旧步数时刷新, 然后让它们一致, 这样就可以避免持续描绘窗口了  发表于 2020-10-11 19:53
回复

使用道具 举报

梦石
0
星屑
208
在线时间
420 小时
注册时间
2020-1-11
回帖
175
 楼主| 发表于 2020-10-11 19:09:23 | 显示全部楼层
alexncf125 发表于 2020-10-11 18:26
參考一下好了,  下面的脚本跑步跑一步会扣7次, 原地停留每10帧加1次
208行: return unless moving?
209行:  ...

非常感谢,我只是想优化一下运算的内存,真的没想要弄个几百行的脚本。
我有自知之明,搞不定的就放著不管。再次感谢。

点评

所以说是拿来參考嘛  发表于 2020-10-11 19:16
回复

使用道具 举报

梦石
0
星屑
30103
在线时间
5741 小时
注册时间
2016-3-8
回帖
1662
发表于 2020-10-11 19:18:53 | 显示全部楼层
alianlord 发表于 2020-10-11 19:09
非常感谢,我只是想优化一下运算的内存,真的没想要弄个几百行的脚本。
我有自知之明,搞不定的就放著不 ...

或者你可以这样写
  1. class Game_Party
  2.   alias update_increase_steps increase_steps
  3.   def increase_steps
  4.     你要執行的內容
  5.     update_increase_steps
  6.   end
  7. end
复制代码
回复

使用道具 举报

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

本版积分规则

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

在本版发帖返回顶部