3

Hello so I am currently programming a little 2D game. I already have movement etc. figured out. Now I want to add a simple animation by using something I found on here:

def anim_right():
    global counter_right
    imagesright = ["tesbold_right1_x1000.png","tesbold_right2_x1000.png"]
    Tesbold.image = imagesright[counter_right]
    counter_right = (counter_right + 1) % len(imagesright)
    print("anim_right ausgeführt")

I have this for every cardinal direction. This also works fine with the only exception that it is seizure enducing because I call up this function in my update() function. Essentially changing the image every single frame.

I was think about adding a clock.schedule_unique but this seems to not work.

If I add it in the animation function itself like this:

def anim_right():
    global counter_right
    imagesright = ["tesbold_right1_x1000.png","tesbold_right2_x1000.png"]
    Tesbold.image = imagesright[counter_right]
    counter_right = (counter_right + 1) % len(imagesright)
    print("anim_right ausgeführt")
    clock.schedule_unique(anim_right(), 0.5)

I get following error code:

RecursionError: maximum recursion depth exceeded

If I try it in my function that is calling up my anim_right() function like this:

def update():               #Updates per Frame
    global walking
    if walking == True:
        if Tesbold.direction == 0:
            clock.schedule_unique(anim_right(), 0.5)

I get the following Error Code:

TypeError: cannot create weak reference to 'NoneType' object

I have searched both Error Codes and have found nothing usefull for my Case.

Ignitris
  • 55
  • 5
  • [Pygame Zero](https://pygame-zero.readthedocs.io/en/stable/) is not [Pygame](https://www.pygame.org/news). Don't use both tags. This would lead to confusions. – Rabbid76 Apr 16 '21 at 15:33
  • @Rabbid76 oh sorry. I honestly believed to this point both were almost the same/similar enough. Like dark and light blue. Or maybe I was just stupid. – Ignitris Apr 16 '21 at 15:35
  • To which line of the snippet does the TypeError point? – Lyndon Gingerich Apr 16 '21 at 15:57
  • @themadpsychologist the second one. – Ignitris Apr 16 '21 at 16:53
  • That's odd; I would figure it would point to the specific problem within `direction_change()` where the problem is taking place. – Lyndon Gingerich Apr 16 '21 at 17:06
  • @themadpsychologist funnily enough the direction_change() is working as intented. It is just `If Actor = looking right, give him the sprite that looks to the right`. For the detection if the frames should be animated I have it a bit unnecessarily complex by asking the same thing again. Basically `If boolean of pressing walking keys = True and looking right: Start the function that is cycling the Frames of the right looking sprites making them look like they are walking.` – Ignitris Apr 16 '21 at 17:11
  • @Ignitris Wait. You were saying that the error message points to the line `direction_change()`, correct? – Lyndon Gingerich Apr 16 '21 at 17:35
  • 1
    @themadpsychologist I said the error appears once I put `clock.schedule_unique()` in either anim_right() or the update(). I copied the code blocks directly out of my source code and forgot to delete the `direction_change()`. Sorry for the confusion. – Ignitris Apr 16 '21 at 20:03

2 Answers2

2

You get the error "RecursionError: maximum recursion depth exceeded", because anim_right() is a function call statement.
The line clock.schedule_unique(anim_right(), 0.5) actually calls the function anim_right and pass the return value to clock.schedule_unique. This leads to an endless recursion.

You have to pass the function itself (anim_right instead of anim_right()):

clock.schedule_unique(anim_right(), 0.5)

clock.schedule_unique(anim_right, 0.5)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • This is for the first variant right? With the clock.schedule_unique directly in anim_right? Edit: I tried it out in anim_right directly and unfortunately it does not slow down the animation cycle. – Ignitris Apr 16 '21 at 15:43
  • and If I try to use if in the second variant it appears as if it does not animate at all. – Ignitris Apr 16 '21 at 15:46
  • @Ignitris I just pointed out what is obviously wrong. I can't debug your program and I can't fix your game logic, because I can just see the small code snippets in the question. – Rabbid76 Apr 16 '21 at 15:54
  • I mean I could send you the whole code which is around 120 lines long at this point if you want – Ignitris Apr 16 '21 at 16:54
0

Your code probably depends on creating storing information from an object that does not always exist. Whatever you're creating a weak reference to, either figure out how to make sure that it's always defined or code your program in such a way that it works even when the object is not defined.

Lyndon Gingerich
  • 604
  • 7
  • 17