Tobi Akinyemi
Tobi Akinyemi

Reputation: 19

TypeError: detectCollisions() takes exactly 8 arguments (9 given)

i keep getting this type error TypeError: detectCollisions() takes exactly 8 arguments (9 given)

def detectCollisions(x1,y1,w1,h1,x2,y2,w2,h2,):

    if (x2+w2>=x1>=x2 and y2+h2>=y1>=y2):

        return True

    elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1>=y2):

        return True

    elif (x2+w2>=x1>=x2 and y2+h2>=y1+h1>=y2):

        return True

    elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2):

        return True

    else:

        return False

def update(self,gravity,blocklist):
    if(self.velocity<0):
        self.falling=True
    blockX,blockY=0,0

    collision=False

    for block in blocklist:

        collision = self.detectCollisions(self.x, self.y, self.width, self.height, block.x, block.y, block.width, block.height)

im not sure what is wrong with it, i have already added a def for detect collision

Upvotes: 0

Views: 981

Answers (2)

Nicklas B&#246;rjesson
Nicklas B&#246;rjesson

Reputation: 433

This is a typical problem learning python. The first parameter in a member function is always the "self"-parameter. It is included in the call "behind the scenes".

That is why it says 9 arguments when you've only given 8. Here is a class declaration illustrating this, try play around with the code:

class TestClass():
    def test(self, _param1, _param2):
        print("_param1:" + str(_param1) + "_param2:" + _param2)

_instance = TestClass()
_instance.test("a", "b")
_instance.test("a", "b", "c")

The first call works, the second doesn't and you get that typical error message. This also goes the other way, you always have to add the "self" parameter to the declaration.

Upvotes: 2

MightyPork
MightyPork

Reputation: 18881

I'm pretty sure you missed self in your method declaration

def detectCollisions(x, y, ....

The first argument should be self, then your custom arguments.

When calling it, the self is passed implicitly (so it gets 9 arguments, when you used just 8 in the call)

Upvotes: 2

Related Questions