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.

[Resolved] Little Help - Image Sorting

Status
Not open for further replies.

Mac

Member

I'm having a few troubles with my images for my shop buy menu, the image appears up the call of the shop, but i onlt want it to appear when i select the buy option. Does anybody know how to sort this little problem.

Code:
#==============================================================================
# ** Window_Tactics_ShopBuy
#------------------------------------------------------------------------------
#  This window displays buyable goods on the shop screen.
#==============================================================================

class Window_Tactics_ShopBuy < Window_Selectable
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     shop_goods : goods
  #--------------------------------------------------------------------------
  def initialize(shop_goods)
    super(20, 20, 600, 228)
    self.contents = Bitmap.new(width - 32, row_max * 32)
    @SHOPBACK_graphic = Sprite.new
    @SHOPBACK_graphic.x = self.x + 1
    @SHOPBACK_graphic.y = self.y + 1
    @SHOPBACK_graphic.z = self.z + 1
    self.opacity = 0
    @shop_goods = shop_goods
    refresh
    self.index = 0
  end
  #--------------------------------------------------------------------------
  # * Item Acquisition
  #--------------------------------------------------------------------------
  def item
    return @data[self.index]
  end
  #--------------------------------------------------------------------------
  # * Refresh
  #--------------------------------------------------------------------------
  def refresh
    if self.contents != nil
      self.contents.dispose
      self.contents = nil
    end
    @data = []
    for goods_item in @shop_goods
      case goods_item[0]
      when 0
        item = $data_items[goods_item[1]]
      when 1
        item = $data_weapons[goods_item[1]]
      when 2
        item = $data_armors[goods_item[1]]
      end
      if item != nil
        @data.push(item)
      end
    end
    # If item count is not 0, make a bit map and draw all items
    @item_max = @data.size
    if @item_max > 0
    self.contents = Bitmap.new(width - 32, row_max * 32)
    for i in 0...@item_max
      draw_item(i)
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Draw Item
  #     index : item number
  #--------------------------------------------------------------------------
def draw_item(index)
    item = @data[index]
    # Get items in possession
    case item
    when RPG::Item
      number = $game_party.item_number(item.id)
      equipped_number = 0
    when RPG::Weapon
      number = $game_party.weapon_number(item.id)
      equipped_number = $game_party.equipped_weapon_number(item.id)
    when RPG::Armor
      number = $game_party.armor_number(item.id)
      equipped_number = $game_party.equipped_armor_number(item.id)
    end
    # If price is less than money in possession, and amount in possession is
    # not 99, then set to normal text color. Otherwise set to disabled color
    if item.price <= $game_party.gold and number < 99
      self.contents.font.color = normal_color
    else
      self.contents.font.color = disabled_color
    end
    x = 4
    y = index * 32
    rect = Rect.new(x, y, self.width - 32, 32)
    bitmap = RPG::Cache.icon(item.icon_name)
    opacity = self.contents.font.color == normal_color ? 255 : 128
    @SHOPBACK_graphic.bitmap = RPG::Cache.picture('Shop Window')
    @SHOPBACK_graphic.visible = true
    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24), opacity)
    self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)
    self.contents.draw_text(x + 370, y, 88, 32, (number + equipped_number).to_s, 2)
    self.contents.draw_text(x + 290, y, 88, 32, equipped_number.to_s, 2)
    self.contents.draw_text(x + 460, y, 88, 32, item.price.to_s, 2)
  end
  def dispose
        @SHOPBACK_graphic.dispose
        end
  #--------------------------------------------------------------------------
  # * Help Text Update
  #--------------------------------------------------------------------------
  def update_help
    @help_window.set_text(self.item == nil ? "" : self.item.description)
  end
end
 
Ok. I re-coded quite a bit of the code.

Code:
#==============================================================================
# ** Game_Temp
#==============================================================================

class Game_Temp
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :call_fftshopsystem
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias_method :mac_fftshop_init, :initialize
  def initialize
    # Original Initialization
    mac_fftshop_init
    # Set Call FFT Shop False
    @call_fftshopsystem = false
  end
end

#==============================================================================
# ** Game_Party
#==============================================================================

class Game_Party
  #--------------------------------------------------------------------------
  # * Total Weapon Number
  #--------------------------------------------------------------------------
  def total_weapon_number(weapon_id)
    n = weapon_number(weapon_id)
    n += equipped_weapon_number(weapon_id)
    return n
  end
  #--------------------------------------------------------------------------
  # * Equipped Weapon Number
  #--------------------------------------------------------------------------
  def equipped_weapon_number(weapon_id)
    n = 0
    @actors.each do |actor|
      n += 1 if actor.weapon_id == weapon_id
    end
    return n
  end
  #--------------------------------------------------------------------------
  # * Total Armor Number
  #--------------------------------------------------------------------------
  def total_armor_number(armor_id)
    n = armor_number(armor_id)
    n += equipped_armor_number(armor_id)
    return n
  end
  #--------------------------------------------------------------------------
  # * Equipped Armor Number
  #--------------------------------------------------------------------------
  def equipped_armor_number(armor_id)
    n = 0
    @actors.each do |actor|
      n += 1 if actor.armor1_id == armor_id
      n += 1 if actor.armor2_id == armor_id
      n += 1 if actor.armor3_id == armor_id
      n += 1 if actor.armor4_id == armor_id
    end
    return n
  end
end

#==============================================================================
# ** Scene_Shop
#==============================================================================

class Scene_Shop
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  alias_method :mac_fftshop_main, :main
  def main
    # If Call FFT Shop
    if $game_temp.call_fftshopsystem
      # Change to FFT Shop
      $scene = FFT_ShopSystem::Scene_Shop.new
      # Turn call FFT Shop off
      $game_temp.call_fftshopsystem = false
      # End Method
      return
    end
    # Original Method
    mac_fftshop_main
  end
end

#==============================================================================
# ** FFT_ShopSystem
#==============================================================================

module FFT_ShopSystem

  #============================================================================
  # ** Window_ShopCommand
  #============================================================================

  class Window_ShopCommand < ::Window_Command
    #--------------------------------------------------------------------------
    # * Object Initialization
    #--------------------------------------------------------------------------
    def initialize
      super(180, ['Buy', 'Sell', 'Exit'])
      # Reassign Window Parameters
      self.x, self.y, self.opacity = 16, 336, 0
      # Creates Background Graphic
      @command_graphic = Sprite.new
      @command_graphic.bitmap = RPG::Cache.picture('Shop Command')
      @command_graphic.x = self.x + 1
      @command_graphic.y = self.y + 7
      @command_graphic.z = self.z - 1
      @command_graphic.visible = true
    end
    #--------------------------------------------------------------------------
    # * Frame Update
    #--------------------------------------------------------------------------
    def update
      super
      @command_graphic.visible = self.visible
    end
    #--------------------------------------------------------------------------
    # * Dispose
    #--------------------------------------------------------------------------
    def dispose
      super
      @command_graphic.dispose
    end
  end
  
  #============================================================================
  # ** Window_ShopBuy
  #============================================================================

  class Window_ShopBuy < ::Window_ShopBuy
    #--------------------------------------------------------------------------
    # * Object Initialization
    #--------------------------------------------------------------------------
    def initialize(shop_goods)
      super(shop_goods)
      # Reassign Window Parameters
      self.x, self.y, self.width, self.height = 16, 20, 600, 228
      self.opacity, self.visible, self.active = 0, false, false
      # Creates Showback Graphic
      @showback_graphic = Sprite.new
      @showback_graphic.bitmap = RPG::Cache.picture('Shop Window')
      @showback_graphic.x = self.x + 1
      @showback_graphic.y = self.y + 1
      @showback_graphic.z = self.z - 1
      @showback_graphic.visible = self.visible
    end
    #--------------------------------------------------------------------------
    # * Draw Item
    #--------------------------------------------------------------------------
    def draw_item(index)
      # Gets item Number
      item = @data[index]
      # Gets Number Owned & Equipped
      case item
      when RPG::Item
        number = $game_party.item_number(item.id)
        equipped_number = 0
      when RPG::Weapon
        number = $game_party.weapon_number(item.id)
        equipped_number = $game_party.equipped_weapon_number(item.id)
      when RPG::Armor
        number = $game_party.armor_number(item.id)
        equipped_number = $game_party.equipped_armor_number(item.id)
      end
      # Adjust Color By Price
      if item.price <= $game_party.gold and number < 99
        self.contents.font.color = normal_color
      else
        self.contents.font.color = disabled_color
      end
      # Clears Area
      rect = Rect.new(0, index * 32, contents.width, 32)
      self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
      # Draws Icon
      bitmap = RPG::Cache.icon(item.icon_name)
      opacity = self.contents.font.color == normal_color ? 255 : 128
      self.contents.blt(4, index * 32 + 4, bitmap, Rect.new(0, 0, 24, 24), 
        opacity)
      # Draws Item Name, Total, Equipped and Price
      self.contents.draw_text(32, index * 32, 212, 32, item.name, 0)
      self.contents.draw_text(374, index * 32, 88, 32, 
        (number + equipped_number).to_s, 2)
      self.contents.draw_text(294, index * 32, 88, 32, equipped_number.to_s, 2)
      self.contents.draw_text(464, index * 32, 88, 32, item.price.to_s, 2)
    end
    #--------------------------------------------------------------------------
    # * Update
    #--------------------------------------------------------------------------
    def update
      super
      @showback_graphic.visible = self.visible
    end
    #--------------------------------------------------------------------------
    # * Dispose
    #--------------------------------------------------------------------------
    def dispose
      super
      @showback_graphic.dispose
    end
  end
  
  #============================================================================
  # ** Window_ShopSell
  #============================================================================

  class Window_ShopSell < ::Window_ShopSell
    #--------------------------------------------------------------------------
    # * Object Initialization
    #--------------------------------------------------------------------------
    def initialize
      super
      # Reassign Window Parameters
      self.x, self.y, self.width, self.height = 16, 20, 600, 228
      self.opacity, self.visible, self.active = 0, false, false
      # Creates Showback Graphic
      @showback_graphic = Sprite.new
      @showback_graphic.bitmap = RPG::Cache.picture('Shop Window')
      @showback_graphic.x = self.x + 1
      @showback_graphic.y = self.y + 1
      @showback_graphic.z = self.z - 1
      @showback_graphic.visible = false
      # Adjust Column Max
      @column_max = 1
    end
    #--------------------------------------------------------------------------
    # * Draw Item
    #--------------------------------------------------------------------------
    def draw_item(index)
      # Gets Item Data
      item = @data[index]
      # Gets Number & Equipped Number
      case item
      when RPG::Item
        number = $game_party.item_number(item.id)
        equipped_number = 0
      when RPG::Weapon
        number = $game_party.weapon_number(item.id)
        equipped_number = $game_party.equipped_weapon_number(item.id)
      when RPG::Armor
        number = $game_party.armor_number(item.id)
        equipped_number = $game_party.equipped_armor_number(item.id)
      end
      # Adjust Font Color By Price
      if item.price > 0
        self.contents.font.color = normal_color
      else
        self.contents.font.color = disabled_color
      end
      # Clears Area
      rect = Rect.new(0, index * 32, contents.width, 32)
      self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
      # Draws Icon
      bitmap = RPG::Cache.icon(item.icon_name)
      opacity = self.contents.font.color == normal_color ? 255 : 128
      self.contents.blt(4, index * 32 + 4, bitmap, Rect.new(0, 0, 24, 24), 
        opacity)
      # Draws Item Name, Total Owned, Equipped and Price
      self.contents.draw_text(32, index * 32, 212, 32, item.name, 0)
      self.contents.draw_text(374, index * 32, 88, 32, 
        (number + equipped_number).to_s, 2)
      self.contents.draw_text(294, index * 32, 88, 32, equipped_number.to_s, 2)
      self.contents.draw_text(464, index * 32, 88, 32, 
        (item.price / 2).to_s, 2)
    end
    #--------------------------------------------------------------------------
    # * Update
    #--------------------------------------------------------------------------
    def update
      super
      @showback_graphic.visible = self.visible
    end
    #--------------------------------------------------------------------------
    # * Dispose
    #--------------------------------------------------------------------------
    def dispose
      super
      @showback_graphic.dispose
    end
  end
  
  #============================================================================
  # ** Window_ShopSell
  #============================================================================

  class Window_ShopNumber < ::Window_ShopNumber
    #--------------------------------------------------------------------------
    # * Object Initialization
    #--------------------------------------------------------------------------
    def initialize
      super
      # Reassign Window Parameters
      self.x, self.y, self.width, self.height = 100, 260, 440, 64
      # Recreates Window Contents
      self.contents = Bitmap.new(width - 32, height - 32)
      self.visible, self.active = false, false
    end
    #--------------------------------------------------------------------------
    # * Refresh
    #--------------------------------------------------------------------------
    def refresh
      self.contents.clear
      draw_item_name(@item, 4, 0)
      self.contents.font.color = normal_color
      self.contents.draw_text(240, 0, 16, 32, "×")
      self.contents.draw_text(260, 0, 56, 32, "#{@number} =", 2)
      self.cursor_rect.set(256, 0, 64, 32)
      # Draw total price and currency units
      domination = $data_system.words.gold
      cx = contents.text_size(domination).width
      total_price = @price * @number
      self.contents.font.color = normal_color
      self.contents.draw_text(- 4, 0, contents.width - cx - 8, 32, 
        total_price.to_s, 2)
      self.contents.font.color = system_color
      self.contents.draw_text(- 4, 0, contents.width, 32, domination, 2)
    end
  end
  
  #==============================================================================
  # ** FFT_ShopSystem
  #==============================================================================
  
  class Scene_Shop
    #--------------------------------------------------------------------------
    # * Main Processing
    #--------------------------------------------------------------------------
    def main
      # Memorize BGM & Play Shop Music
      $game_temp.map_bgm = $game_system.playing_bgm
#      Audio.bgm_play("Audio/BGM/31 - Mysterious Shop", 100, 100)
      # Create Command Window
      @command_window = Window_ShopCommand.new
      # Create Spriteset
      @spriteset = Spriteset_Map.new
      # Create Gold Window
      @gold_window = Window_Gold.new
      @gold_window.x = 640 - @gold_window.width - 16
      @gold_window.y = 480 - @gold_window.height - 16
      # Create Buy Window
      @buy_window = FFT_ShopSystem::Window_ShopBuy.new($game_temp.shop_goods)
      @buy_window.help_window = @help_window
      # Create Sell Window
      @sell_window = FFT_ShopSystem::Window_ShopSell.new
      @sell_window.help_window = @help_window
      # Create Number Window
      @number_window = FFT_ShopSystem::Window_ShopNumber.new
      # Transition run
      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 Scene Objects
      @spriteset.dispose
      @command_window.dispose
      @gold_window.dispose
      @buy_window.dispose
      @sell_window.dispose
      @number_window.dispose
    end
    #--------------------------------------------------------------------------
    # * Frame Update
    #--------------------------------------------------------------------------
    def update
      # Update windows
      @command_window.update
      @gold_window.update
      @buy_window.update
      @sell_window.update
      @number_window.update
      # If command window is active: call update_command
      if @command_window.active
        update_command
        return
      # If buy window is active: call update_buy
      elsif @buy_window.active
        update_buy
        return
      # If sell window is active: call update_sell
      elsif @sell_window.active
        update_sell
        return
      # If quantity input window is active: call update_number
      elsif @number_window.active
        update_number
        return
      end
    end
    #--------------------------------------------------------------------------
    # * Frame Update (when command window is active)
    #--------------------------------------------------------------------------
    def update_command
      # If B button was pressed
      if Input.trigger?(Input::B)
        # Play cancel SE
        $game_system.se_play($data_system.cancel_se)
        # Switch to map screen
        $scene = Scene_Map.new
        return
      end
      # If C button was pressed
      if Input.trigger?(Input::C)
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Branch by command window cursor position
        case @command_window.index
        when 0  # buy
          # Change windows to buy mode
          @command_window.active = false
          @buy_window.active = true
          @buy_window.visible = true
          @buy_window.refresh
        when 1  # sell
          # Change windows to sell mode
          @command_window.active = false
          @sell_window.active = true
          @sell_window.visible = true
          @sell_window.refresh
        when 2  # quit
          # Switch to map screen
          $scene = Scene_Map.new
        end
        return
      end
    end
    #--------------------------------------------------------------------------
    # * Frame Update (when buy window is active)
    #--------------------------------------------------------------------------
    def update_buy
      # If B button was pressed
      if Input.trigger?(Input::B)
        # Play cancel SE
        $game_system.se_play($data_system.cancel_se)
        # Change windows to initial mode
        @command_window.active = true
        @buy_window.active = false
        @buy_window.visible = false
        return
      end
      # If C button was pressed
      if Input.trigger?(Input::C)
        # Get item
        @item = @buy_window.item
        # If item is invalid, or price is higher than money possessed
        if @item == nil or @item.price > $game_party.gold
          # Play buzzer SE
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        # Get items in possession count
        case @item
        when RPG::Item
          number = $game_party.item_number(@item.id)
        when RPG::Weapon
          number = $game_party.weapon_number(@item.id)
        when RPG::Armor
          number = $game_party.armor_number(@item.id)
        end
        # If 99 items are already in possession
        if number == 99
          # Play buzzer SE
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Calculate maximum amount possible to buy
        max = @item.price == 0 ? 99 : $game_party.gold / @item.price
        max = [max, 99 - number].min
        # Turn Buy Window Off
        @buy_window.active = false
        # Change windows to quantity input mode
        @number_window.set(@item, max, @item.price)
        @number_window.active = true
        @number_window.visible = true
      end
    end
    #--------------------------------------------------------------------------
    # * Frame Update (when sell window is active)
    #--------------------------------------------------------------------------
    def update_sell
      # If B button was pressed
      if Input.trigger?(Input::B)
        # Play cancel SE
        $game_system.se_play($data_system.cancel_se)
        # Change windows to initial mode
        @command_window.active = true
        @sell_window.active = false
        @sell_window.visible = false
        return
      end
      # If C button was pressed
      if Input.trigger?(Input::C)
        # Get item
        @item = @sell_window.item
        # If item is invalid, or item price is 0 (unable to sell)
        if @item == nil or @item.price == 0
          # Play buzzer SE
          $game_system.se_play($data_system.buzzer_se)
          return
        end
        # Play decision SE
        $game_system.se_play($data_system.decision_se)
        # Get items in possession count
        case @item
        when RPG::Item
          number = $game_party.item_number(@item.id)
        when RPG::Weapon
          number = $game_party.weapon_number(@item.id)
        when RPG::Armor
          number = $game_party.armor_number(@item.id)
        end
        # Maximum quanitity to sell = number of items in possession
        max = number
        # Turn Sell Window Off
        @sell_window.active = false
        # Change windows to quantity input mode
        @number_window.set(@item, max, @item.price / 2)
        @number_window.active = true
        @number_window.visible = true
      end
    end
    #--------------------------------------------------------------------------
    # * Frame Update (when quantity input window is active)
    #--------------------------------------------------------------------------
    def update_number
      # If B button was pressed
      if Input.trigger?(Input::B)
        # Play cancel SE
        $game_system.se_play($data_system.cancel_se)
        # Set quantity input window to inactive / invisible
        @number_window.active = false
        @number_window.visible = false
        return
      end
      # If C button was pressed
      if Input.trigger?(Input::C)
        # Play shop SE
        $game_system.se_play($data_system.shop_se)
        # Set quantity input window to inactive / invisible
        @number_window.active = false
        @number_window.visible = false
        # Branch by command window cursor position
        case @command_window.index
        when 0  # buy
          # Buy process
          $game_party.lose_gold(@number_window.number * @item.price)
          case @item
          when RPG::Item
            $game_party.gain_item(@item.id, @number_window.number)
          when RPG::Weapon
            $game_party.gain_weapon(@item.id, @number_window.number)
          when RPG::Armor
            $game_party.gain_armor(@item.id, @number_window.number)
          end
          # Refresh each window
          @gold_window.refresh
          @buy_window.refresh
          # Change windows to buy mode
          @buy_window.active = true
          @buy_window.visible = true
        when 1  # sell
          # Sell process
          $game_party.gain_gold(@number_window.number * (@item.price / 2))
          case @item
          when RPG::Item
            $game_party.lose_item(@item.id, @number_window.number)
          when RPG::Weapon
            $game_party.lose_weapon(@item.id, @number_window.number)
          when RPG::Armor
            $game_party.lose_armor(@item.id, @number_window.number)
          end
          # Refresh each window
          @gold_window.refresh
          @sell_window.refresh
          # Change windows to sell mode
          @sell_window.active = true
          @sell_window.visible = true
        end
        return
      end
    end
  end
end

All bugs fixed. Now, I did alter some of the windows dimensions, and changed a few things around. Your main concern is just changing your Shop Command Graphic to be 128 pixels tall compared to what it used to be.

In the future, I suggest making your windows, padding, etc. even numbers, usually something divisible by 16, 32, etc. That way, it has a nice appearance. Random numbers is ok, but considering nearly everything else is done with this pattern of 32, its better to use that.

There's only one other thing I would change:
Code:
      @command_graphic.x = self.x + 1
      @command_graphic.y = self.y + 7

...

      @showback_graphic.x = self.x + 1
      @showback_graphic.y = self.y + 1

Instead, change it to just be self.x, self.y and make your picture dimensions match that of your window.

That's all I have. If you need anything, just let me know.
 
Status
Not open for further replies.

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