The RPG Maker Resource Kit

RMRK RPG Maker Creation => VX => VX Scripts Database => Topic started by: modern algebra on February 20, 2010, 07:00:09 PM

Title: Show Animated Pictures
Post by: modern algebra on February 20, 2010, 07:00:09 PM
Show Animated Pictures
Version: 1.0
Author: modern algebra
Date: February 20, 2010

Version History



Description


This will allow you to show animated pictures through the regular event commands.

Features


Instructions

Place this script in its own slot in the Script Editor (F11) above Main and below all the other default scripts.
For instructions on how to format your pictures and name them to utilize this script, see the instructions in the header, starting at line 12.

Attached at the bottom of this post is a very simple sample picture (using facesets generated with MOROMAGA), with the naming required. The first uses the default timing, while the second specifies a frame alteration every 1/15 second. Both are the same image and so both have a frame width of 96.

Script


Code: [Select]
#==============================================================================
#    Show Animated Picture
#    Version: 1.0
#    Author: modern algebra (rmrk.net)
#    Date: February 20, 2010
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Description:
#    
#    This will allow you to show animated pictures through the regular event
#   commands.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Instructions:
#
#    Place this script in its own slot in the Script Editor (F11) above Main
#   and below all the other default scripts.
#
#    The format for the animated picture is that each frame of the animation
#   should be placed in the image to the right of the previous frame. Each
#   frame must be equal width. The easiest example is any direction of a
#   character sprite. Once you have created your picture in the correct format,
#   you must identify how you want it to animate in its name. You can name the
#   file whatever you want, but you need to include this code somewhere in the
#   name:
#        %[frame_width, time_interval]
#   where:
#     frame_width : the width of each frame, so this should be an integer of
#       however many pixels wide each frame of the animation is
#     time_interval : this determines how much time each frame is shown for
#       before switching to the next frame. It is also an integer, where
#       60 = 1 second. So, if you want each frame to be shown for only 1/10 of
#       a second, then you should put 6 here. If you do not specify a time
#       interval and leave it as %[frame_width] then it will take the value
#       specified in SAP_DEFAULT_TIME at line 56.
#
#    The animation will go from the first through to the last frame and then
#   repeat until the picture is erased. It will switch between frames at the
#   speed you define by setting time_interval.
#
#    If you do not include this code in the name, then the game will treat it
#   as a normal picture and show the whole thing.
#==============================================================================

#==============================================================================
# ** Sprite_Picture
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Summary of Changes:
#    new constant - SAP_DEFAULT_FRAMES
#    aliased method - update
#    new method - update_src_rect
#==============================================================================

class Sprite_Picture
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * CONSTANTS
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  SAP_DEFAULT_TIME = 12
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Frame Update
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias malbr_anmtepictr_upte_6ys1 update
  def update (*args)
    # Check if picture has changed and, if so, whether it is animated
    if @picture_name != @picture.name
      @sap_animated = @picture.name[/%\[(\d+),?\s*?(\d*?)\]/] != nil
      if @sap_animated
        @sap_frame_width = $1.to_i
        @sap_time_interval = $2.to_i != 0 ? $2.to_i : SAP_DEFAULT_TIME
      end
      @sap_current_frame = -1
      @sap_frame_count = 0
    end
    malbr_anmtepictr_upte_6ys1 (*args) # Run Original Method
    # If picture is animated
    update_src_rect if @sap_animated
  end
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Update Transfer Origin Rectangle
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def update_src_rect
    @sap_frame_count %= @sap_time_interval
    if @sap_frame_count == 0
      @sap_current_frame += 1
      @sap_current_frame = 0 if self.bitmap.width < (@sap_current_frame + 1)*@sap_frame_width
      sx = @sap_current_frame*@sap_frame_width
      self.src_rect.set(sx, 0, @sap_frame_width, self.bitmap.height)
    end
    @sap_frame_count += 1
  end
end

Credit



Support


Please post in this topic in RMRK for support, no matter how long it's been since the topic was posted in last. Do not PM me, as if you have an issue it is likely someone else will, and I want them to benefit from the ensuing discussion also.

Known Compatibility Issues

No currently known compatibility issues. It would likely interfere with other scripts that alter how much of a picture is shown, however. I know of no such scripts, however.


Creative Commons License
This script by modern algebra is licensed under a Creative Commons Attribution-Non-Commercial-Share Alike 2.5 Canada License.
Title: Re: Show Animated Pictures
Post by: joy on February 20, 2010, 07:20:07 PM
Now this looks bloody helpful...great work!!
Title: Re: Show Animated Pictures
Post by: Grafikal on February 20, 2010, 07:40:53 PM
oh snap, this is cool (we could so use this in our game) =o
Title: Re: Show Animated Pictures
Post by: modern algebra on February 21, 2010, 05:57:51 PM
Well, I'm glad you guys like it. I was kind of thinking halfway through making it that it wasn't all that useful, as the effect could easily be replicated by an event like:

Loop
Show Picture 1 [frame 1]
Wait: x frames
Show Picture 1 [frame 2]
Wait: x frames
etc..
End Loop

Though, it does have the benefit of being able to perform other picture operations on each at the same time.

So I'm happy that you guys like it, as otherwise I'd be convinced it was boring and useless :P
Title: Re: Show Animated Pictures
Post by: SuperMega on February 21, 2010, 06:31:12 PM
Woah, this is great!  Thanks for another great script, modern algebra!  I'd hate to ask, but would a demo be possible?  I think it would really help show how it works.
Title: Re: Show Animated Pictures
Post by: tSwitch on February 21, 2010, 07:24:41 PM
that'd be in an event.
and those are the exact event commands you'd need to call, more or less.
Title: Re: Show Animated Pictures
Post by: Nessiah on February 17, 2013, 03:08:54 PM
I find it pretty amazing that this also works in VXAce!
Just wanted to add that there is a bug tho.

At line 70, after this: @sap_frame_count = 0
Add this: @picture_name = @picture.name
And it works, at least in VXAce ;w;)/