0
我可以添加b2body或b2fixture到NSmutableArray或NSArray。?
我一直在尋找一些解決方案,其中之一表示應該先插入NSValue,然後再將值添加到nsmutable數組中。所以我寫這在我的代碼:b2body和b2fixture到數組
// Create ball body
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(0/PTM_RATIO, 300/PTM_RATIO);
ballBodyDef.userData = [ballSpriteArray objectAtIndex:a];
_ballBody = _world->CreateBody(&ballBodyDef);
// Create circle shape on ball
b2CircleShape circle;
circle.m_radius = 30.0/PTM_RATIO;
// Create shape definition and add to body
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 0.5f;
ballShapeDef.friction = 0.5f;
ballShapeDef.restitution = 0.6f;
_ballFixture = _ballBody->CreateFixture(&ballShapeDef);
fixValue = [NSValue valueWithBytes:&_ballFixture objCType:@encode(struct b2Fixture)];
[ballFixtureArray addObject:fixValue];
//memberi impulse awal terhadap bola
b2Vec2 force = b2Vec2(5, 10);
_ballBody->ApplyLinearImpulse(force, ballBodyDef.position);
bodyValue = [NSValue valueWithBytes:&_ballBody objCType:@encode(struct b2Body)];
[ballBodyArray addObject:bodyValue];
得到關於NSMutableArray的我在CCtouchBegan方法做了這樣每b2Fixture和b2body:
for(NSValue *val in ballFixtureArray)
{
b2Fixture *ballFix;
[val getValue:&ballFix];
if (ballFix->TestPoint(locationWorld)) {
//if([ballFixtureArray objectAtIndex:0]->TestPoint(locationWorld)){
for (NSValue *bodyVal in ballBodyArray){
b2MouseJointDef md;
md.bodyA = _groundBody;
b2Body *ballBody;
[bodyVal getValue:&ballBody];
md.bodyB = ballBody;
md.target = locationWorld;
md.collideConnected = true;
md.maxForce = 1000.0f * ballBody->GetMass();
_mouseJoint = (b2MouseJoint *)_world->CreateJoint(&md);
ballBody->SetAwake(true);
}
}
}
,但它不工作,在另一方面,應用程序只是當我觸摸屏幕上的每個位置時崩潰..
有沒有人可以幫助我..?
日Thnx每一個之前提前..
問候