omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Why does an error happen when I try to call the subclass of "SpriteNode"?

    Pythonista
    subclass error spritenode
    2
    2
    2551
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • happy_variable
      happy_variable last edited by

      Here is the subclass of SpriteNode

      class Powerup(SpriteNode):
      def init(name, duration, from_brick):
      self.name = name
      self.duration = duration
      self.from_brick = from_brick
      SpriteNode.init(self.name, position=from_brick.position)

      def __setattr__(name, duration, from_brick):
      	SpriteNode.__setattr__(name, duration, from_brick)
      

      It actually must display a Powerup on the screen.
      Don't worry about the from_brick.position attribute in the init function, the from_brick attribute of my class must be a SpriteNode.

      Notice: I have done all the indents, but they may be not displayed, don't worry.

      Here is how I call the class to create a Powerup:

      pup = Powerup("plf:Item_Star", 30, brickOnScreen)

      The brickOnScreen variable was defined.

      Gives a TypeError, that init() takes 3 arguments, but 4 were given (The error bookmark points at the line where I assign the class to the pup variable.

      Thanks for your answers, programmers!

      1 Reply Last reply Reply Quote 0
      • bennr01
        bennr01 last edited by bennr01

        @happy_variable try the following instead of __init__:
        def __init__ (self, name, dration, from_brick):
        In python, class methods (except the staticmethods or classmethods) are called with the instance of the class as the first argument.

        EDIT: You will also need to replace SpriteNode.__setattr__(name, duration, from_brick) with SpriteNode.__setattr__(self, name, duration, from_brick).
        This is because SpriteNode.__setattr__ is a unbound class method.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Powered by NodeBB Forums | Contributors