2013-03-07 92 views
1

我有一个球发生器,它“生成”并将球(圆圈)添加到模拟中。pymunk space.remove方法的错误

当球在列表s_boxes中击中一个静态多边形时将被移除。
这是由碰撞处理程序ball_wall_collision完成的。

的错误:
以下弹出窗口做什么它的名字一样,它弹出式
**POP UP WINDOW**

我的代码:
球发电机

class BallGenerator: 
    def __init__(self, min_y, max_y, x): 
     self.min = min_y 
     self.max = max_y 
     self.x = x 

     self.counter = 0 

    def bowl(self, balls): 
     global ball_bowled 
     y = random.randint(self.min, self.max) 
     pos = to_pymunk((self.x,y)) 
     r = 10 
     m = 15 
     i = pm.moment_for_circle(m, 0, r) 
     b = pm.Body(m,i) 
     b.position = pos 
     f_x = random.randint(-600000,-400000) 
     b.apply_force((f_x,0.0),(0,0)) 

     ball = pm.Circle(b, r) 
     ball.elasticity = 0.75 
     ball.friction = 0.95 
     balls.append(ball) 
     space.add(ball,b) 
     print 'bowled' 

     ball_bowled += 1 

    def handle(self, balls): 
     if self.counter == FPS: 
      self.bowl(balls) 
      self.counter = 0 
     self.counter += 1 

碰撞处理器

def ball_wall_collision(space, arb, balls, s_boxes): 
    shapes = arb.shapes 
    boxes = [box[0] for box in s_boxes] # Get walls 
    ball = None 
    wall = None 
    for ba in balls: 
     if ba in shapes: 
      ball = ba 
      break 
    for box in boxes: 
     if box in shapes: 
      wall = box 
      break 
    if wall and ball: 
     print 'removing' 
     space.remove(ball, ball.body) # Where the runtime problem happens 
     balls.remove(ball) 
     print 'removed' 

     return False 
    else: 
     return True 
space.add_collision_handler(0,0,begin=ball_wall_collision, 
          balls=balls,s_boxes=s_boxes) # Other args to function 

我在做什么错在碰撞处理?

  • 我在致电space.remove的电话中缺少什么吗?
  • 功能不工作,因为我想它? 或者是错误的其他地方(我不认为这是)...
+0

并且*错误*然后是什么?请包括完整的追溯。 – 2013-03-07 10:28:38

回答

2

它看起来像问题是,你尝试在模拟工序,从在冲突处理的空间删除对象。

相反,你可以尝试手动收集所有的球到一个列表中,然后调用步骤之后删除或排队删除与后续步骤回调是这样的:

space.add_post_step_callback(space.remove, ball) 
space.add_post_step_callback(space.remove, ball.body) 

(未经测试的代码)

我应该尝试,并在API文档中使这更明显..我不知道是否自动调度删除直到步骤结束,或较少侵入选项是一个好主意,触发一个断言在Python中,所以你不要得到c + +错误。

+0

好的,我做了这些:我做了一个列表并添加了球(将被删除)到列表中,并且在该步骤之后,我从列表中删除了'ball'和'ball.body',并且'space' 。 Thx的帮助.. – pradyunsg 2013-03-08 10:50:50

+0

检查我的[新问题](http://stackoverflow.com/q/15459943/1931274).. – pradyunsg 2013-03-17 11:05:18

+1

现在有一个代码,所以你可以打电话删除任何时间,而不必做任何手动的东西,如post_step_callback或一个单独的列表。 (所以这将被包含在下一个版本的pymunk中) – viblo 2013-03-18 16:56:11