SFML community forums

Bindings - other languages => Python => Topic started by: shackra on January 16, 2013, 01:42:59 am

Title: [SOLVED] sfml.Sprite inheritance nightmare
Post by: shackra on January 16, 2013, 01:42:59 am
import sfml

class BaseSprite(sfml.Sprite):
    def __init__(self, texture, arg1, arg2, arg3, rectangle=None):
        super(sfml.Sprite, self).__init__(texture, rectangle)
        self.__arg1 = arg1
        self.__arg2 = arg2
        self.__arg3 = arg3
    # ...
       
# Later, in other file...
import sfml
import media
from spritefactory import BaseSprite
from scenefactory import BaseScene

class Testsprite(BaseSprite):
    # second time inheritance
    def __init__(self, texture, arg1, arg2, arg3, rectangle=None):
        BaseSprite.__init__(texture, arg1, arg2, arg3, rectangle)
        self.setsomething(0)
       
class Scenetest(BaseScene):
    def __init__(self, scenemanager):
        BaseScene.__init__(self, scenemanager)
        self.loadmap("some/map.tmx")
        self.spritetexture = media.loadimg("some/sprite/sheet.png", False) # returns a Texture not an Image
        self.__sprite = {"mai": Test(self.spritetexture, 0, 1, 3)}
        # a bunch of code...
 

Hello! The code above is a less-detailed version of my actual source code. I'm having a nightmare trying to inheritance BaseSprite one more time. When I run the app, an awkward error shows up.

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    escena = escena_2.Maitest(director)
  File "/home/jorge/coders/desarrollo/thomas-aquinas/scenes/escena_2.py", line 22, in __init__
    "sprites/others/Mai Shiranui "
  File "graphics.pyx", line 1365, in sfml.graphics.Sprite.__cinit__ (src/sfml/graphics.cpp:23985)
TypeError: __cinit__() takes at most 2 positional arguments (4 given)
 

and if I tend to pass just one argument (i.e: the texture for the sprite) the exception change just a little:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    escena = escena_2.Maitest(director)
  File "/home/jorge/coders/desarrollo/thomas-aquinas/scenes/escena_2.py", line 21, in __init__
    self.__sprite = {"mai": Mai(self.maitext)}#, 0, self.scenemanager.window,
TypeError: __init__() takes exactly 5 arguments (2 given)

and if I don't pass any arguments at all, it changes again:

Traceback (most recent call last):
  File "main.py", line 8, in <module>
    escena = escena_2.Maitest(director)
  File "/home/jorge/coders/desarrollo/thomas-aquinas/scenes/escena_2.py", line 21, in __init__
    self.__sprite = {"mai": Mai()}#self.maitext), 0, self.scenemanager.window,
  File "graphics.pyx", line 1365, in sfml.graphics.Sprite.__cinit__ (src/sfml/graphics.cpp:23985)
TypeError: __cinit__() takes at least 1 positional argument (0 given)

For God's sake, somebody help me!! D':

cheers! :)
Title: Re: sfml.Sprite inheritance nightmare
Post by: eXpl0it3r on January 16, 2013, 02:00:10 am
I've no idea about the python binding, but in C++ one should not derive from the sprite class. It certainly is possible, but for OOP one should use the rule: composition over inheritances or derive from the classes Drawable and Transformable, but maybe Python has its own way there...

Also please, please use the code=python tag for posting! ::)

Title: Re: sfml.Sprite inheritance nightmare
Post by: shackra on January 16, 2013, 08:35:05 pm
I've no idea about the python binding, but in C++ one should not derive from the sprite class. It certainly is possible, but for OOP one should use the rule: composition over inheritances or derive from the classes Drawable and Transformable, but maybe Python has its own way there...

Also please, please use the code=python tag for posting! ::)

Composition over inhertance... got it. I'll try that jutsu and see how it works. Thanks!