0
\$\begingroup\$

I have got a json file and I am trying to load the single sprites from that file but I gives this error after a few loops and also the animation is not smooth(gets stuck) error:

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    screen.blit(image, (0, 50))
TypeError: argument 1 must be pygame.Surface, not None

This is the json file I am using:

{
  "textureAtlas": {
    "texture": "Run (32x32).png",
    "regionWidth": 32,
    "regionHeight": 32
  },
  "cycles": {
    "animation0": {
      "frames": [0,1,2,3,4,5,6,7,8,9,10,11]
    }
  }
}

The script for the animation:

import json

pygame.init()

clock=pygame.time.Clock()
c=0
def lcAnim(sfile):
    global c
    with open(sfile) as f:
        data=json.load(f)
        img=pygame.image.load(data['textureAtlas']['texture'])

        while c<len(data['cycles']['animation0']['frames']):
            img.set_clip(pygame.Rect(data['cycles']['animation0']['frames'][c-1]*32,0,32,32))  # Locate the sprite you want
            draw_me = img.subsurface(img.get_clip())  # Extract the sprite you want
            c =c+1
            print(c)
            return draw_me
    c=0

and the main script:

import pygame
import lcanim #name of the animation script
(width,height)=(300,200)
clock=pygame.time.Clock()
screen=pygame.display.set_mode((width,height))
pygame.display.flip()
running=True

while running:

    image = lcanim.lcAnim('run.sf') #name of json file
    for event in pygame.event.get():
        if event.type==pygame.QUIT:

            running=False

        screen.blit(image, (0, 50))
        #screen.blit(alimg,(-10,10))
        pygame.display.update()
        screen.fill((255,255,255))
    clock.tick(10)
pygame.quit()

Here's a video showing the problem:

https://youtu.be/8IeaqVCxvms (sorry other video sharing services were not working)

\$\endgroup\$
0

1 Answer 1

0
\$\begingroup\$

I found out the problem:

The functions in python return None by default, so when the while loop turned False it returned None thus throwing an error.

import pygame
import json
pygame.init()

c=0
def lcAnim(sfile):
    global c
    with open(sfile) as f:
        data=json.load(f)

    width=data['textureAtlas']['regionWidth']
    height = data['textureAtlas']['regionHeight']
 
    img=pygame.image.load(data['textureAtlas']['texture'])
    if c>=len(data['cycles']['animation0']['frames']):
        c=0
    if c<len(data['cycles']['animation0']['frames']):
        img.set_clip(pygame.Rect(data['cycles']['animation0']['frames'][c-1]*width,animpos*height,width,height))  # Locate the sprite you want
        draw_me = img.subsurface(img.get_clip())  # Extract the sprite you want
        c+=1
        return draw_me

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .