In trying to subclass MG::Sprite I discovered that it is possible to subclass MG::Sprite. However, the technique is not intuitive. In order to subclass MG::Sprite you have to define a self.new method instead of the typical initialize method:
class Character < MG::Sprite
# this works, but is not like Ruby
def self.new(filename = "default.png")
puts "Executing Character.new"
instance = super
instance.attach_physics_box
instance.dynamic = false
instance.scale = 2
instance
end
# this is the approach that I would expect, but it does not get called
def initialize(filename = "default.png")
puts "Executing Character#initialize" # does not get called
super
attach_physics_box
self.dynamic = false
self.scale = 2
end
end
In trying to subclass
MG::SpriteI discovered that it is possible to subclassMG::Sprite. However, the technique is not intuitive. In order to subclassMG::Spriteyou have to define aself.newmethod instead of the typicalinitializemethod: