Objective-C和Box2D多重精灵触摸检测

时间:2011-10-26 09:19:54

标签: objective-c cocos2d-iphone box2d box2d-iphone

我有一个非常讨厌的问题。我已经创建了一个敌人阵列,当我添加一个敌人时,我添加了相应的box2d代码,但是我发现我的敌人都没有被触及,我不知道是什么造成了这个但是从我可以告诉它永远不会返回一个固定装置,任何人都可以帮忙。

我已尝试设置用户数据,但之后我没有获得多个项目。如果有人需要来源,请告诉我

提前致谢

这就是我添加精灵等的方式

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);

  }
}

然后我使用我的触摸处理程序尝试返回已触摸的项目

- (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);
    }

}

1 个答案:

答案 0 :(得分:1)

在你的init方法中,就在if语句之后,这是在你的代码中:

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

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

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

当我处理精灵时,我总是把它们放在一个精灵数组中,我在标题中声明它。您还必须在.h文件和.m @property(nonatomic, retain) NSMutableArray *arrowArray;中执行@synthesize arrowArray = _arrowArray; 然后我只是将所有精灵添加到该数组中。应该工作。