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



我创建了一个敌人数组,当我添加敌人时,我添加了适当的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);
    }
}

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

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

编辑 ------------------

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

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

最新更新