Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Cursor Sprite + Smooth Selection Movement

Cursor Sprite + Smooth Selection Movement
Originally by strcat; topic found here: http://www.rpgrevolution.com/forums/ind ... opic=12741
RMXP
Description:
Either a RGSS2 to RGSS translation, or a rewrite of the script...
The features:
1.Create a cursor sprite for Window_Selectable class.

2.Enable smooth movement of cursor, and menu selection. In other word, rather than just move down, the highlight on the item slides down.

Original Code:
Code:
==============================================================================

# Cursor Sprite + Smooth Movement Script

#==============================================================================

# Author  : strcat

# Version : 1.2

# Date    : 29 / 01 / 2008 

#------------------------------------------------------------------------------ 

# This script will create a new cursor sprite,and add a smooth movement to 

# the cursor,thus making your game look better 

#------------------------------------------------------------------------------

#==============================================================================

# ** STRRGSS2

#==============================================================================

module STRRGSS2

  # Cursor graphic file name

  WCURSOR_FN = "Window_cursor"

  # position of the cursor(x, y)

  WCURSOR_X = 0

  WCURSOR_Y = 0

end

class Sprite_WindowCursor < Sprite

  WAIT = 8    # numbers of frame for cursor update delay.

  CW   = 24   # cursor width

  CH   = 24   # cursor height

end

#==============================================================================

# ** Window_Selectable

#==============================================================================

class Window_Selectable < Window_Base

  #--------------------------------------------------------------------------

  #  * Update Cursor

  #--------------------------------------------------------------------------

  def update_se_cursor

    cr = self.cursor_rect

    r = Rect.new(0, 0, 0, 0)

    y = (self.cursor_rect.height - 24) / 2

    v = nil

    v = self.viewport if self.viewport != nil

    @strcursor.viewport = v

    @strcursor.x = self.x + 16 + cr.x - 24 + STRRGSS2::WCURSOR_X

    @strcursor.y = self.y + 16 + cr.y + y  + STRRGSS2::WCURSOR_Y 

    @strcursor.z = self.z + 16

    @strcursor.opacity += 64

    @strcursor.opacity = self.opacity if @strcursor.opacity > self.opacity 

    @strcursor.visible = ((cr != r) and self.openness == 255)

    @strcursor.visible = false unless self.visible

    @strcursor.blink = (not self.active)

    @strcursor.update

  end

  def update_cursor_str(r,rect)

    self.cursor_rect = r

    rect = Rect.new(0, 0, 0, 0) if @index < 0

    mx = self.cursor_rect.x - rect.x

    if mx >= -1 and mx <= 1

      self.cursor_rect.x = rect.x

    elsif mx != 0

      x = self.cursor_rect.x - (mx/2.0)

      self.cursor_rect.x = x

    end

    my = self.cursor_rect.y - rect.y

    if my >= -1 and my <= 1

      self.cursor_rect.y = rect.y

    elsif my != 0

      y = self.cursor_rect.y - (my/2.0)

      self.cursor_rect.y = y

    end

    self.cursor_rect.width = rect.width

    self.cursor_rect.height = rect.height

  end

  def visible=(v)

    super

    @strcursor.visible = v

  end

  def active=(v)

    super

    @strcursor.visible = false

  end

  def dispose

    @strcursor.dispose

    super

  end

  #--------------------------------------------------------------------------

  # * Object Initialization

  #--------------------------------------------------------------------------

  alias initialize_str05m initialize

  def initialize(x, y, width, height, spacing = 32)

    initialize_str05m(x, y, width, height, spacing)

    file = STRRGSS2::WCURSOR_FN

    v = nil

    v = self.viewport if self.viewport != nil

    @strcursor = Sprite_WindowCursor.new(file, v)

    @f_set = false

  end

  alias update_str05m update

  def update

    update_se_cursor unless @opening

    update_str05m

  end

  alias update_cursor_str05m update_cursor

  def update_cursor

    r = self.cursor_rect.clone unless @f_set

    update_cursor_str05m

    update_cursor_str(r, self.cursor_rect.clone) unless @f_set

    @f_set = false

  end

  #--------------------------------------------------------------------------

  # * Get Index

  #--------------------------------------------------------------------------

  def index=(index)

    @f_set = true

    @index = index

    update_cursor

    call_update_help

  end

end

#==============================================================================

# ** Sprite_WindowCursor

#==============================================================================

class Sprite_WindowCursor < Sprite

  #--------------------------------------------------------------------------

  # * Public Instance Variables

  #--------------------------------------------------------------------------

  attr_accessor :blink

  #--------------------------------------------------------------------------

  # * Object Initialization

  #--------------------------------------------------------------------------

  def initialize(name,viewport)

    super(viewport)

    self.bitmap = Cache.system(name)

    self.src_rect.set(0, 0, CW, CH)

    self.opacity = 0

    @xx = -8

    @wait = WAIT

    @count = 0

    @blink = false

  end

  #--------------------------------------------------------------------------

  # * Dispose

  #--------------------------------------------------------------------------

  def dispose

    super

  end

  #--------------------------------------------------------------------------

  # * Frame Update

  #--------------------------------------------------------------------------

  def update

    super

    @xx += 1 if @xx != 0

    self.x += @xx

    @count += 1

    self.visible = false if @blink and (@count % 4) == 0

    @wait -= 1

    return if @wait > 0

    @wait = WAIT

    self.src_rect.x += CW

    self.src_rect.x = 0 if (self.bitmap.width <= self.src_rect.x)

  end

end
Screen shots:
None.


Help is highly appreciated,
-abreaction
 

MicKo

Member

[ruby]#==============================================================================
# Cursor Sprite + Smooth Movement Script
#==============================================================================
# Author  : strcat
# Version : 1.2
# Date    : 29 / 01 / 2008
#------------------------------------------------------------------------------
# This script will create a new cursor sprite,and add a smooth movement to
# the cursor,thus making your game look better
#------------------------------------------------------------------------------
#==============================================================================
# ** STRRGSS2
#==============================================================================
module STRRGSS2
  # Cursor graphic file name
  WCURSOR_FN = "Window_cursor"
  # position of the cursor(x, y)
  WCURSOR_X = 0
  WCURSOR_Y = 0
end
class Sprite_WindowCursor < Sprite
  WAIT = 8    # numbers of frame for cursor update delay.
  CW   = 24   # cursor width
  CH   = 24   # cursor height
end
#==============================================================================
# ** Window_Selectable
#==============================================================================
class Window_Selectable < Window_Base
  #--------------------------------------------------------------------------
  #  * Update Cursor
  #--------------------------------------------------------------------------
  def update_se_cursor
    cr = self.cursor_rect
    r = Rect.new(0, 0, 0, 0)
    y = (self.cursor_rect.height - 24) / 2
    v = nil
    v = self.viewport if self.viewport != nil
    @strcursor.x = self.x + 16 + cr.x - 24 + STRRGSS2::WCURSOR_X
    @strcursor.y = self.y + 16 + cr.y + y  + STRRGSS2::WCURSOR_Y
    @strcursor.z = self.z + 16
    @strcursor.opacity += 64
    @strcursor.opacity = self.opacity if @strcursor.opacity > self.opacity
    @strcursor.visible = ((cr != r) and self.opacity == 255)
    @strcursor.visible = false unless self.visible
    @strcursor.blink = (not self.active)
    @strcursor.update
  end
  def update_cursor_str(r,rect)
    self.cursor_rect = r
    rect = Rect.new(0, 0, 0, 0) if @index < 0
    mx = self.cursor_rect.x - rect.x
    if mx >= -1 and mx <= 1
      self.cursor_rect.x = rect.x
    elsif mx != 0
      x = self.cursor_rect.x - (mx/2.0)
      self.cursor_rect.x = x
    end
    my = self.cursor_rect.y - rect.y
    if my >= -1 and my <= 1
      self.cursor_rect.y = rect.y
    elsif my != 0
      y = self.cursor_rect.y - (my/2.0)
      self.cursor_rect.y = y
    end
    self.cursor_rect.width = rect.width
    self.cursor_rect.height = rect.height
  end
  def visible=(v)
    super
    @strcursor.visible = v
  end
  def active=(v)
    super
    @strcursor.visible = false
  end
  def dispose
    @strcursor.dispose
    super
  end
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias initialize_str05m initialize
  def initialize(x, y, width, height)
    initialize_str05m(x, y, width, height)
    file = STRRGSS2::WCURSOR_FN
    v = nil
    v = self.viewport if self.viewport != nil
    @strcursor = Sprite_WindowCursor.new(file, v)
    @f_set = false
  end
  alias update_str05m update
  def update
    update_se_cursor unless @opening
    update_str05m
  end
  alias update_cursor_str05m update_cursor_rect
  def update_cursor_rect
    r = self.cursor_rect.clone unless @f_set
    update_cursor_str05m
    update_cursor_str(r, self.cursor_rect.clone) unless @f_set
    @f_set = false
  end
  #--------------------------------------------------------------------------
  # * Get Index
  #--------------------------------------------------------------------------
  def index=(index)
    @f_set = true
    @index = index
    update_cursor_rect
    #update_help
  end
end
#==============================================================================
# ** Sprite_WindowCursor
#==============================================================================
class Sprite_WindowCursor < Sprite
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :blink
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize(name,viewport)
    super(viewport)
    self.bitmap = RPG::Cache.picture(name)
    self.src_rect.set(0, 0, CW, CH)
    self.opacity = 0
    @xx = -8
    @wait = WAIT
    @count = 0
    @blink = false
  end
  #--------------------------------------------------------------------------
  # * Dispose
  #--------------------------------------------------------------------------
  def dispose
    super
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    super
    @xx += 1 if @xx != 0
    self.x += @xx
    @count += 1
    self.visible = false if @blink and (@count % 4) == 0
    @wait -= 1
    return if @wait > 0
    @wait = WAIT
    self.src_rect.x += CW
    self.src_rect.x = 0 if (self.bitmap.width <= self.src_rect.x)
  end
end
[/ruby]
Works for me, but i didn't try it on VX so tell me if there's something wrong with it. (if it works, don't bother to credit me or anything, since i just changed a few lines)
 
Works for me, but i didn't try it on VX so tell me if there's something wrong with it.
I'm not trying to get it to work in VX, I need it in XP.
But I'll try it...

Finished trying it, and the highlight's smooth movement works great, thanks; same with the cursor.

Thanks for your help!
-abreaction
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top