objective-ccocos2d-iphonebox2dbox2d-iphone

Objective-C and Box2D multiple sprite touch detection


I have created an array of enemies and when I add an enemy I add the appropriate box2d code, however I have found that none of my enemies can be touched, I am not sure what is causing this but from what I can tell it never returns a fixture.

I have tried setting the user data but then I do not get multiple items.

This is how I add my sprite etc

for (int i = 0; i < EnemyType_MAX; i++)
{
   CCArray* enemiesOfType = [enemies objectAtIndex:i];
   int numEnemiesOfType = [enemiesOfType capacity];

   for (int j = 0; j < numEnemiesOfType; j++)
   {
     EnemyEntity* enemy = [[EnemyEntity alloc]init:_gameScene enemyType:EnemyTypeBreadman];
     [batch addChild:enemy z:0 tag:i];
     [enemiesOfType addObject:enemy];
     [allEnemies addObject:enemy];

     b2BodyDef bodyDef;
     bodyDef.type = b2_dynamicBody;
     bodyDef.position.Set(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO);
     bodyDef.userData = self;
     b2Body *body = _gameScene.world->CreateBody(&bodyDef);

     b2CircleShape circle;
     circle.m_radius = 26.0/PTM_RATIO;

     // Define the dynamic body fixture.
     b2FixtureDef fixtureDef;
     fixtureDef.shape = &circle;
     fixtureDef.density = 1.0f;
     fixtureDef.friction = 0.3f;
     body->CreateFixture(&fixtureDef);
  }
}

I then use my touch handler to try and return what item has been touched

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    //NSLog(@"ccTouchesBegan %@", (_mouseJoint!= NULL) ? @"YES" : @"FALSE" );
    if (_gameScene.mouseJoint != NULL) return;

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    float move = 0.0f, x1, y1, z1;

    [_gameScene.camera centerX:&x1 centerY:&y1 centerZ:&z1];

    b2Vec2 locationWorld = b2Vec2((location.x+x1)/PTM_RATIO, (location.y+y1)/PTM_RATIO);
    NSLog(@"ccTouchesBegan %@",NSStringFromCGPoint(location));

    b2AABB aabb;
    aabb.lowerBound.Set(-1.0f+locationWorld.x, -1.0f+locationWorld.y);
    aabb.upperBound.Set(1.0f+locationWorld.x, 1.0f+locationWorld.y);
    b2Vec2 callPoint;
    callPoint.Set (locationWorld.x,locationWorld.y);
    QueryCallback callback(callPoint);
    _gameScene.world->QueryAABB(&callback, aabb);

    b2Body* nbody = NULL;
    if (callback.m_fixture)
    {
        nbody= callback.m_fixture->GetBody();
    }

    if (nbody)
    {
        b2BodyDef bodyDef;
        b2Body* groundBody = _gameScene.world->CreateBody(&bodyDef);

        b2MouseJointDef md;
        md.bodyA = groundBody;
        md.bodyB = nbody;
        md.target = locationWorld;

        #ifdef TARGET_FLOAT32_IS_FIXED
            md.maxForce = (nbody->GetMass() < 16.0)? (1000.0f * nbody->GetMass()) : f        loat32(16000.0);
        #else
            md.maxForce = 1000.0f * nbody->GetMass();
        #endif

        _gameScene.mouseJoint = (b2MouseJoint *)_gameScene.world->CreateJoint(&md);
        nbody->SetAwake(true);
    }
}

Solution

  • In your init method, right after the if statement, is this in your code:

    if(self = [super init]){
         self.isTouchEnabled = YES;
    

    EDIT------------------

    Instead of using ccArray, you should use this:
    CCSprite *_anArray[x];
    

    When I deal with sprites I always put them in a sprite array, I declared it in the header. You also have to do the @property(nonatomic, retain) NSMutableArray *arrowArray; in the .h file and in the .m @synthesize arrowArray = _arrowArray; Then I just added all my sprites into that array. Should work.