0

I am trying to assign a separate function to each instance that is created from bgImages, so that when I call instance.collisiontext, it calls that function. I feel like I am doing something wrong. If you look down in the build, you can see an example of where I create an instance and assign a function to collidetext.

The whole point of the collisiontext method is to call the function assigned to collidetext (which is going to be a different function for each instance).

What am I doing wrong here?

Also, when assigning a function to the instance slum, do I need to keep the '()' after 'slumnotice'?

class BgImages(ButtonBehavior, Image):
    def __init__(self, **kwargs):
        super(Npcs, self).__init__(**kwargs)
        self.collidetext=''

    def collisiontext(self,**kwargs):
        global collidetext
        return collidetext()

class MainCharacter(Image):

    def __init__(self, **kwargs):
        super(MainCharacter, self).__init__(**kwargs)
        self._keyboard = Window.request_keyboard(None, self)
        if not self._keyboard:
            return
        self._keyboard.bind(on_key_down=self.on_keyboard_down)
        self._keyboard.bind(on_key_up=self.on_keyboard_up)

    elif keycode[1] == 'up':
            for i in listofwidgets:
                if i.collide_point(self.x,self.top):
                    self.y -=1
                    i.collisiontext()

class gameApp(App):
    def build(self):
        slum=BgImages(source='slum.png', collidetext=slumnotice())
        listofwidgets=[]
        listofwidgets.append(slum)
0

1 Answer 1

1

What am I doing wrong here?

Quite a bit. It'll be easier if I just correct it and you figure out what I did after:

class BgImages(ButtonBehavior, Image):
    def __init__(self, **kwargs):
        self.collidetext = kwargs['collidetext']
        del kwargs['collidetext']
        super(BgImages, self).__init__(**kwargs)

    def collisiontext(self):
        return self.collidetext()

Also, when assigning a function to the instance slum, do I need to keep the '()' after 'slumnotice'?

That would depend on whether you want to use slumnotice itself or the return value of slumnotice().

Sign up to request clarification or add additional context in comments.

12 Comments

I'm not exactly sure what you did with kwargs['collidetext']
It's a dictionary. I accessed and mutated it.
Why? Anyway, that gives me error, 'KeyError: 'collidetext''
Can I just have the 'collidetext' be a placeholder for whatever function i assign to it (without having to delete things)?
Are you actually passing a "collidetext" argument to the constructor?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.