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.

[RMXP] Changing picture opacity, and wait commands ?

Jason

Awesome Bro

Hi,

So basically, I've created a script that, if a variable is equal to a number, an image will appear onscreen,

However, I'm having a few problems so far, because there are three possibilities for the variable, which means three pictures can be shown, however, when the variable switch from 1 to 2, as you can imagine, picture 1 would dispose, and 2 would appear, however, this isn't the issue, as I don't know how to dispose of just one of the images. I'm also trying to get the opacity of the images to fade in when called, and fade out when they are disposed, which is becoming annoying. And also, would anyone be able to help me with making wait commands in a script, because as one image is disposed, I'm guessing the next image will be called directly after, however, I'm wanting it to wait x amount of frames first, so the transition doesn't happen too quickly.

Thanks to anyone that can help !
 
To wait using a script do something like that:
Code:
  def wait_a_sec

    while(true)

      Graphics.update

      if Graphics.frame_count / Graphics.frame_rate != @total_sec

        @total_sec = Graphics.frame_count / Graphics.frame_rate

        break

      end

    end

  end
Inside your class's update, or prior to using this, set @total_sec:
@total_sec = Graphics.frame_count / Graphics.frame_rate
making a fade-in /out would be :
Code:
 def fade_in(pic_name)

           @pic = Sprite.new

           @pic.bitmap = RPG::Cache.picture(pic_name)

            @pic.opacity = 0

            for i in 0...3

              @pic.opacity = i* 75

               wait_a_sec

            end

            @pic.opacity = 255

end
also, isn't disposing a picture simply pic.dispose ?
 
Actually, that won't work as a wait command. What you have to do is set a variable to however many frames that you want to wait, and then have an if statement in the update method that checks the variable, and, if it is <= 0, does what you want, but if it isn't, it cycles the variable down by 1.

As to the pictures, it depends on exactly what you want. To perfrom actions on a specific picture, you use the following bit of code:

Code:
picture = $game_screen.pictures[picture_number]

picture_number is the same number that you set it to in events. If you want to know what kinds of actions you can do to each individual picture, check out Game_Picture, in the default scripts.

Finally, I'd avoid the method for fading that was suggested by silver wind. That, like his wait method, will freeze the game until it is over. Instead, you'll want to use the default move command, in a similar fashion to this:

Code:
picture = $game_screen.pictures[picture_number]

duration = Graphics.frame_rate

origin = picture.origin

x = picture.x

y = picture.y

zoom_x = picture.zoom_x

zoom_y = picture.zoom_y

opacity = 0

blend_type = picture.blend_type

picture.move(duration, origin, x, y, zoom_x, zoom_y, opacity, blend_type)

That code will fade the picture out over a period of one second. If you want to change the time, edit duration, keeping in minde that Graphics.frame_rate always equals the target number of frames in one second. (Otherwise, it may be off, because the game's frame rate is either 20 or 40, depending on whether the player has smooth mode on or off) If you want to fade the picture in to completely opaque, you have to change opacity to 255.

Oh, and silver wind, you can't actually dispose a picture using the dafault scripts. Instead, you have to use the default erase command, which sould be picture.erase after you had set picture to the actual picture you wanted to get rid of. This is because a picture is not a bitmap or window, but is instead a class that contains information for a bitmap, which is read elsewhere and displayed. Although, once the erase command is executed, one of the other scripts may actually dispose the picture's bitmap. THat's what an efficient scripter would have done, based on the default setup.
 
Yeah, bad scripting on my side. I actually went and fixed it, but you were quicker :D
I didn't know about dispose.. It's interesting, as I used pic.dispose few times and never had a problem.
Anyway, here it is.
It's used like this:
pic = Fade_Pic.new("my_mic.png", 0, 0)
pic.fade_in
pic.fade_out(rate, duration)
* the parameters for fade_in/out can be left out.
rate : pic's opacity is changed every 'rate' frames
duration: How long it'll take to fade in/out in frames.

Code:
 

class Fade_Pic

  # rate : How fast the pic opacity changes. number of frames.

  # duration : How long it takes the picture to fade in/out, num of frames.

  def initialize(pic_name, x ,y)

    @rate = 10

    @duration = Graphics.frame_rate/@rate

    @pic = Sprite.new

    @pic.bitmap = RPG::Cache.picture(pic_name)

    @pic.x = x

    @pic.y = y

  end

  

  def wait(frames)

    @wait = frames

     while(@wait>0)

       update

     end

  end

 

  def fade_in (rate = @rate, duration = @duration)

    @pic.opacity = 0

    for i in 0...@duration

      # wait x frames

      wait(@rate)

      @pic.opacity = i* (255/@duration).to_i

    end

    @pic.opacity = 255

  end

  

  def fade_out (rate = @rate, duration = @duration)

    @pic.opacity = 255

    for i in 0...@duration

      # To wait 1 sec, frames= Graphics.frame_rate

      wait(@rate)

      @pic.opacity = @pic.opacity - ( (255/@duration).to_i )

    end

    @pic.opacity = 0

  end

 

  def update

    @wait -=1

    $scene.update

    Graphics.update

  end

  

end

Or, you can use Glitchfinder's script. :)
 
I just went back and checked the Sprite class, which the Picture class is derived from. Apparently, the Sprite class does have a dispose option, but it isn't used by the event interpreter. Instead, it uses the erase command. I'm not entirely sure what would have happened if you tried to add a picture to one you had disposed, but I think it may throw an error, either about nilclass or about already being disposed.
 

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