Update function not working?

I'm trying to make a mini-game for my on-going game but it seems like my script is not really working properly.


Here is what my script looks like : 

Spoiler
module Cache
  def self.cooku(filename)
    load_bitmap("Graphics/Cooku/", filename)
  end
end


class Cooku < Scene_Base
  
  def start
    super()
    cooky_sceney
  end


  def update
    super()
    update_user_movement
    update_salem
  end


  def update_user_movement
    if Input.press?🙂LEFT)
      @doomy.x -= @speedy if @doomy.x > 0
    end
    if Input.press?🙂RIGHT) 
      @doomy.x += @speedy if @doomy.x < 490
    end
    if (Input.press?🙂SHIFT) && Input.press?🙂LEFT))
      @doomy.x -= @speedy + 2.5 if @doomy.x > 0
    end
    if (Input.press?🙂SHIFT) && Input.press?🙂RIGHT))
      @doomy.x += @speedy + 2.5 if @doomy.x < 490
    end  
  end
  
  def update_salem
    if @spawn_timer <= 0
      @enemies << Sprite_Salem.new(@viewport1, @salem_count)
      @salem_count += 1
      @i_spawn_timer -= 1
      @spawn_timer = @i_spawn_timer
    end
    @spawn_timer -= 1
  end
  
  def cooky_sceney
    @speedy = 5
    @spawn_timer = 60
    @i_spawn_timer = 60
    @enemies = []
    @salem_count = 0
    @background = Sprite.new
    @doomy = Sprite.new
    @background.bitmap = Cache.picture("BS")
    @doomy.bitmap = Cache.picture("Doomy")
    @doomy.x = 222
    @doomy.y = 316
    @background.opacity = 0
    @doomy.opacity = 0
    Graphics.wait(15)
    Graphics.fadeout(2)
    @background.opacity = 255
    @doomy.opacity = 255
    Graphics.fadein(2)
    RPG::BGM.new("Airship", 100, 100).play
  end
end


class Sprite_Salem < Sprite
  
  def initialize(viewport, id)
    super(viewport)
    @id = id
    @salem_speedy = 2
    salem_mancare
    init_position
  end
  
  def init_position
    self.x = rand(490)
    self.y = 0
  end
  
  def dispose
    super()
  end
  
  def update
    super()
    update_move
  end
  
  def update_move
    self.y += @salem_speedy
  end
  
  def salem_mancare
    self.bitmap = Cache.cooku("Salem")
  end
end

The problem is the update function in the Sprite_Salem class doesn't seem to work, whatever I would type there.


What's the problem? Am I missing something?
What is the problem?
The sprite isn't updating? Are you telling the sprite to update?


Sprites do not update automatically.


It looks like you create a sprite in your update_salem method in your scene, but then you never actually update the sprites in that array of enemies.
What is the problem?
The sprite isn't updating? Are you telling the sprite to update?


Sprites do not update automatically.


It looks like you create a sprite in your update_salem method in your scene, but then you never actually update the sprites in that array of enemies.

Thanks for the answer! I thought that it would be enough to update each sprite in the Sprite_Salem class ( see update_move ). Would you mind telling me how to actually update the sprites in the array?
Add this into the update method of the scene:


@enemies.each {|en| en.update }


I see that you create these enemy sprites, but you doesn't seem to dispose them anywhere. 


That is a problem, you must dispose them when you don't need them anymore. At the very least, dispose them when you exit that scene, or else you risk game crashes and memory/FPS loss. 


First dispose their bitmaps, than the sprites. The order is important!
 


You can add this into your scene class:


def dispose_all_windows
super
@enemies.each {|en| en.bitmap.dispose; en.dispose}
end


And it will take care of any undisposed bitmaps/sprites created as an "enemy".
Thanks, it worked! I was actually planning to dispose but the script is still work in progress and I actually got to the part where I need to dispose but when I do, I get an error saying " disposed sprite ". I think it's trying to tell me that the sprite has been disposed and the script is still trying to update that disposed sprite, so it crashes. Any idea how this could be fixed?


Here's the script :

Spoiler
module Cache
  def self.cooku(filename)
    load_bitmap("Graphics/Cooku/", filename)
  end
end


class Cooku < Scene_Base
  
  def start
    super()
    cooky_sceney
  end


  def update
    super()
    if @running == true
      update_user_movement
      update_salem
      @enemies.each {|en| en.update }
      update_catch
    end
  end


  def update_catch
    @enemies.each_with_index {|en,i|
      if ( en.y >= 390 - @doomy.height )
        if en.x - @doomy.x < 50 && en.x - @doomy.x > -50
          en.dispose
        end
      end
    }
  end


  def update_user_movement
    if Input.press?🙂LEFT)
      @doomy.x -= @speedy if @doomy.x > 0
    end
    if Input.press?🙂RIGHT) 
      @doomy.x += @speedy if @doomy.x < 490
    end
    if (Input.press?🙂SHIFT) && Input.press?🙂LEFT))
      @doomy.x -= @speedy + 2.5 if @doomy.x > 0
    end
    if (Input.press?🙂SHIFT) && Input.press?🙂RIGHT))
      @doomy.x += @speedy + 2.5 if @doomy.x < 490
    end  
  end
  
  def update_salem
    if @spawn_timer <= 0
      @enemies << Sprite_Salem.new(@viewport1, @salem_count)
      @salem_count += 1
      @i_spawn_timer -= 1
      @spawn_timer = @i_spawn_timer
    end
    @spawn_timer -= 1
  end
  
  def cooky_sceney
    @running = true
    @speedy = 5
    @spawn_timer = 60
    @i_spawn_timer = 60
    $game_variables[5] = 0
    @enemies = []
    @salem_count = 0
    @background = Sprite.new
    @doomy = Sprite.new
    @background.bitmap = Cache.picture("BS")
    @doomy.bitmap = Cache.picture("Doomy")
    @doomy.x = 222
    @doomy.y = 316
    @background.opacity = 0
    @doomy.opacity = 0
    Graphics.wait(15)
    Graphics.fadeout(2)
    @background.opacity = 255
    @doomy.opacity = 255
    Graphics.fadein(2)
    RPG::BGM.new("Airship", 100, 100).play
  end
  
  def doomy_height
    @doomy.bitmap.height
  end
end


class Sprite_Salem < Sprite
  
  def initialize(viewport, id)
    super(viewport)
    @id = id
    @salem_speedy = 2
    salem_mancare
    init_position
  end
  
  def init_position
    self.x = rand(490)
    self.y = 0
  end
  
  def dispose
    super()
  end
  
  def update
    super()
    update_move
  end
  
  def update_move
    self.y += @salem_speedy * 2
  end
  
  def salem_mancare
    self.bitmap = Cache.cooku("Salem")
  end
end
@enemies.each {|en| en.update if en && !en.disposed? }


Replace the update code I showed you with this.

Original thread: https://forums.rpgmakerweb.com/threads/update-function-not-working.65521/