Skip to content

Instantly share code, notes, and snippets.

@shawn42
Created January 3, 2015 23:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawn42/de709173e0aa40f547fa to your computer and use it in GitHub Desktop.
Save shawn42/de709173e0aa40f547fa to your computer and use it in GitHub Desktop.
fading text in gamebox
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