Class: Ruby2D::Sprite
- Inherits:
-
Object
- Object
- Ruby2D::Sprite
- Defined in:
- lib/ruby2d/sprite.rb,
ext/ruby2d/ruby2d-opal.rb
Instance Attribute Summary collapse
-
#clip_h ⇒ Object
Returns the value of attribute clip_h.
-
#clip_w ⇒ Object
Returns the value of attribute clip_w.
-
#clip_x ⇒ Object
Returns the value of attribute clip_x.
-
#clip_y ⇒ Object
Returns the value of attribute clip_y.
-
#data ⇒ Object
Returns the value of attribute data.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
readonly
Returns the value of attribute z.
Instance Method Summary collapse
- #add(animations) ⇒ Object
- #animate(animation) ⇒ Object
- #ext_init(path) ⇒ Object
- #ext_render ⇒ Object
-
#initialize(x, y, path, z = 0) ⇒ Sprite
constructor
A new instance of Sprite.
-
#remove ⇒ Object
TODO: Sprite already has an ‘add` method, have to reconsile def add if Module.const_defined? :DSL Application.add(self) end end.
- #reset ⇒ Object
- #start(x, y, w, h) ⇒ Object
Constructor Details
#initialize(x, y, path, z = 0) ⇒ Sprite
Returns a new instance of Sprite.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ruby2d/sprite.rb', line 9 def initialize(x, y, path, z=0) # unless File.exists? path # raise Error, "Cannot find image file `#{path}`" # end @x, @y, @path = x, y, path @clip_x, @clip_y, @clip_w, @clip_h = 0, 0, 0, 0 @default = nil @animations = {} @current_animation = nil @current_frame = 0 @current_frame_time = 0 @z = z ext_init(path) if Module.const_defined? :DSL Application.add(self) end end |
Instance Attribute Details
#clip_h ⇒ Object
Returns the value of attribute clip_h.
6 7 8 |
# File 'lib/ruby2d/sprite.rb', line 6 def clip_h @clip_h end |
#clip_w ⇒ Object
Returns the value of attribute clip_w.
6 7 8 |
# File 'lib/ruby2d/sprite.rb', line 6 def clip_w @clip_w end |
#clip_x ⇒ Object
Returns the value of attribute clip_x.
6 7 8 |
# File 'lib/ruby2d/sprite.rb', line 6 def clip_x @clip_x end |
#clip_y ⇒ Object
Returns the value of attribute clip_y.
6 7 8 |
# File 'lib/ruby2d/sprite.rb', line 6 def clip_y @clip_y end |
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/ruby2d/sprite.rb', line 6 def data @data end |
#x ⇒ Object
Returns the value of attribute x.
6 7 8 |
# File 'lib/ruby2d/sprite.rb', line 6 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
6 7 8 |
# File 'lib/ruby2d/sprite.rb', line 6 def y @y end |
#z ⇒ Object (readonly)
Returns the value of attribute z.
7 8 9 |
# File 'lib/ruby2d/sprite.rb', line 7 def z @z end |
Instance Method Details
#add(animations) ⇒ Object
35 36 37 |
# File 'lib/ruby2d/sprite.rb', line 35 def add(animations) @animations.merge!(animations) end |
#animate(animation) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/ruby2d/sprite.rb', line 39 def animate(animation) if @current_animation != animation @current_frame = 0 @current_frame_time = 0 @current_animation = animation end animate_frames(@animations[animation]) end |
#ext_init(path) ⇒ Object
172 173 174 |
# File 'ext/ruby2d/ruby2d-opal.rb', line 172 def ext_init(path) `#{self}.data = S2D.CreateSprite(path);` end |
#ext_render ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'ext/ruby2d/ruby2d-opal.rb', line 176 def ext_render ` #{self}.data.x = #{self}.x; #{self}.data.y = #{self}.y; S2D.ClipSprite( #{self}.data, #{self}.clip_x, #{self}.clip_y, #{self}.clip_w, #{self}.clip_h ); S2D.DrawSprite(#{self}.data); ` end |
#remove ⇒ Object
TODO: Sprite already has an ‘add` method, have to reconsile def add
if Module.const_defined? :DSL
Application.add(self)
end
end
60 61 62 63 64 |
# File 'lib/ruby2d/sprite.rb', line 60 def remove if Module.const_defined? :DSL Application.remove(self) end end |
#reset ⇒ Object
48 49 50 51 |
# File 'lib/ruby2d/sprite.rb', line 48 def reset clip(@default[0], @default[1], @default[2], @default[3]) @current_animation = nil end |
#start(x, y, w, h) ⇒ Object
30 31 32 33 |
# File 'lib/ruby2d/sprite.rb', line 30 def start(x, y, w, h) @default = [x, y, w, h] clip(x, y, w, h) end |