【问题标题】:Sprite's bounding box staysSprite 的边界框保持不变
【发布时间】:2013-06-12 19:37:50
【问题描述】:

我在 cocos2d 中使用边界框来检测球和另一个精灵之间的碰撞,但是当我使用[self removeChild:Row4Tile1 cleanup: YES]; 时,CGRect 停留在那里,而精灵被移除。有没有办法删除边界框?我将每个初始化为

Row4Tile1 = [CCSprite spriteWithFile:@"Row4.png" rect:(CGRectMake(Row4Tile1.position.x, Row4Tile1.position.y, 26, 73))];
Row4Tile1.color = ccc3(0, 168, 255);
Row4Tile1.position = ccp(301, 443);
Row4Tile1.rotation = 15.0;
[row4 addObject:Row4Tile1];
[self addChild:Row4Tile1];

然后我在if (CGRectIntersectsRect(ball.boundingBox, Row4Tile1.boundingBox)) 之后删除它们 但是,我确实对多个精灵使用相同的图像,所以我不确定这是否重要。下面是tick方法的代码:

 - (void)tick:(ccTime) dt {    
 _world->Step(dt, 10, 10);
    for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
    if (b->GetUserData() != NULL) {
        ballData = (__bridge CCSprite *)b->GetUserData();
        ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
                                b->GetPosition().y * PTM_RATIO);
        ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
         NSLog(@"ball position is %@", NSStringFromCGPoint(ballData.position));
        for(CCSprite *sprite in row4){
            if (CGRectIntersectsRect(ball.boundingBox, Row4Tile1.boundingBox)) {
                [self removeBall];
                [row4 removeObject:Row4Tile1];
                [self removeChild:Row4Tile1 cleanup: YES];
            }
            if (CGRectIntersectsRect(ball.boundingBox, Row4Tile2.boundingBox)) {
                [self removeBall];
                [row4 removeObject:Row4Tile2];
                [self removeChild:Row4Tile2 cleanup: YES];
            }
            if (CGRectIntersectsRect(ball.boundingBox, Row4Tile3.boundingBox)) {
                [self removeBall];
                [row4 removeObject:Row4Tile3];
                [self removeChild:Row4Tile3 cleanup: YES];
            }
   }

remove ball 方法不存在,但效果很好,所以我没有添加它。只是没有被删除的行边界框

【问题讨论】:

  • 你使用的是box2d调试模式显示吗?你的精灵有在 box2d 世界中定义的身体吗?

标签: cocos2d-iphone sprite removechild


【解决方案1】:

« 谈论未删除的内存?那么这是另一个similar problem.

您将 Row4Tile1 添加到 row4 数组和自身。所以精灵的dealloc只有在它的retain count为0时才会被调用。所以从数组中删除它并自己调用它的dealloc。查看上面的线程以获得更清晰的信息。

« 如果只是使用颜色矩形绘制,则可能是 box2d 调试形状或 CC_SPRITE_DEBUG_DRAW 为 1。如果是 box2d 形状,则需要在移除精灵之前销毁 b2body。

【讨论】:

  • 启用 ARC 是否重要?而且只有它碰撞的物体有一个b2body,这只是一个我用文件添加的普通精灵。
猜你喜欢
  • 2012-07-12
  • 1970-01-01
  • 2017-09-07
  • 2012-11-15
  • 1970-01-01
  • 2014-02-03
  • 2012-10-28
  • 2018-08-20
  • 1970-01-01
相关资源
最近更新 更多