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.

Windows Move into Scene

This is probably a quick question, I can't really test it because its a new script and because I haven't wrote the 'window' classes for the script yet. Its not really a "how do I?" question, its more of a "I just barely wrote this, will this code work?" question.

This code, I wrote with intentions of the Windows coming 'INTO' the screen all fancy like. Everything happens within 10 frames based on origional Window Height/Width and Opacity (hence the * and / 10 methods.) Also, by default, the windows aren't assigned X/Y offsets, because that would interfer with the code, so that is defined within the scene too.

I'm just curious (when I finally write the windows) is this the correct way to go about moving windows? I'm still new with Scenes so bare with me here.

Code:
class Scene_Bank
  #------------------------
  # * Initialization Method
  #------------------------
  def initialize
    # Create Window_Bank
    @window_bank = Window_Bank.new
    @window_bank.ox = (self.x * -1)
    @window_bank.oy = (self.y * -1)
    @window_bank.opacity = 160
    # Create Window_BankSelect
    @command_window = Window_BankSelect.new
    @command_window.ox = 0
    @command_window.oy = 0
    @window_bank.opacity = 160
    # Create Window_Help
    @window_help = Window_Help.new
    @window_help.ox = (self.x * -1)
    @window_help.oy = (self.y * -1)
    @window_help.opacity = 160
  end
  #----------------
  # * Update Method
  #----------------
  def update
    # Animate Positioning Window_Bank (Window Comes in from bottom-right corner.)
    if @window_bank.ox < @window_bank.x or @window_bank.oy < @window_bank.y
      @window_bank.ox -= (@window_bank.x * 10)
      @window_bank.oy -= (@window_bank.y * 10)
      @window_bank.opacity += (@window_bank.opacity / 10)
    end
    # Animate Positioning Window_BankSelect (Window comes in from left side)
    if @command_window.ox < @command_window.x
      @command_window.ox += (@command_window.x * 10)
      @command_window.opacity += (@command_window.opacity / 10)
    end
    # Animate Positioning Window_Help (Window comes in from top)
    if @window_help.oy < @window_help.y
      @window_help.oy += (@window_help.y / 10)
      @window_help.opacity += (@window_help.opacity / 10)
    end
 

khmp

Sponsor

If it works than I would say you are doing it correctly. ox and oy alter the contents though not the actual window location. I guess if you're clever you could use (ox, oy) to make a marquee effect but they won't affect sliding. I would automate this behavior from within the actual window though. I wouldn't depend so much on the scene to dictate where it should be at a given moment rather than it should tell the window you are here and you will moving to there.

@window_test = Window_Test.new
@window_test.x = x
@window_test.y = y
@window_test.slide_to(to_x, to_y, frames)

@window_test.update # In update we will decide that if we are not at the location we need to be to keep going.

I think that's the kind of behavior you should shoot for. It'll make your scene a lot cleaner.

Code:
#==============================================================================
# ** Window_Base
#------------------------------------------------------------------------------
#  This class is for all in-game windows.
#==============================================================================

class Window_Base < Window
  #--------------------------------------------------------------------------
  # * Alias Methods
  #--------------------------------------------------------------------------
  alias_method :windowslide_window_base_initialize, :initialize
  alias_method :windowslide_window_base_update, :update
  #--------------------------------------------------------------------------
  # *Constant Variables
  #--------------------------------------------------------------------------
  WLH = 32
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     x      : window x-coordinate
  #     y      : window y-coordinate
  #     width  : window width
  #     height : window height
  #--------------------------------------------------------------------------
  def initialize(x, y, width, height)
    # Call the old initialize code.
    windowslide_window_base_initialize(x, y, width, height)
    #------------------------------------------------------------------------
    # @sliding
    #   - the boolean that tells update to slide.
    #------------------------------------------------------------------------
    @sliding = false
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Call the old update code.
    windowslide_window_base_update
    # If the user has called slide_to at least once. Let it slide.
    if @sliding
      # If the window is active continue moving it to the slide location.
      if self.active && (((self.x - @tx).abs > @xtol) || 
                        ((self.y - @ty).abs > @ytol))
        # Increment position by frame based velocity.
        self.x += @xvel
        self.y += @yvel
        self.opacity += @delta_opacity
      # If the window isn't active continue moving it to the original location.
      elsif !self.active && (((self.x - @ox).abs > @xtol) || 
                            ((self.y - @oy).abs > @ytol))
        # Decrement position by frame based velocity.
        self.x -= @xvel
        self.y -= @yvel
        self.opacity -= @delta_opacity
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Slide To Location - When Active
  #     x             : window x-coordinate destination.
  #     y             : window y-coordinate destination.
  #     frames        : amount of frames it takes to reach destination.
  #     final_opacity : the final opacity when window gets to destination.
  #--------------------------------------------------------------------------
  def slide_to(x, y, frames, final_opacity = 255)
    #------------------------------------------------------------------------
    # @tx
    #   - the location on the x axis where you want the window to end up.
    # @ty
    #   - the location on the y axis where you want the window to end up.
    # @ox
    #   - the location on the x axis where the window starts.
    # @oy
    #   - the location on the y axis where the window starts.
    # @delta_opacity
    #   - the change in opacity from start to finish. (*based on frames)
    # @xvel
    #   - the x speed it uses to reach start to finish. (*based on frames)
    # @yvel
    #   - the y speed it uses to reach start to finish. (*based on frames)
    # @xtol
    #   - the tolerance of x units from start to finish. (*based on frames)
    # @ytol
    #   - the tolerance of y units from start to finish. (*based on frames)
    #------------------------------------------------------------------------
    # Just enable the boolean so the update doesn't skip sliding.
    @sliding = true
    # Save the values.
    @tx = x
    @ty = y
    @ox = self.x
    @oy = self.y
    @delta_opacity = (final_opacity - self.opacity) / frames
    # Figure out the velocity vector to slide location from original position.
    @xvel = (@tx - @ox) / frames
    @yvel = (@ty - @oy) / frames
    # The tolerance is meant to determine how much space is left over if the
    # number does not divide evenly.
    @xtol = (@tx - @ox) % frames
    @ytol = (@ty - @oy) % frames
  end
end

Good luck with it Kain Nobel! :thumb:
 
I've gotten alot more done than I expected with this one, I might even finish it today! (Meaning it'd be my first full window and scene script.)

Here's my progress so far, only if you want to look it over, theres obviously a couple things I still need to define in each class, and a Window class I haven't made yet, amongst other things. Thanks to you, Arbiter and whoever else has thrown in their two cents to help me learn, I wouldn't have gotten this far without you guys (and gals?)!

(And no, this isn't finished.)

If my sliding windows method doesn't work, I'll try out that *cheater* script ;)

Code:
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# ** Banking Script
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Written by: Kain Nobel
# Version: 0.0
# Date Updated: 5/9/2008
# Date Created: 5/9/2008
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
################################################################################
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# * Game_Bank
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
class Game_Bank
  #[Attributes]- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  attr_accessor :gold               # How much gold you have on hand
  attr_accessor :checking           # Checking account balance
  attr_accessor :savings            # Savings account balance
  attr_accessor :credit_score       # Credit SCORE
  #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  def initialize
    @gold = $game_party.gold
    @checking = checking
    @savings = savings
    @credit_score = credit_score
  end
  #------------------
  # * Define Accounts
  #------------------
  def account
    case @accounts
    when 'Gold'
      @balance = $game_party.gold
    when 'Checking'
      @balance = checking
    when 'Savings'
      @balance = savings
    end
    return @balance
  end
  #------------------
  # * Define Balances
  #------------------
  def balance
    gold = @accounts('Gold')
    checking = @accounts('Checking')
    savings = @accounts('Saving')
  end
  #----------------
  # * Gain Checking
  #----------------
  def gain_checking(n)
    @checking = [[@checking + n, 0].max, 9999999].min
  end
  #----------------
  # * Lose Checking
  #----------------
  def lose_checking(n)
    checking(-n)
  end
  #---------------
  # * Gain Savings
  #---------------
  def gain_savings(n)
    @savings = [[@savings + n, 0].max, 9999999].min
  end
  #---------------
  # * Lose Savings
  #---------------
  def lose_savings(n)
    savings(-n)
  end
end
################################################################################
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# * Window_Bank
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
class Window_Bank < Window_Base
  #------------------------
  # * Initialization Method
  #------------------------
  def initialize
    # These variables control Window Offset & Size (see super)
    ox = 0
    oy = 0
    x = 260
    y = 128
    # Bank Window Dispose method is false
    bank_window_dispose = false
    # Create Window Offset & Size
    super(ox, oy, x, y)
    self.contents = Bitmap.new(width - 32, height - 32)
    # Initialize Banking Variables
    @mode = 0
    @gold = 0
    @checking = 0
    @savings = 0
    @credit_score = 0
    # Call Refresh method
    self.opacity = 160
    update
  end
  #-----------------
  # * Refresh Method
  #-----------------
  def update
    # Clear page for new data
    self.contents.clear
    # Define Banking variables
    @gold = $game_party.gold
    @checking = $game_variables[CHECKING]
    @savings = $game_variables[SAVINGS]
    @credit_score = $game_variables[CREDIT]
    # Set System Color
    self.contents.font.color = system_color
    # Draw Account names
    self.contents.draw_text(0, 0, 96, 36, "Gold :")
    self.contents.draw_text(0, 0, 96, 72, "Checking :")
    self.contents.draw_text(0, 0, 96, 106, "Savings :")
    self.contents.draw_text(0, 0, 192, 140, "CreditSCORE :")
    # Set Normal Color
    self.contents.font.color = normal_color
    # Draw Account variables
    self.contents.draw_text(128, 0, 96, 36, "$" + @gold.to_s, 2)
    self.contents.draw_text(128, 0, 96, 72, "$" + @checking.to_s, 2)
    self.contents.draw_text(128, 0, 96, 106, "$" + @savings.to_s, 2)
    self.contents.draw_text(128, 0, 96, 140, "$" + @credit_score.to_s, 2)
    # Dispose of Window with B button
  end
end
################################################################################
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# * Window_BankCommand
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
class Window_BankCommand < Window_Selectable
  #------------------------
  # * Initialization Method
  #------------------------
  def initialize
    # These variables control Window Offset & Size (see super)
    ox = 0
    oy = 0
    x = 128
    y = 128
    super(ox, oy, x, y)
    self.contents = Bitmap.new(width - 32, height - 32)
    @column_max = 1
    @item_max = 3
    # Write Commands
    b1 = "Checking Account"
    b2 = "Savings Account"
    b3 = "Credit & Loans"
    b4 = "Promotional"
    @commands = [b1, b2, b3, b4]
    refresh
    self.index = 0
  end
  #-----------------
  # * Refresh Method
  #-----------------
  def refresh
    self.contents.clear
    for i in 0...@item_max
      draw_item(i)
    end
  end
  #-------------------------
  # * Draw Item
  #     index : item number
  #-------------------------
  def draw_item(index)
    y = 5 + index * 160
    self.contents.draw_text(x, 0, 128, 32, @commands[index])
  end
end
################################################################################
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# * Scene_Bank
#= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
class Scene_Bank
  #------------------------
  # * Initialization Method
  #------------------------
  def initialize
    # Create Window_Bank
    @window_bank = Window_Bank.new
    @window_bank.ox = (self.x * -1)
    @window_bank.oy = (self.y * -1)
    @window_bank.opacity = 160
    # Create Window_BankCommand
    @command_window = Window_BankCommand.new
    @command_window.ox = 0
    @command_window.oy = 0
    @window_bank.opacity = 160
    # Create Window_Help
    @window_help = Window_Help.new
    @window_help.ox = (self.x * -1)
    @window_help.oy = (self.y * -1)
    @window_help.opacity = 160
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @command_window.dispose
    @window_bank.dispose
    @window_help.dispose
  end
  #----------------
  # * Update Method
  #----------------
  def update
    #[Window Animation]---------------------------------------------------------
    # Animate Positioning Window_Bank (Window Comes in from bottom-right corner.)
    if @window_bank.ox < @window_bank.x or @window_bank.oy < @window_bank.y
      @window_bank.ox -= (@window_bank.x * 10)
      @window_bank.oy -= (@window_bank.y * 10)
      @window_bank.opacity += (@window_bank.opacity / 10)
    end
    # Animate Positioning Window_BankCommand (Window comes in from left side)
    if @command_window.ox < @command_window.x
      @command_window.ox += (@command_window.x * 10)
      @command_window.opacity += (@command_window.opacity / 10)
    end
    # Animate Positioning Window_Help (Window comes in from top)
    if @window_help.oy < @window_help.y
      @window_help.oy += (@window_help.y / 10)
      @window_help.opacity += (@window_help.opacity / 10)
    end
    #[Account Access]-----------------------------------------------------------
    # Disable ALL Account Access
    if $game_bank.all_disabled
      @command_window.disable_item(0) # Checking Account
      @command_window.disable_item(1) # Savings Account
      @command_window.disable_item(2) # Credit and Loans
      @command_window.disable_item(3) # Promotional Deals
    else
      # Disable Checking Account
      if $game_bank.checking_disabled
        @command_window.disable_item(0)
      end
      # Disable Savings Account
      if $game_bank.savings_disabled
        @command_window.disable_item(1)
      end
      # Disable Credit and Loans
      if $game_bank.loans_disabled
        @command_window.disable_item(2)
      end
      # Disable Promotions and Deals
      if $game_bank.deals_disabled
        @command_window.disable_item(3)
      end
      #[Input::C]---------------------------------------------------------------
      if Input.press?(Input::C) # Selection Button
        case @command_window.index
        # Checking Account
        when 0
          checking_account
          return
        end
        # Savings Account
        when 1
          savings_account
          return
        end
        # Credit and Loans
        when 2
          credit_and_loans
          return
        end
        # Promotional Deals"
        when 3
          promotional
          return
        else
          # Cycle Cursor
          if @command_window.index > (@command_window.item_max - 1)
            @command_window.index = 0
          end
        end
      end
      #[Input::B]---------------------------------------------------------------
      if Input.press?(Input::B) # Cancel button
        if @command_window.active == false
          @command_window.active = true
          @transaction_window.active = false
        else
          $scene = Scene_Map.new
        end
      end
    end
  end
  #--------------------------
  # * Define Checking Account
  #--------------------------
  def help_update
    # Window_BankCommand
    if @command_window.active = true
      @window_transaction.active = false
      @window_transaction.visible = false
      for i in 0..@command_window.index
        case @command_window.index
        when 0
          @window_help.set_text("Access your Checking Account.")
        when 1
          @window_help.set_text("Access your Savings Account.")
        when 2
          @window_help.set_text("Do you need Cash? Do you need it now?")
        when 3
          @window_help.set_text("Find the latest and greatest Promotional Offers!")
        end
      end
    end
    # Window_Transaction
    if @command_window.active == false && @window_transaction.active == true
      @window_transaction.visible = true
      for i in 0..@transaction_window.index
        case @window_transaction.index
        when 0
          @window_help.set_text("Deposit Money into Checking.")
        when 1
          @window_help.set_text("Withdraw Money from Checking.")
        when 2
          @window_help.set_text("Transfer Money from Savings.")
        end
      end
    end
  end
end
 

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