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.

Undefined method error

Code:
#==============================================================================
# ** Game_Weapon
#------------------------------------------------------------------------------
#  This script controls the gun operations.
#==============================================================================

class Game_Weapon
  def initialize
    @bullet = []
    for i in 0..29
      @bullet[i[2]] = 0
    end
    @bulletspeed = 1
    for i in 0..29
      @sprite_bullet = [i]
    end
  end

  def movebullet
    for i in 0..29
      if @bullet[i[2]] == 1
        @sprite_bullet[i].x += @bullet[i[0]]
        @sprite_bullet[i].y += @bullet[i[1]]
      end
    end
  end
  
  def trigger
    if Mouse.click?(1)
      for i in 0..29
        if @bullet[i[2]] == 0
          @angle = Math.atan((Mouse.pixels[0]-320)/((Mouse.pixels[1]-240)*-1))
          @bullet[i[0]] = @bulletspeed / (Math.sin @angle)
          @bullet[i[1]] = @bulletspeed / (Math.cos @angle)
          @sprite_bullet[i] = Sprite.new
          @sprite_bullet[i].bitmap = RPG::Cache.picture("bullet.PNG")
          @sprite_bullet[i].x = 320
          @sprite_bullet[i].y = 240
          @bullet[i[2]] = 1
          break
        end
      end
    end
  end
  
  def update
    for i in 0..29
      if @bullet[i[2]] == 1
        @sprite_bullet[i].update
      end
    end
  end
  
end

I keep getting an undefined method error

Code:
Script 'Game_Weapon' line 22: NoMethodError occurred
undefined method 'x' for nil:NilClass

Which is refering to this line (and probably the other lines using .x and .y)

@sprite_bullet.x += @bullet[i[0]]

I'm using DerVVulfmans Revised Mouse Script.

Anyone have any an anwser to my problem?

-Charles-
 
Such a simple fix.


        @sprite_bullet.x += @bullet[i[0]]

should be

        @sprite_bullet.x += @bullet[i[1]]


reason:      if @bullet[i[2]] == 1 effects what it is. The reason your Y doesn't have a problem is because you set it to the variable of 1. Set this to 1, and it should be fixed.
 
Based on the script, I think he is trying to make a bullet fire, and he is having a problem with this area.


The only problem i see is an uneven variable, besides that, it should work.


Any ideas khmp?
 
Code:
#==============================================================================
# ** Game_Weapon
#------------------------------------------------------------------------------
#  This script controls the gun operations.
#==============================================================================
class Bullet
  attr_accessor :x,:y,:x_speed,:y_speed,:need_delete
  def initialize
    #Your Stuff just copied
    @x = 320
    @y = 240 
    @x_speed = 0 
    @y_speed = 0
    @sprite = Sprite.new
    @sprite.bitmap = RPG::Cache.picture("bullet.PNG")
    @need_delete = false
  end
  #-----------------------------------------------------------------------------
  #Moves the Bullet
  def update
    return if @sprite.disposed?
    @x += @x_speed
    @y += @y_speed
    @sprite.x = @x
    @sprite.y = @y
    #checks if outside the screen
    if @sprite.x < 0 || @sprite.x > 640 || @sprite.y < 0 || @sprite.y > 480
      @need_delete = true
      #Removes sprite
      @sprite.dispose if !@sprite.disposed?
    end
  end
end
#==============================================================================
class Game_Weapon
  #-----------------------------------------------------------------------------
  def initialize
    @bullet = []
    @bullet_speed = 20 #Set this to your own value
  end
  #-----------------------------------------------------------------------------
  def trigger
    if Mouse.click?(1)
      if ( @bullets.size < 30) #Check if you haven't used the 30 bullets
        bullet = Bullet.new
        angle = Math.atan2((Mouse.pixels[0]-320)/((Mouse.pixels[1]-240)*-1))
        @bullet.x_speed = @bulletspeed / Math.sin(angle) #Set the x speed
        @bullet.y_speed = @bulletspeed / Math.cos(angle) #set the y speed
        #Adds the new Bullet
        @bullets.push(bullet)
      end
    end
  end
  #-----------------------------------------------------------------------------
  #UPDATE ALL BULLETS
  def update
    #Runs through all the bullets and Updates them
    for i in 0...@bullets.size
      @bullets[i].update
      #IF outside the screen
      if @bullet[i].need_delete
        @bullets.delete(@bullet)
      end
    end
  end
end
If Got it right, this is a better way of doing what you want, I comment some things, if you don't understand reply back and i will try to explain, if something is not working reply and I will try to fix it ;)

Good luck!
 
".size" method is giving me errors in your script.

And is anyone able to tell me why my original one isn't working? I have a feeling its somthing so simple but i just can't figure it out.

EDIT: Ok, ive tried everything, i just dont get it.

Code:
#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs map screen processing.
#==============================================================================

class Scene_Map
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main

#----------------------------------------------        
    $bullet = []
    for i in 0..29
      $bullet[i[2]] = 0
    end
    $bulletspeed = 1
    for i in 0..29
      $sprite_bullet = [i]
    end
#----------------------------------------------        
    
    # Make sprite set
    @spriteset = Spriteset_Map.new
    # Make message window
    @message_window = Window_Message.new
    # Transition run
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      
#----------------------------------------------    
      Mouse.update
      if Mouse.click?(1)
        for i in 0..29
          if $bullet[i[2]] == 0
            $angle = Math.atan((Mouse.pixels[0]-320)/((Mouse.pixels[1]-240)*-1))
            $bullet[i[0]] = $bulletspeed / (Math.sin $angle)
            $bullet[i[1]] = $bulletspeed / (Math.cos $angle)
            $sprite_bullet[i] = Sprite.new
            $sprite_bullet[i].bitmap = RPG::Cache.picture("bullet.PNG")
            $sprite_bullet[i].x = 320
            $sprite_bullet[i].y = 240
            print $sprite_bullet[i].x, $sprite_bullet[i].y
            $bullet[i[2]] = 1
            break
          end
        end
      end
      
      for i in 0..29
        if $bullet[i[2]] == 1
          $sprite_bullet[i].update
        end
      end
      
      for i in 0..29
        if $bullet[i[2]] == 1
          $sprite_bullet[i].x += $bullet[i[0]]
          $sprite_bullet[i].y += $bullet[i[1]]
        end
      end
#----------------------------------------------          
      
      
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of sprite set
    @spriteset.dispose
    # Dispose of message window
    @message_window.dispose
    # If switching to title screen
    if $scene.is_a?(Scene_Title)
      # Fade out screen
      Graphics.transition
      Graphics.freeze
    end
  end
  
  
  

    

  
  
  
  
  
  
  
  
  
  
  
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Loop
    loop do
      # Update map, interpreter, and player order
      # (this update order is important for when conditions are fulfilled 
      # to run any event, and the player isn't provided the opportunity to
      # move in an instant)
      $game_map.update
      $game_system.map_interpreter.update
      $game_player.update
      # Update system (timer), screen
      $game_system.update
      $game_screen.update
      # Abort loop if player isn't place moving
      unless $game_temp.player_transferring
        break
      end
      # Run place move
      transfer_player
      # Abort loop if transition processing
      if $game_temp.transition_processing
        break
      end
    end
    # Update sprite set
    @spriteset.update
    # Update message window
    @message_window.update
    # If game over
    if $game_temp.gameover
      # Switch to game over screen
      $scene = Scene_Gameover.new
      return
    end
    # If returning to title screen
    if $game_temp.to_title
      # Change to title screen
      $scene = Scene_Title.new
      return
    end
    # If transition processing
    if $game_temp.transition_processing
      # Clear transition processing flag
      $game_temp.transition_processing = false
      # Execute transition
      if $game_temp.transition_name == ""
        Graphics.transition(20)
      else
        Graphics.transition(40, "Graphics/Transitions/" +
          $game_temp.transition_name)
      end
    end
    # If showing message window
    if $game_temp.message_window_showing
      return
    end
    # If encounter list isn't empty, and encounter count is 0
    if $game_player.encounter_count == 0 and $game_map.encounter_list != []
      # If event is running or encounter is not forbidden
      unless $game_system.map_interpreter.running? or
             $game_system.encounter_disabled
        # Confirm troop
        n = rand($game_map.encounter_list.size)
        troop_id = $game_map.encounter_list[n]
        # If troop is valid
        if $data_troops[troop_id] != nil
          # Set battle calling flag
          $game_temp.battle_calling = true
          $game_temp.battle_troop_id = troop_id
          $game_temp.battle_can_escape = true
          $game_temp.battle_can_lose = false
          $game_temp.battle_proc = nil
        end
      end
    end
    # If B button was pressed
    if Input.trigger?(Input::B)
      # If event is running, or menu is not forbidden
      unless $game_system.map_interpreter.running? or
             $game_system.menu_disabled
        # Set menu calling flag or beep flag
        $game_temp.menu_calling = true
        $game_temp.menu_beep = true
      end
    end
    # If debug mode is ON and F9 key was pressed
    if $DEBUG and Input.press?(Input::F9)
      # Set debug calling flag
      $game_temp.debug_calling = true
    end
    # If player is not moving
    unless $game_player.moving?
      # Run calling of each screen
      if $game_temp.battle_calling
        call_battle
      elsif $game_temp.shop_calling
        call_shop
      elsif $game_temp.name_calling
        call_name
      elsif $game_temp.menu_calling
        call_menu
      elsif $game_temp.save_calling
        call_save
      elsif $game_temp.debug_calling
        call_debug
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Battle Call
  #--------------------------------------------------------------------------
  def call_battle
    # Clear battle calling flag
    $game_temp.battle_calling = false
    # Clear menu calling flag
    $game_temp.menu_calling = false
    $game_temp.menu_beep = false
    # Make encounter count
    $game_player.make_encounter_count
    # Memorize map BGM and stop BGM
    $game_temp.map_bgm = $game_system.playing_bgm
    $game_system.bgm_stop
    # Play battle start SE
    $game_system.se_play($data_system.battle_start_se)
    # Play battle BGM
    $game_system.bgm_play($game_system.battle_bgm)
    # Straighten player position
    $game_player.straighten
    # Switch to battle screen
    $scene = Scene_Battle.new
  end
  #--------------------------------------------------------------------------
  # * Shop Call
  #--------------------------------------------------------------------------
  def call_shop
    # Clear shop call flag
    $game_temp.shop_calling = false
    # Straighten player position
    $game_player.straighten
    # Switch to shop screen
    $scene = Scene_Shop.new
  end
  #--------------------------------------------------------------------------
  # * Name Input Call
  #--------------------------------------------------------------------------
  def call_name
    # Clear name input call flag
    $game_temp.name_calling = false
    # Straighten player position
    $game_player.straighten
    # Switch to name input screen
    $scene = Scene_Name.new
  end
  #--------------------------------------------------------------------------
  # * Menu Call
  #--------------------------------------------------------------------------
  def call_menu
    # Clear menu call flag
    $game_temp.menu_calling = false
    # If menu beep flag is set
    if $game_temp.menu_beep
      # Play decision SE
      $game_system.se_play($data_system.decision_se)
      # Clear menu beep flag
      $game_temp.menu_beep = false
    end
    # Straighten player position
    $game_player.straighten
    # Switch to menu screen
    $scene = Scene_Menu.new
  end
  #--------------------------------------------------------------------------
  # * Save Call
  #--------------------------------------------------------------------------
  def call_save
    # Straighten player position
    $game_player.straighten
    # Switch to save screen
    $scene = Scene_Save.new
  end
  #--------------------------------------------------------------------------
  # * Debug Call
  #--------------------------------------------------------------------------
  def call_debug
    # Clear debug call flag
    $game_temp.debug_calling = false
    # Play decision SE
    $game_system.se_play($data_system.decision_se)
    # Straighten player position
    $game_player.straighten
    # Switch to debug screen
    $scene = Scene_Debug.new
  end
  #--------------------------------------------------------------------------
  # * Player Place Move
  #--------------------------------------------------------------------------
  def transfer_player
    # Clear player place move call flag
    $game_temp.player_transferring = false
    # If move destination is different than current map
    if $game_map.map_id != $game_temp.player_new_map_id
      # Set up a new map
      $game_map.setup($game_temp.player_new_map_id)
    end
    # Set up player position
    $game_player.moveto($game_temp.player_new_x, $game_temp.player_new_y)
    # Set player direction
    case $game_temp.player_new_direction
    when 2  # down
      $game_player.turn_down
    when 4  # left
      $game_player.turn_left
    when 6  # right
      $game_player.turn_right
    when 8  # up
      $game_player.turn_up
    end
    # Straighten player position
    $game_player.straighten
    # Update map (run parallel process event)
    $game_map.update
    # Remake sprite set
    @spriteset.dispose
    @spriteset = Spriteset_Map.new
    # If processing transition
    if $game_temp.transition_processing
      # Clear transition processing flag
      $game_temp.transition_processing = false
      # Execute transition
      Graphics.transition(20)
    end
    # Run automatic change for BGM and BGS set on the map
    $game_map.autoplay
    # Frame reset
    Graphics.frame_reset
    # Update input information
    Input.update
  end
end


Code:
#==============================================================================
# ** Mouse Input Module (Revised)
#------------------------------------------------------------------------------
#   by DerVVulfman
#   version 1.2
#   08-18-2007
#------------------------------------------------------------------------------
#   Based on...
#   Mouse Input Module
#   by Near Fantastica
#------------------------------------------------------------------------------
#   Set_Pos feature by
#   Freakboy
#------------------------------------------------------------------------------
#
#   THE CALLS: 
#
#   Mouse.click?
#   This returns a true/false value  when you test whether a button is clicked.
#   The values you pass are 1 (for the left mouse button), 2 (for the right) or
#   3 (for the middle button).
#
#   Mouse.press?
#   This returns a true/false value  when you test  whether a button is pressed
#   and kept depressed.  The values you pass are 1 (for the left mouse button),
#   2 (for the right mouse button), or 3 (for the middle).
#
#   Mouse.pixels
#   This returns the  mouse's screen coordinates  in pixels.  Based on a screen
#   with a 640x480 dimension,  this returns an array of the mouse's position in
#   index values.  Calling Mouse.pixels returns both x & y positions  in a sin-
#   gle string,  but calling Mouse.pixels[0] returns the x position (0-639) and
#   calling Mouse.pixels[1]  returns  the y position (0-439).   If the mouse is
#   outside of the game's window region, this call returns nil.
#
#   Mouse.tiles
#   This returns  the mouse's screen  coordinates  in map tiles.   Based on the
#   system's 20x15 tile size,  this returns it in index values  (a 0-19 width & 
#   a 0-14 height).  This functions the same manner as Mouse.pixels.
#
#   Mouse.set_pos
#   This allows you  to forcefully position the mouse at an x/y position within
#   the game screen by pixel coordinates.  Given the game's normal screen width
#   of 640x480, adding:  Mouse.set_pos(320,240)  should position the mouse dead
#   center of the gaming window.
#
#   Mouse.update
#   Add this routine  into your update routines  to update  the mouse position.
#   It must be called otherwise you won't get valid mouse coordinates.
#
#==============================================================================

module Mouse
  @mouse_menu = 0
  #--------------------------------------------------------------------------
  # * Mouse Click
  #     button      : button
  #--------------------------------------------------------------------------
  def Mouse.click?(button)
    return true if @keys.include?(button)
    return false
  end  
  #--------------------------------------------------------------------------
  # * Mouse Pressed
  #     button      : button
  #--------------------------------------------------------------------------
  def Mouse.press?(button)
    return true if @press.include?(button)
    return false
  end
  #--------------------------------------------------------------------------
  # * Mouse Pressed
  #     button      : button
  #--------------------------------------------------------------------------
  def Mouse.area?(x, y, width=32, height=32)
    return false if @pos == nil
    return true if @pos[0] >= x and @pos[0] <= (x+width) and @pos[1] >= y and @pos[1] <= (y+height)
    return false
  end
  #--------------------------------------------------------------------------
  # * Mouse Pixel Position
  #--------------------------------------------------------------------------
  def Mouse.pixels
    return @pos == nil ? [0, 0] : @pos
  end
  #--------------------------------------------------------------------------
  # * Mouse Tile Position
  #--------------------------------------------------------------------------
  def Mouse.tiles
    return nil if @pos == nil
    x = @pos[0] / 32
    y = @pos[1] / 32
    return [x, y]
  end
  #--------------------------------------------------------------------------
  # * Set Mouse Position
  #-------------------------------------------------------------------------- 
  def Mouse.set_pos(x_pos=0, y_pos=0)
    width, height = Mouse.client_size
    if (x_pos.between?(0, width) && y_pos.between?(0, height))
      x = Mouse.client_pos[0] + x_pos; y = Mouse.client_pos[1] + y_pos
      Win32API.new('user32', 'SetCursorPos', 'NN', 'N').call(x, y)
    end
  end
  #--------------------------------------------------------------------------
  # * Mouse Update
  #--------------------------------------------------------------------------
  def Mouse.update
    @pos            = Mouse.pos
    @keys, @press   = [], []
    @keys.push(1)   if Win32API.new("user32","GetAsyncKeyState",['i'],'i').call(1) & 0X01 == 1
    @keys.push(2)   if Win32API.new("user32","GetAsyncKeyState",['i'],'i').call(2) & 0X01 == 1
    @keys.push(3)   if Win32API.new("user32","GetAsyncKeyState",['i'],'i').call(4) & 0X01 == 1
    @press.push(1)  if Win32API.new("user32","GetKeyState",['i'],'i').call(1) & 0X01 == 1
    @press.push(2)  if Win32API.new("user32","GetKeyState",['i'],'i').call(2) & 0X01 == 1
    @press.push(3)  if Win32API.new("user32","GetKeyState",['i'],'i').call(4) & 0X01 == 1
  end  
  #--------------------------------------------------------------------------
  # * Automatic functions below 
  #--------------------------------------------------------------------------
  #
  #--------------------------------------------------------------------------
  # * Obtain Mouse position in screen
  #--------------------------------------------------------------------------
  def Mouse.global_pos
    pos = [0, 0].pack('ll')
    if Win32API.new('user32', 'GetCursorPos', 'p', 'i').call(pos) != 0
      return pos.unpack('ll')
    else
      return nil
    end
  end
  #--------------------------------------------------------------------------
  # * Return Screen mouse position within game window
  #--------------------------------------------------------------------------
  def Mouse.pos
    x, y = Mouse.screen_to_client(*Mouse.global_pos)
    width, height = Mouse.client_size
    begin
      if (x >= 0 and y >= 0 and x < width and y < height)
        return x, y
      else
        return nil
      end
    rescue
      return nil
    end
  end
  #--------------------------------------------------------------------------
  #  * Pass Screen to Game System
  #--------------------------------------------------------------------------
  def Mouse.screen_to_client(x, y)
    return nil unless x and y
    pos = [x, y].pack('ll')
    if Win32API.new('user32', 'ScreenToClient', %w(l p), 'i').call(Mouse.hwnd, pos) != 0
      return pos.unpack('ll')
    else
      return nil
    end
  end
  #--------------------------------------------------------------------------
  # * Get Screen Window Handle
  #--------------------------------------------------------------------------
  def Mouse.hwnd
    game_name = "\0" * 256
    Win32API.new('kernel32', 'GetPrivateProfileStringA', %w(p p p p l p), 'l').call('Game','Title','',game_name,255,".\\Game.ini")
    game_name.delete!("\0")
    return Win32API.new('user32', 'FindWindowA', %w(p p), 'l').call('RGSS Player',game_name)
  end
  #--------------------------------------------------------------------------
  # * Get Game Window Size
  #--------------------------------------------------------------------------
  def Mouse.client_size
    rect = [0, 0, 0, 0].pack('l4')
    Win32API.new('user32', 'GetClientRect', %w(l p), 'i').call(Mouse.hwnd, rect)
    right, bottom = rect.unpack('l4')[2..3]
    return right, bottom
  end
  #--------------------------------------------------------------------------
  # * Get Window Position (RGSS Player)
  #--------------------------------------------------------------------------
  def Mouse.client_pos
    rect = [0, 0, 0, 0].pack('l4')
    Win32API.new('user32', 'GetWindowRect', %w(l p), 'i').call(Mouse.hwnd, rect)
    left, upper = rect.unpack('l4')[0..1]
    return left+4, upper+30
  end  
end

Thats all the code ive change/used instead of the defulats. Mouse goes above main, create any sized picture named "bullet.PNG". Two things have been added to Scene_Map which i have enclosed in comment blocks. This time i tried directly placeing my code into Scene_Map.

It seems using an array it deletes itself or somthing so when i call ".update" or ".x" or ".y" after the main calculation it isn't assigned as a sprite instance.


-Charles-
 
My mistake here is the fix
Code:
#==============================================================================
# ** Game_Weapon
#------------------------------------------------------------------------------
#  This script controls the gun operations.
#==============================================================================
class Bullet
  attr_accessor :x,:y,:x_speed,:y_speed,:need_delete
  def initialize
    #Your Stuff just copied
    @x = 320
    @y = 240 
    @x_speed = 0 
    @y_speed = 0
    @sprite = Sprite.new
    @sprite.bitmap = RPG::Cache.picture("bullet.PNG")
    @need_delete = false
  end
  #-----------------------------------------------------------------------------
  #Moves the Bullet
  def update
    return if @sprite.disposed?
    @x += @x_speed
    @y += @y_speed
    @sprite.x = @x
    @sprite.y = @y
    #checks if outside the screen
    if @sprite.x < 0 || @sprite.x > 640 || @sprite.y < 0 || @sprite.y > 480
      @need_delete = true
      #Removes sprite
      @sprite.dispose if !@sprite.disposed?
    end
  end
end
#==============================================================================
class Game_Weapon
  #-----------------------------------------------------------------------------
  def initialize
    @bullets = []
    @bullet_speed = 20 #Set this to your own value
  end
  #-----------------------------------------------------------------------------
  def trigger
    if Mouse.click?(1)
      if ( @bullets.size < 30) #Check if you haven't used the 30 bullets
        bullet = Bullet.new
        angle = Math.atan2((Mouse.pixels[0]-320)/((Mouse.pixels[1]-240)*-1))
        bullet.x_speed = @bulletspeed / Math.sin(angle) #Set the x speed
        bullet.y_speed = @bulletspeed / Math.cos(angle) #set the y speed
        #Adds the new Bullet
        @bullets.push(bullet)
      end
    end
  end
  #-----------------------------------------------------------------------------
  #UPDATE ALL BULLETS
  def update
    #Runs through all the bullets and Updates them
    for i in 0...@bullets.size
      @bullets[i].update
      #IF outside the screen
      if @bullets[i].need_delete
        @bullets.delete(@bullet)
      end
    end
  end
end
 
lol, that worked, but it fires when i dont click and when i do click i get an error.

EDIT: Ok, i did more checking and at random intervals i get an integer error. And the bullet moves about 45 degrees in the wrong direction. Maybe it due to my math, not sure though, ill have a look at it.


Also, anyone have an answer to my first script. I knida want to know why it doesn't work so that i can improve on my future scripts.

-Charles-
 

khmp

Sponsor

Math.atan2 takes two parameters I only see one. Try using:
Code:
angle = Math.atan2(Mouse.pixels[0]-320,Mouse.pixels[1]-240)

And you need to dispose of the bitmap thats held within the sprite.
Code:
@sprite.bitmap.dispose

You might want to create a disposal method for both the bullet class and the weapon class. Disposing of the weapon should dispose of all the bullets that are currently in use. Stick with the object oriented approach that Eduardo is suggesting. It's a hell of lot cleaner and Scene_Map main doesn't look like a used diaper. You can use alias_method to add code to main and update without overriding both. But these are design decisions I'll take a look into why the mouse thing is bugging.

Code:
#==============================================================================
# ** Game_Bullet
#------------------------------------------------------------------------------
#  This class will hold the bullet information that will be used inside Game_Weapon
#==============================================================================
class Game_Bullet
  def dispose
    @sprite.bitmap.dispose if !@sprite.bitmap.disposed?
    @sprite.dispose if !@sprite.disposed?
  end
end

#==============================================================================
# ** Game_Weapon
#------------------------------------------------------------------------------
#  This script controls the gun operations.
#==============================================================================
class Game_Weapon
  def dispose
    @bullets.each {|bullet| bullet.dispose}
  end
end

#==============================================================================
# ** Scene_Map
#------------------------------------------------------------------------------
#  This class performs map screen processing.
#==============================================================================
class Scene_Map
  alias_method :opt7_gameweapon_main, :main
  alias_method :opt7_gameweapon_update, :update
  def main
    # Create a new weapon.
    @weapon = Game_Weapon.new
    opt7_gameweapon_main
    # Destroy the weapon.
    @weapon.dispose
  end
  
  def update
    # Update the weapon which will update the bullets.
    @weapon.update
    # Check to see if the user wants to shoot again.
    @weapon.trigger
    opt7_gameweapon_update
  end
end
 

khmp

Sponsor

It is to my understanding that the bitmap of a sprite must also be disposed of. Sadly now I must ask a question. Does disposing of the sprite object also dispose of the bitmap? Or does it slip through until garbage cleanup occurs?
 

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