Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: Calintz on January 31, 2008, 07:43:06 pm

Title: {RESOLVED} Scripted by RPG...but he didn't explain how to use it...
Post by: Calintz on January 31, 2008, 07:43:06 pm
I couldn't figure out how to do this on my own, but I located this script made by RPG...Problem is, he didn't explain it

Spoiler: ShowHide
Animated Sprite

Author: RPG
Updated: November 07, 2007

Description


This class inherits RPG::Sprite (which in turn inherits Sprite) and allows you to have animated sprites and also sprites that move around the screen. You can probably make Sprite_Battle inherit this to allow battle sprite animation and movement. To use... Uh, I'm too lazy to explain it right now.

#==============================================================================
# * Animated_Sprite                                Scripted by: RPG
#------------------------------------------------------------------------------
#  A class for animated sprites.
#==============================================================================

class Animated_Sprite < RPG::Sprite
  #--------------------------------------------------------------------------
  # - Accessible instance variables.
  #--------------------------------------------------------------------------
  attr_accessor :frames        # Number of animation frames
  attr_accessor :delay         # Delay time between frames (speed)
  attr_accessor :frame_width   # Width of each frame
  attr_accessor :frame_height  # Height of each frame
  attr_accessor :offset_x      # X coordinate of the 1st frame
  attr_accessor :offset_y      # Y coordinate of all frames
  attr_accessor :current_frame # Current animation frame
  attr_accessor :moving        # Is the sprite moving?

  #--------------------------------------------------------------------------
  # - Initialize an animated sprite
  #   bitmap : The sprite's bitmap
  #   frames : Number of animation frames
  #   delay : Frame delay, controls animation speed
  #   startf : Starting frame for animation
  #   fwidth : Width of each frame
  #   fheight : Height of each frame
  #   viewport : Sprite viewport
  #--------------------------------------------------------------------------
  def initialize(bitmap = nil, frames = 0, delay = 0, startf = 0,
                 fwidth = 0, fheight = 0, viewport = nil)
    super(viewport)
    self.bitmap = bitmap
    @frames = frames
    @delay = delay
    @offset_x, @offset_y = 0, 0
    @frame_width, @frame_height = fwidth, fheight
    @current_frame = startf
    self.src_rect = Rect.new(x + startf * fwidth, y, fwidth, fheight)
  end

  #--------------------------------------------------------------------------
  # - Update animation and movement
  #--------------------------------------------------------------------------
  def update
    super
    unless self.bitmap == nil
      x = @current_frame * @frame_width + @offset_x
      self.src_rect = Rect.new(x, @offset_y, @frame_width, @frame_height)
      @current_frame = (@current_frame + 1) % @frames unless @frames == 0
      delay(@delay)
    end
  end
 
  #--------------------------------------------------------------------------
  # - Move the sprite
  #   x : X coordinate of the destination point
  #   y : Y coordinate of the destination point
  #   speed : Speed of movement (0 = delayed, 1+ = faster)
  #   delay : Movement delay if speed is at 0
  #--------------------------------------------------------------------------
  def move(x, y, speed = 1, delay = 0)
    @destx = x
    @desty = y
    @move_speed = speed
    @move_delay = delay
    @move_old = Graphics.frame_count
    @moving = true
  end
 
  #--------------------------------------------------------------------------
  # - Move sprite to destx and desty
  #--------------------------------------------------------------------------
  def update_move
    return unless @moving
    movinc = @move_speed == 0 ? 1 : @move_speed
    if Graphics.frame_count - @move_old > @move_delay or @move_speed != 0
      self.x += movinc if self.x < @destx
      self.x -= movinc if self.x > @destx
      self.y += movinc if self.y < @desty
      self.y -= movinc if self.y > @desty
      @move_old = Graphics.frame_count
    end
    if @move_speed > 1
      self.x += 1 if (@destx - self.x).abs % @move_speed != 0
      self.y += 1 if (@desty - self.y).abs % @move_speed != 0
    end
    if self.x == @destx and self.y == @desty
      @moving = false
    end
  end
 
  #--------------------------------------------------------------------------
  # - Pause animation, but still updates movement
  #   frames : Number of frames
  #--------------------------------------------------------------------------
  def delay(frames)
    old = Graphics.frame_count
    loop do
      Graphics.update
      update_move
      break if (Graphics.frame_count - old >= frames)
    end
  end
end

Title: Re: Scripted by RPG...but he didn't explain how to use it...
Post by: Blizzard on January 31, 2008, 08:20:05 pm
I am actually wondering why he overcomplicated that stuff instead of simply using sprite.animation($data_animations[ID], hit_flag). -_-
Title: Re: Scripted by RPG...but he didn't explain how to use it...
Post by: Calintz on January 31, 2008, 08:21:04 pm
Will this script help me make animated sprites??
Title: Re: Scripted by RPG...but he didn't explain how to use it...
Post by: Blizzard on January 31, 2008, 08:31:10 pm
Better use the command I just gave you instead, it's less complicated and less laggy. hit_flag is true or false, depending on which animation you want to be played exactly (you know, hit or miss), sprite is a Sprite instance and ID is the ID of the animation in the database.
Title: Re: Scripted by RPG...but he didn't explain how to use it...
Post by: Calintz on January 31, 2008, 08:33:08 pm
No no no...I'm not tinkering with a battle system, just trying to add an animated character into my CMS i'm working on...
Title: Re: Scripted by RPG...but he didn't explain how to use it...
Post by: Fantasist on February 02, 2008, 12:57:49 am
Works even for that. Animations can be used anywhere (like on the map, for example). Just make an instance of an RPG::Sprite (@my_sprite = RPG::Sprite.new). Now make an animation with the required graphics in your database. Now, put this code in:
@my_sprite.animation($data_animations[ID], true)
where ID is the animation you just made. If you want to loop the animation, use loop_animation($data_animations[ID], true) instead of just animation.

And don't forget to update the sprite (in the def update, add "@my_sprite.update").
Title: Re: Scripted by RPG...but he didn't explain how to use it...
Post by: Calintz on February 02, 2008, 06:36:55 pm
Yeah, but that would require more work than simply doing what you explained in a different thread..