Home > RGSS Script Reference > Sprite_Character
Inherits from: Sprite
Description: This class performs graphical changes on battler sprites, such as updating whether the battler graphic is visible, making it flash before attacking, and showing damage dealt to the battler.
class Sprite_Character < RPG::Sprite
def initialize(viewport, character = nil)
@character = character
if @tile_id != @character.tile_id or
@character_name != @character.character_name or
@character_hue != @character.character_hue
@tile_id = @character.tile_id
@character_name = @character.character_name
@character_hue = @character.character_hue
if @tile_id >= 384
self.bitmap = RPG::Cache.tile($game_map.tileset_name,
self.src_rect.set(0, 0, 32, 32)
self.ox = 16
self.oy = 32
self.bitmap = RPG::Cache.character(@character.character_name,
@cw = bitmap.width / 4
@ch = bitmap.height / 4
self.ox = @cw / 2
self.oy = @ch
self.visible = (not @character.transparent)
if @tile_id == 0
sx = @character.pattern * @cw
sy = (@character.direction - 2) / 2 * @ch
self.src_rect.set(sx, sy, @cw, @ch)
self.x = @character.screen_x
self.y = @character.screen_y
self.z = @character.screen_z(@ch)
self.opacity = @character.opacity
self.blend_type = @character.blend_type
self.bush_depth = @character.bush_depth
if @character.animation_id != 0
animation = $data_animations[@character.animation_id]
@character.animation_id = 0
Character: The Game_Character object this sprite represents.
Tile_ID: If the character's graphic is drawn from a tileset rather than a character set file, then this value is set to the ID of that tile.
Character_Name: The filename of the character graphic.
Character_Hue: The character sprite's hue modification value.
CW: The width of the character graphic, in pixels.
CH: The height of the character graphic, in pixels.
Local Variables: None
How it Works: This method sets the character to which the sprite belongs and updates the character so that all the other instances variables are initialized to their proper values immediately.
Local Variables: None
How it Works: This method updates the character sprite each frame. First, the call to super does the updating that is common to all sprites. Then, the method checks to see if the value of @tile_id differs from the character's old tile ID ,the value of @character_name differs from from the character's old filename, or the value of @character_hue differs from the character's old hue modification due to a "Move Event" command changing these values. If any of these is true, then the values of @tile_id, @character_name, and @character_hue are all updated to reflect the character's new settings. If the tile ID is 384 or more (a valid tile), then the character's graphic is changed to this new tile. Otherwise, the character's graphic is changed to the appropriate character set file. Next the character is set to be visible, as long as it isn't transparent. A little explanation is in order to understand the if @tile_id == 0 clause. This means that the character is being drawn from a character set file. To begin, all sixteen patterns are shown, but the viewport size is manipulated so that only one pattern at a time shows up. The sx = @character.pattern * @cw statement displaces the viewport to the right within the character set file by an amount equal to the width of one pattern, times the pattern number being shown, with 0 being the first pattern. The sy = (@character.direction - 2) / 2 * @ch statement displaces the viewport downward based on the character's facing (2 = Down, 4 = Left, 6 = Right, 8 = Up). The self.src_rect.set(sx, sy, @cw, @ch) statement then sets up the slice of the character set file you see based on these displacements. The next three statements set the character's X, Y, and Z coordinates on the screen. Then, the character opacity, blending, and obscurement are set based on the current values for that character. Finally, if there's an animation waiting to resolve on this character, then the animation is played and the value of @character.animation_id is reset to 0.