Skip to content

Cannot subclass MG::Sprite using normal Ruby initializer #76

@andrewhavens

Description

@andrewhavens

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions