Update function not working?

LagV

Deckus Maximus
Member
Joined
Dec 24, 2015
Messages
7
Reaction score
0
First Language
Romanian
Primarily Uses
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 : 

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?
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,848
First Language
English
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.
 
Last edited by a moderator:

LagV

Deckus Maximus
Member
Joined
Dec 24, 2015
Messages
7
Reaction score
0
First Language
Romanian
Primarily Uses
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?
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
823
First Language
Hungarian
Primarily Uses
RMVXA
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".
 

LagV

Deckus Maximus
Member
Joined
Dec 24, 2015
Messages
7
Reaction score
0
First Language
Romanian
Primarily Uses
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 :

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
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
823
First Language
Hungarian
Primarily Uses
RMVXA
@enemies.each {|en| en.update if en && !en.disposed? }


Replace the update code I showed you with this.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Don't forget, aspiring writers: Personality isn't what your characters do, it is WHY they do it.
Hello! I would like to know if there are any pluggings or any way to customize how battles look?
I was thinking that when you start the battle for it to appear the eyes of your characters and opponents sorta like Ace Attorney.
Sadly I don't know how that would be possible so I would be needing help! If you can help me in any way I would really apreciate it!
The biggest debate we need to complete on which is better, Waffles or Pancakes?
rux
How is it going? :D
Day 9 of giveaways! 8 prizes today :D

Forum statistics

Threads
106,050
Messages
1,018,548
Members
137,835
Latest member
yetisteven
Top