The RPG Maker Resource Kit

RMRK RPG Maker Creation => RPG Maker General => General Scripting => Topic started by: cozziekuns on June 15, 2010, 11:25:27 PM

Title: Trouble with animated characters in HUD [VX]
Post by: cozziekuns on June 15, 2010, 11:25:27 PM
Ok, so I've made a simple animated character in my HUD. Every 30 frames, the actor does a stepping animation. It's code (or what part matters) can be found here:


#==============================================================================
# ** Window_Base
#------------------------------------------------------------------------------
#  This is a superclass of all windows in the game.
#==============================================================================

class Window_Base < Window
  #--------------------------------------------------------------------------
  # * Draw Battler Character
  #     character_name  : Character graphic filename
  #     character_index : Character graphic index
  #     index : position of character
  #     x     : draw spot x-coordinate
  #     y     : draw spot y-coordinate
  #--------------------------------------------------------------------------
  def draw_battler_character(character_name, character_index, pos_index, x, y)
    return if character_name == nil
    bitmap = Cache.character(character_name)
    sign = character_name[/^[\!\$]./]
    if sign != nil and sign.include?('$')
      cw = bitmap.width / 3
      ch = bitmap.height / 4
    else
      cw = bitmap.width / 12
      ch = bitmap.height / 8
    end
    n = character_index
    src_rect = Rect.new((n % 4 * 3 + 1) * cw - cw * pos_index, (n / 4 * 4) * ch, cw, ch)
    self.contents.blt(x - cw / 2, y - ch, bitmap, src_rect)
  end
end

#==============================================================================
# ** Window_BattleStatus
#------------------------------------------------------------------------------
#  This window displays the status of all party members on the battle screen.
#==============================================================================

class Window_BattleStatus < Window_Selectable
#--------------------------------------------------------------------------
  # * Object Initialization
  #     x : window X coordinate
  #     y : window Y coordinate
  #--------------------------------------------------------------------------
  alias coz_omhud_bs_initialize initialize
  def initialize
    @char_index = -1
    @frames = 0
    coz_omhud_bs_initialize
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh(pos = 0)
    @pos = pos
    self.contents.clear
    for actor in $game_party.members
      draw_actor_face(actor, 2, actor.index * 96 + 2, 92)
      x = 104
      y = actor.index * 96 + WLH / 2
      draw_actor_name(actor, x, y)
      draw_actor_class(actor, x + 150, y)
      draw_icon(16, x + 126, y)
      draw_actor_level(actor, x, y + WLH * 1)
      draw_actor_state(actor, x, y + WLH * 2)
      draw_actor_hp(actor, x + 150, y + WLH * 1)
      draw_icon(99, x + 126, y + WLH * 1)
      draw_actor_mp(actor, x + 150, y + WLH * 2)
      draw_icon(100, x + 126, y + WLH * 2)
      draw_battler_character(actor.character_name, actor.character_index, @char_index, 48, 94)
    end
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    if @pos == 0
      @frames += 1
      if @frames == 30
        if @char_index < 1
          @char_index += 1
          @frames -= 30
          refresh
        else
          @char_index -= 2
          @frames -= 30
          refresh
        end
        if @char_index != 0 and @char_index != 1 and @char_index != -1
          @char_index == -1
        end
        refresh
      end
    elsif @pos == 1
      @frames += 2
      if @frames == 30
        if @char_index < 1
          @char_index += 1
          @frames -= 30
          refresh
        else
          @char_index -= 2
          @frames -= 30
          refresh
        end
        if @char_index != 0 and @char_index != 1 and @char_index != -1
          @char_index == -1
        end
        refresh
      end
    end
  end


The HUD is accompanied by a piece of code that's around 7 lines.


  class Scene_Battle < Scene_Base
  #--------------------------------------------------------------------------
  # * Update Party Command Selection
  #--------------------------------------------------------------------------
  alias coz_update_party_command_selection update_party_command_selection
  def update_party_command_selection
    @status_window.refresh(@party_command_window.index)
    coz_update_party_command_selection
  end
end


As you can probably see, the status window (Window_BattleStatus) refreshes according the the party command windows index. Should the player move the cursor to Escape, the actor's graphic should increase his movement from 30 frames per movement to 15 frames per movement. This usually happens, but is sometimes not the case. Sometimes, the actor's graphic may stop moving. I've tried checking if the @char_index is not -1, 0, or 1 (thus the @char_index checking code), but it doesn't seem to be working. I've also tried doing checks with @pos, but that doesn't seem to be working either.

Any ideas?
Title: Re: Trouble with animated characters in HUD [VX]
Post by: modern algebra on June 15, 2010, 11:36:13 PM
Well, it could be that the player is going over Escape at a time when @frames is an odd number. If that happens, then @frames would go from 29 to 31 and never hit 30, and therefore never be refreshed and never be reset, but keep increasing forever. A better way to do it would use a >= check, rather than an == check, and just set @frames to 0 instead of subtracting 30.

Anyway, aside from that, I don't think you should be refreshing the entire window every 15/30 frames just to change the actor position. I would either use a separate sprite or make a separate draw_sprite method that would only clear the rect where the sprite is and redraw there. Since it only happens twice/four times a second, it likely won't make a big difference in lag, but a good general rule to follow is not to do more than is necessary to achieve the result.
Title: Re: Trouble with animated characters in HUD [VX]
Post by: cozziekuns on June 15, 2010, 11:41:14 PM
I was experimenting with sprites, but I think I'm going to stick to drawing the sprite through window and refreshing it until I can get more familiar with them.

Anyway, it works fine! Thanks.