Created
January 3, 2015 23:05
-
-
Save shawn42/de709173e0aa40f547fa to your computer and use it in GitHub Desktop.
fading text in gamebox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define_behavior :fades do | |
requires :director | |
setup do # |opts| | |
fade_out_time = 3_000 | |
actor.has_attributes total_fade_time: fade_out_time, fade_time: fade_out_time | |
director.when :update do |time_ms, secs| | |
actor.fade_time -= time_ms | |
alpha = 255 * actor.fade_time / actor.total_fade_time | |
actor.remove if actor.fade_time <= 0 | |
# must set color to a different array to trigger the change event.. | |
actor.color = actor.color[0..2] + [alpha] | |
end | |
reacts_with :remove | |
end | |
helpers do | |
def remove | |
director.unsubscribe_all self | |
end | |
end | |
end | |
define_stage :demo do | |
# gamebox usually encourages the adding of behaviors from within another behavior, to do it from the stage we need: | |
requires :behavior_factory | |
curtain_up do | |
@text = create_actor :label, x: 10, y:30, text: "In the beginning" | |
timer_manager.add_timer :turn_the_page, 1_000, false do | |
behavior_factory.add_behavior @text, :fades #, fade_time: 3_000 ## opts can be passed in here | |
end | |
@text.when :remove_me do | |
fire :next_stage | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment