blinking text attempt

● ARCHIVED · READ-ONLY
Started by chungsie 5 posts View original ↗
  1. I'm trying to make blinking text. To display Insert coin and have it appear and vanish every 2 seconds.
    I cannot find any tutorials for this, or documentation describing a method for this.

    Code:
    class Window_Flash_Start < Window_Base
     
      def initialize(x,y,w,h)
        super
        contents.font.size = 42
        $show_text = true
        #start_timer
        draw_content
      end
     
      def draw_content
        draw_text(0,0,"Insert Coin".length * 42,48,"Insert Coin", 1) if $show_text
      end
     
      def start_timer
        i = 0
        for i in 0...Graphics.frame_rate * 20
          if i < Graphics.frame_rate * 2 then
            $show_text = true
          else
            $show_text = false
          end
          break if Input.trigger?(11)
        end
        pause_start if $show_text == true
      end
     
      def pause_start
        i = 0
        for i in 0...Graphics.frame_rate * 2
        end
        update
      end
     
      def update
        $Graphics.update
        start_timer
      end
    end

    simple, establish with a window, draw text, start a timer, pause the timer, update. but I'm not sure why it's failing. If I run without start_timer, draws the text fine. if I run with start_timer, no text is drawn.

    Have I made some simple mistake in my logic?
  2. 'Scripts' is for completed scripts only.
    [move]RGSSx Script Support[/move]
  3. Your start_timer will always end up being false after the whole "for" block runs since you run it from 0 to framerate*20 amd makes it return false once the index is equal to or greater than framerate*2..

    Remember that the "for" block will be finished first (meaning loop thru all index) before the method proceeds, so only the final value of show_text would be considered by the next method which is draw_contents

    So before your draw_contents method is called, the value of show_text is already false. That is why its not shown when you run it with the start_timer, your pause_start will never even be called too since its outside for loop, so show_start is also always false before that line is called

    It seems you want the text to appear and disappear over time, in which case you dont want a for loop since as I said it loops thru everything before it proceeds to the next code, what you should do in the start_timer method is to manually increase the index variable everytime that method is run.

    so basically like

    Code:
    @index = 0 if @index.nil?
    @index += 1
    if @index < framerate*2
      show_text = true
    else
      show_text = false
    end
  4. did you call the object window from a scene as
    Code:
    window = Window_Flash_Start.new

    ?
  5. Why are you using global variables if those variables are tied to the object they are made in anyway? Use instant variables.
    Global variables are only needed if you have something that should be accessible from all classes (or close to all). Well, it's not really necessary even than, but that's at least a valid reason to use them.

    Once you draw something on a bitmap, it stays there for good unless you clear or dispose (and change, optionally) that same bitmap.
    This renders your whole attempt of adding that condition check there kinda useless.
    Even if you would clear it before and redraw it later over and over again when it's needed to show up, it would be extremely inefficient and needless.
    The only thing you need to do is show and hide the window itself (or it's content, if you still want to have the window graphics shown), since the only thing it contains is that one liner text you draw on it.

    But all that aside, why would you remove the default update method and replace it with something that's already being updated in another class?
    The update method of a window will always run if you made that window object in a scene class and if it's stored in it's own variable (meaning not in an array or hash, or in another object, etc).
    If it's not, you can still call the update method every frame in whichever class you made it, and place your show/hide logic there, you don't have to use any kind of loops for it (actually, you shouldn't).

    Draw that text right on object creation, just like you have now.
    Initialize an instant variable for the timer. The window already has some built-in variables for different show flags/opacity values, so you don't need anything else.
    Finally, make the window appear and disappear based on that timer in the update method.
    You should end up with something like this:
    Spoiler
    Code:
    class Some_Window < Window_Base
    
      def initialize(x,y,w,h)
        @timer = 120
        super(x,y,w,h)
        draw_stuff
      end
    
      def draw_stuff()
        # Your text drawing here...
      end
    
      def update
        super
        update_vis
      end
    
      def update_vis
        if @timer > 0
          @timer -= 1
        else
          @timer = 120
          self.visible = !self.visible
        end
      end
    
    end
    If you want to make the text disappear only while still showing the window graphics, you can change the contents_opacity value of the window instead of the visible flag. That opacity can be set to 0 to 255, 0 being fully transparent.

    And that's it. Assuming you create this window in a scene class, you don't even have to do anything else, just make it there and forget about it.

    Note that I assumed you use VX Ace. This might work differently in VX or XP, so if you use those, you should let us know.

    From the looks of it, you wanted to do something else here when the player press a key too (but no idea what 11 is, you probably use a custom input script?), right?
    Depending on what you want to happen on that key press, the solution is different, so I won't go there now unless you ask about that later.

    Btw... This part:
    Code:
    "Insert Coin".length * 42
    Is probably one of the most unnecessary code bit I ever saw in my coding history. :D
    Not trying to be rude, so I hope you won't get offended by this, just trying to point out a bad habit you may have picked up from somewhere (I saw something like this in other scripts already, sadly).
    You know what that text will be, you used a static string there. This makes it pointless to make the system count the length of that string and multiply it by 42, you can easily do it yourself within less than 2 seconds. Don't make your code do unnecessary things, that's the number 1 rule everyone should learn when trying to learn coding. Even if that particular code bit won't really affect performance since it's only called once, it is still unnecessary.
    What you did here could be justified if that string would be a dynamically changing string, but it's not, so help your code and replace that with the real number instead (462). :p

    There goes my coffee break! :D