2011-10-26 49 views
0

我有一個非常惱人的問題。我創建了一系列敵人,當我添加一個敵人時,我添加了合適的box2d代碼,但是我發現我的敵人沒有一個可以被觸摸,我不確定是什麼造成了這一點,但從我可以告訴它永遠不會返回一個燈具,任何人都可以幫忙。Objectice-C和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); 
    } 

} 
+0

固定格式建議 –

回答

1

在你的init方法,if語句之後,這是在你的代碼:

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

編輯------------------

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

當我對付精靈,我總是把它們放在一個精靈陣列,我宣佈它在頭。你還必須在.h文件中和.m中執行@property(nonatomic, retain) NSMutableArray *arrowArray;然後我只是將所有我的精靈加入到該數組中。應該管用。

+0

是的,它不是那種觸摸沒有啓用,因爲它更多,你只能觸摸一個對象,我認爲我接近解決問題,並會盡快發佈解決方案 –

+0

嗯,我想我知道什麼會解決你的問題知道我已經閱讀了你的代碼更多...不知道,但我會編輯我的答案,並將其添加到它的末尾 – Gabe

+0

我改變了我的方法,我現在替換標準的移動精靈與box2d身體接觸,似乎很無縫,做我所需要的。謝謝你的幫助 –