从图层中删除精灵而不是用它删除矩形(Cocos2d)?

发布于 2024-11-09 15:43:22 字数 434 浏览 0 评论 0原文

我正在 Cocos2d 中制作游戏。到目前为止,一切都还好。我使用了 Ray Wenderlich 的 使碰撞检测发挥作用的教程。它有效,但每当“敌人”在子弹被删除的地方产生(因为被删除的子弹击中目标,因此被删除),敌人也会被自动删除。我认为这是因为它没有删除为精灵声明的矩形。请注意,即使子弹被删除,它也可以穿过多个敌人。任何帮助表示赞赏。谢谢!

编辑: 我发现问题出在哪里了。我在schedule:@selector方法中设置了拍摄方法,没有设定间隔。这意味着它能够以 60 fps 的速度快速发射子弹。所以我一点击就得到了两颗子弹。他们靠得很近,我花了好一会儿才注意到。我不会再犯这样的错误了!!!

I am making a game in Cocos2d. Everything is okay, so far. I used Ray Wenderlich's tutorial to get collision detection to work. It works, but whenever an 'enemy' spawns where a bullet was deleted (because the bullet that was deleted hit a target, therefore, was deleted), the enemy is automatically deleted, too. I think it's because it doesn't remove the rect that was declared for the sprite. Note, it also can go through more than one enemy, even though the bullet is deleted. Any help is appreciated. Thanks!

EDIT:
I found out what the problem was. I had the shoot method set in a schedule:@selector method, with no set interval. That meant that it would fire bullets 60fps fast. So I was getting TWO bullets with ONE click. They were so close together, that it took me a while to notice it. I won't make that mistake again!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞美少年 2024-11-16 15:43:22

您使用以下代码吗? (来自 如何制作一个简单的iPhone 游戏 Cocos2D 教程)

- (void)update:(ccTime)dt {

  NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
  for (CCSprite *projectile in _projectiles) {
    CGRect projectileRect = CGRectMake(
      projectile.position.x - (projectile.contentSize.width/2), 
      projectile.position.y - (projectile.contentSize.height/2), 
      projectile.contentSize.width, 
      projectile.contentSize.height);

    NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *target in _targets) {
      CGRect targetRect = CGRectMake(
        target.position.x - (target.contentSize.width/2), 
        target.position.y - (target.contentSize.height/2), 
        target.contentSize.width, 
        target.contentSize.height);

      if (CGRectIntersectsRect(projectileRect, targetRect)) {
        [targetsToDelete addObject:target];             
      }                     
    }

    for (CCSprite *target in targetsToDelete) {
      [_targets removeObject:target];
      [self removeChild:target cleanup:YES];                                    
    }

    if (targetsToDelete.count > 0) {
      [projectilesToDelete addObject:projectile];
    }
    [targetsToDelete release];
  }

  for (CCSprite *projectile in projectilesToDelete) {
    [_projectiles removeObject:projectile];
    [self removeChild:projectile cleanup:YES];
  }
  [projectilesToDelete release];
}

每当删除子弹的地方出现“敌人”时,敌人也会自动删除。

听起来子弹已从图层中删除,但并未从 _projectiles 数组中删除。

    [_projectiles removeObject:projectile];

您确定这段代码有效吗?

Are you using the following code? (from How To Make A Simple iPhone Game with Cocos2D Tutorial)

- (void)update:(ccTime)dt {

  NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
  for (CCSprite *projectile in _projectiles) {
    CGRect projectileRect = CGRectMake(
      projectile.position.x - (projectile.contentSize.width/2), 
      projectile.position.y - (projectile.contentSize.height/2), 
      projectile.contentSize.width, 
      projectile.contentSize.height);

    NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *target in _targets) {
      CGRect targetRect = CGRectMake(
        target.position.x - (target.contentSize.width/2), 
        target.position.y - (target.contentSize.height/2), 
        target.contentSize.width, 
        target.contentSize.height);

      if (CGRectIntersectsRect(projectileRect, targetRect)) {
        [targetsToDelete addObject:target];             
      }                     
    }

    for (CCSprite *target in targetsToDelete) {
      [_targets removeObject:target];
      [self removeChild:target cleanup:YES];                                    
    }

    if (targetsToDelete.count > 0) {
      [projectilesToDelete addObject:projectile];
    }
    [targetsToDelete release];
  }

  for (CCSprite *projectile in projectilesToDelete) {
    [_projectiles removeObject:projectile];
    [self removeChild:projectile cleanup:YES];
  }
  [projectilesToDelete release];
}

whenever an 'enemy' spawns where a bullet was deleted, the enemy is automatically deleted, too.

It sounds that the bullet is removed from the layer, but it is not removed from _projectiles array.

    [_projectiles removeObject:projectile];

Are you sure that this code works?

游魂 2024-11-16 15:43:22

矩形不是与项目符号分开的实体。矩形是与项目符号关联的属性。一旦您的“项目符号被删除”,该矩形将不再有效。

您应该查看的是碰撞检查代码。

您可能想用这样的条件包围您的子弹碰撞检查代码:

if(bullet exists)
{
    check for collision
}

由于您还没有发布代码,所以我只能在此处发布伪代码。也许如果您发布碰撞检查代码,我可以向您展示更详细的信息。

Rect is not a seperate entity from your bullet. Rect is the property associated with the bullet. As soon as your "bullet is deleted" the rect will no longer be valid.

What you should be looking at is your collision checking code.

You probably want to surround your bullet collision check code with a condition like:

if(bullet exists)
{
    check for collision
}

Since you haven't posted code I could only post pseudo code here. Maybe if you post your collision checking code I could show you in more detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文