Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
andrewhavens opened this issue Mar 9, 2017 · 0 comments
Open

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

andrewhavens opened this issue Mar 9, 2017 · 0 comments

Comments

@andrewhavens
Copy link
Contributor

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant