【问题标题】:detect collision with sprite in sprite sheet检测与精灵表中精灵的碰撞
【发布时间】:2013-05-13 15:17:51
【问题描述】:

我有一个精灵表,其中有四个图像(bat1,bat2,bat3.bat4),在图像中有一个拿着球棒的人,当所有图像在动画中组合时,它看起来就像在打棒球。 下面是我用来添加精灵表的代码。

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"baseball.plist"];

spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"baseball.plist"];
[self addChild:spriteSheet];

background = [CCSprite spriteWithSpriteFrameName:@"bat4.png"];
background.position = ccp(220, 185);
background.tag = 10;
[self addChild:background];

for(int i = 1; i < 5; i++) {
        [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]    spriteFrameByName:[NSString stringWithFormat:@"bat%d.png", i]]];
}

CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:5.0f];

 self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim]];

[background runAction:_walkAction];

我正在检测球与球棒的碰撞。通过下面的代码

for (CCSprite *monster in _monsters) {
        if (CGRectIntersectsRect(background.boundingBox, monster.boundingBox)) {
            if (((background.position.x -5 > monster.position.x + (monster.contentSize.width/2)) && background.position.y > monster.position.y)) {
                isCollision = 1;
                [monstersToDelete addObject:monster];
            }
           // [monstersToDelete addObject:monster];
            //[self addFishToBoat];
        }
    }

这里发生的情况是检测到碰撞,但它总是检测到与 bat4 的 rect 的碰撞。因为 bat 正在移动并且所有 bat 的 cgrect 都不同,所以即使 bas 离 bat1 很远,它也会检测到碰撞,因为球的矩形会与 bat4 的矩形相交。

但我希望只有当球与不同的球棒碰撞时才会检测到碰撞,我的意思是当球用 bat1、bat2、bat3、bat4 击中时,只有它会检测到碰撞而不是总是用 bat4 检测

【问题讨论】:

  • 缩小bat4的bounding box试试,如果所有bat的bounding box都一样就可以了。
  • 如何减少?还有一件事蝙蝠处于不同的角度假设当我轻触屏幕时 bat(bat1) 处于 90 度所以它的矩形会不同但是当我开始触摸然后通过 sprite sheet 另一个蝙蝠 (bat2) 来了bat2 的角度为 60 度,因此它的矩形会有所不同,以此类推,直到 0 度为止。所以所有的rect会有很多差异所以我想检测碰撞会纠正rect
  • CCSprite *bat1Sprite = [CCSprite spriteWithFile:@"bat.jpg" rect:CGRectMake(0, 0, 30, 10)];无论角度是什么,边界框始终保持不变。
  • 但我希望它采用与蝙蝠碰撞的蝙蝠边界框
  • 所有蝙蝠的图像都一样吗?我对吗?如果它是真的那么你需要改变边界框来获得碰撞,否则它们会在同一个位置重叠,你不能得到特定的碰撞

标签: iphone objective-c ios6 cocos2d-iphone


【解决方案1】:

这是 raywenderlich 在sprites collision detection 上的精彩教程。

或者

你也可以试试这个教程Sprite Collision

希望这些对您有所帮助。

【讨论】:

    【解决方案2】:

    你试试这个代码它可能对你有帮助......

    _contactListener = new MyContactListener();
    _world->SetContactListener(_contactListener);
    
    // Preload effect
    [[SimpleAudioEngine sharedEngine] preloadEffect:@"hahaha.caf"];
    
    
    
    std::vector<b2Body *>toDestroy; 
    std::vector<MyContact>::iterator pos;
    for(pos = _contactListener->_contacts.begin(); 
        pos != _contactListener->_contacts.end(); ++pos) {
        MyContact contact = *pos;
    
        b2Body *bodyA = contact.fixtureA->GetBody();
        b2Body *bodyB = contact.fixtureB->GetBody();
        if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
            CCSprite *spriteA = (CCSprite *) bodyA->GetUserData();
            CCSprite *spriteB = (CCSprite *) bodyB->GetUserData();
    
            if (spriteA.tag == 1 && spriteB.tag == 2) {
                toDestroy.push_back(bodyA);
            } else if (spriteA.tag == 2 && spriteB.tag == 1) {
                toDestroy.push_back(bodyB);
            } 
        }        
    }
    
    std::vector<b2Body *>::iterator pos2;
    for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
        b2Body *body = *pos2;     
        if (body->GetUserData() != NULL) {
            CCSprite *sprite = (CCSprite *) body->GetUserData();
            [_spriteSheet removeChild:sprite cleanup:YES];
        }
        _world->DestroyBody(body);
    }
    
    if (toDestroy.size() > 0) {
        [[SimpleAudioEngine sharedEngine] playEffect:@"hahaha.caf"];   
    }
    

    有关此代码的更多详细信息,请访问

    http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 2019-11-05
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多