Class: Smalruby3::Sprite
- Inherits:
-
Object
- Object
- Smalruby3::Sprite
- Includes:
- Smalruby3::SpriteMethod::Control, Smalruby3::SpriteMethod::Events, Smalruby3::SpriteMethod::Looks, Smalruby3::SpriteMethod::Motion, Smalruby3::SpriteMethod::MyBlocks, Smalruby3::SpriteMethod::Operations, Smalruby3::SpriteMethod::Pen, Smalruby3::SpriteMethod::Sensing, Smalruby3::SpriteMethod::Sound, Smalruby3::SpriteMethod::Variables
- Defined in:
- lib/smalruby3/sprite.rb
Direct Known Subclasses
Constant Summary collapse
- ROTATION_STYLE =
{ all_around: "all around", left_right: "left-right", none: "don't rotate", }
Constants included from Smalruby3::SpriteMethod::Control
Smalruby3::SpriteMethod::Control::STOP_OPTION
Instance Attribute Summary collapse
-
#checking_hit_targets ⇒ Object
Returns the value of attribute checking_hit_targets.
-
#costumes ⇒ Object
Returns the value of attribute costumes.
-
#current_costume ⇒ Object
Returns the value of attribute current_costume.
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#enable_pen ⇒ Object
readonly
Returns the value of attribute enable_pen.
-
#event_handlers ⇒ Object
Returns the value of attribute event_handlers.
-
#lists ⇒ Object
Returns the value of attribute lists.
-
#name ⇒ Object
Returns the value of attribute name.
-
#pen_color ⇒ Object
Returns the value of attribute pen_color.
-
#rotation_style ⇒ Object
Returns the value of attribute rotation_style.
-
#size ⇒ Object
Returns the value of attribute size.
-
#threads ⇒ Object
Returns the value of attribute threads.
-
#variables ⇒ Object
Returns the value of attribute variables.
-
#visible ⇒ Object
Returns the value of attribute visible.
-
#volume ⇒ Object
Returns the value of attribute volume.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
- #costume ⇒ Object
- #draw ⇒ Object
- #fire(event, *options) ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Sprite
constructor
A new instance of Sprite.
- #join_threads(wait = false) ⇒ Object
- #position ⇒ Object
- #stage? ⇒ Boolean
Methods included from Smalruby3::SpriteMethod::Variables
#hide_list, #hide_variable, #show_list, #show_variable
Methods included from Smalruby3::SpriteMethod::Sensing
Methods included from Smalruby3::SpriteMethod::Control
#create_clone, #delete_this_clone, #forever, #repeat, #stop, #wait
Methods included from Smalruby3::SpriteMethod::Events
#broadcast, #broadcast_and_wait, #when
Methods included from Smalruby3::SpriteMethod::Looks
Methods included from Smalruby3::SpriteMethod::Motion
#direction=, #go_to, #move, #point_towards, #position=, #turn_left, #turn_right, #x=, #y=
Constructor Details
#initialize(name, options = {}, &block) ⇒ Sprite
Returns a new instance of Sprite.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/smalruby3/sprite.rb', line 45 def initialize(name, = {}, &block) @name = name @x = 0 @y = 0 @costumes = [] @current_costume = 0 @vector = { x: 1, y: 0 } @event_handlers = {} @threads = [] @name_to_costume = {} @dxruby_sprite = DXRuby::Sprite.new(0, 0) defaults = { x: 0, y: 0, direction: 90, visible: true, size: 100, current_costume: 0, costumes: [], rotation_style: ROTATION_STYLE[:all_around], variables: [], lists: [], } opts = Util.(, defaults) opts.each do |k, v| send("#{k}=", v) end World.instance.add_target(self) if block_given? instance_eval(&block) end end |
Instance Attribute Details
#checking_hit_targets ⇒ Object
Returns the value of attribute checking_hit_targets.
40 41 42 |
# File 'lib/smalruby3/sprite.rb', line 40 def checking_hit_targets @checking_hit_targets end |
#costumes ⇒ Object
Returns the value of attribute costumes.
32 33 34 |
# File 'lib/smalruby3/sprite.rb', line 32 def costumes @costumes end |
#current_costume ⇒ Object
Returns the value of attribute current_costume.
31 32 33 |
# File 'lib/smalruby3/sprite.rb', line 31 def current_costume @current_costume end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
28 29 30 |
# File 'lib/smalruby3/sprite.rb', line 28 def direction @direction end |
#enable_pen ⇒ Object (readonly)
Returns the value of attribute enable_pen.
41 42 43 |
# File 'lib/smalruby3/sprite.rb', line 41 def enable_pen @enable_pen end |
#event_handlers ⇒ Object
Returns the value of attribute event_handlers.
37 38 39 |
# File 'lib/smalruby3/sprite.rb', line 37 def event_handlers @event_handlers end |
#lists ⇒ Object
Returns the value of attribute lists.
35 36 37 |
# File 'lib/smalruby3/sprite.rb', line 35 def lists @lists end |
#name ⇒ Object
Returns the value of attribute name.
25 26 27 |
# File 'lib/smalruby3/sprite.rb', line 25 def name @name end |
#pen_color ⇒ Object
Returns the value of attribute pen_color.
42 43 44 |
# File 'lib/smalruby3/sprite.rb', line 42 def pen_color @pen_color end |
#rotation_style ⇒ Object
Returns the value of attribute rotation_style.
33 34 35 |
# File 'lib/smalruby3/sprite.rb', line 33 def rotation_style @rotation_style end |
#size ⇒ Object
Returns the value of attribute size.
30 31 32 |
# File 'lib/smalruby3/sprite.rb', line 30 def size @size end |
#threads ⇒ Object
Returns the value of attribute threads.
38 39 40 |
# File 'lib/smalruby3/sprite.rb', line 38 def threads @threads end |
#variables ⇒ Object
Returns the value of attribute variables.
34 35 36 |
# File 'lib/smalruby3/sprite.rb', line 34 def variables @variables end |
#visible ⇒ Object
Returns the value of attribute visible.
29 30 31 |
# File 'lib/smalruby3/sprite.rb', line 29 def visible @visible end |
#volume ⇒ Object
Returns the value of attribute volume.
43 44 45 |
# File 'lib/smalruby3/sprite.rb', line 43 def volume @volume end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
26 27 28 |
# File 'lib/smalruby3/sprite.rb', line 26 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
27 28 29 |
# File 'lib/smalruby3/sprite.rb', line 27 def y @y end |
Instance Method Details
#costume ⇒ Object
110 111 112 |
# File 'lib/smalruby3/sprite.rb', line 110 def costume @costumes[@current_costume] end |
#draw ⇒ Object
131 132 133 134 135 |
# File 'lib/smalruby3/sprite.rb', line 131 def draw if @visible @dxruby_sprite.draw end end |
#fire(event, *options) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/smalruby3/sprite.rb', line 137 def fire(event, *) if (events = @event_handlers[event]) events.each do |e| if e. == @threads << e.call end end end end |
#join_threads(wait = false) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/smalruby3/sprite.rb', line 147 def join_threads(wait = false) @threads.compact! error = false @threads.delete_if { |t| if t.alive? false else begin t.join rescue => e Util.print_exception(e) error = true end true end } if error exit(1) end if wait @threads.each(&:join) end end |
#position ⇒ Object
87 88 89 |
# File 'lib/smalruby3/sprite.rb', line 87 def position [x, y] end |
#stage? ⇒ Boolean
83 84 85 |
# File 'lib/smalruby3/sprite.rb', line 83 def stage? false end |