簡體   English   中英

斷言錯誤:必須在形狀之前(或同時)將形狀的主體添加到空間中

[英]AssertionError: The shape's body must be added to the space before (or at the same time) as the shape

我正在嘗試制作一個游戲,其中包含 pymunk 中的多邊形,為了能夠在 pymunk 中做到這一點,我決定使用許多段並將其繪制為實心多邊形。 當我執行代碼時出現錯誤

我的代碼

class StaticPoly:
    def __init__(self, space, pos, elasticity=1, collision_type=None):
        self.bodies = []
        self.shapes = []
        self.pos = pos
        for i in list(range(0, len(self.pos) - 1))+[-1]:
            print(i)
            self.bodies.append(pymunk.Body(body_type=pymunk.Body.STATIC))
            self.shapes.append(pymunk.Segment(self.bodies[i], self.pos[i], self.pos[i + 1], 1))
            self.shapes[i].elasticity = elasticity
            space.add(self.shapes[i])
            if collision_type:
                self.shapes[i].collision_type = collision_type

錯誤:

Traceback (most recent call last):
  File *path*, line 33, in <module>
    catch = StaticPoly(space, ((0, 100), (100, 101), (0, 0)))
  File *path again*, line 37, in __init__
    space.add(self.shapes[i])
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymunk/space.py", line 401, in add
    self._add_shape(o)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pymunk/space.py", line 441, in _add_shape
    assert (
AssertionError: The shape's body must be added to the space before (or at the same time) as the shape.

StaticPoly.bodies包含pymunk.Body s。 這些實體也必須添加到空間中。

Reed 錯誤信息:

形狀的主體必須在添加到形狀之前(或同時)添加到空間中。

無論是之前

space.add(self.bodies[i])
space.add(self.shapes[i])

或同時

space.add(self.bodies[i], self.shapes[i])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM