3

这听起来很简单。我创建了一个方法,并在 init 方法中按如下方式调用它。

[self createNewSpr:ccp(s.width * 0.25,s.height-200)];
[self createNewSpr:ccp(s.width * 0.50,s.height-200)];
[self createNewSpr:ccp(s.width * 0.75,s.height-200)];
[self scheduleUpdate];

我在我的更新方法中定义了一个 for 循环,它对精灵施加高于世界重力的重力。只有最后一个调用受新引力影响,但第一个和第二个作用于世界引力。我不确定出了什么问题,但我怀疑它是 scheduleUpdate。请帮忙。

编辑:更新方法:

-(void) update: (ccTime) dt 
{
 int32 velocityIterations = 8;
 int32 positionIterations = 1; 
 world->Step(dt, velocityIterations, positionIterations);
 for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
   {
     if (b == sprite)
        {
         b->ApplyForce( b2Vec2(0.0,20*b->GetMass()),b->GetWorldCenter());
        }
   }
}

the createNewSpr:
-(void) createNewSpr:(CGPoint)pos {
//CGSize s = [CCDirector sharedDirector].winSize;
b2Vec2 startPos = [self toMeters:pos];
CGFloat linkHeight = 0.24;
CGFloat linkWidth = 0.1;

b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = startPos;
b2FixtureDef fixtureDef;
fixtureDef.density = 0.1;
b2PolygonShape polygonShape;
polygonShape.SetAsBox(linkWidth,linkHeight);
fixtureDef.shape = &polygonShape;


//first
b2Body* link = world->CreateBody( &bodyDef );
link->CreateFixture( &fixtureDef );

PhysicsSprite* segmentSprite = [PhysicsSprite spriteWithFile:@"sg.png"];
[self addChild:segmentSprite];
[segmentSprite setPhysicsBody:link];


b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.localAnchorA.Set( 0,  linkHeight);
revoluteJointDef.localAnchorB.Set( 0, -linkHeight);


for (int i = 0; i < 10; i++) {

    b2Body* newLink = world->CreateBody( &bodyDef );
    newLink->CreateFixture( &fixtureDef );
    PhysicsSprite* segmentSprite = [PhysicsSprite spriteWithFile:@"sg.png"];
    [self addChild:segmentSprite];
    [segmentSprite setPhysicsBody:link];

    revoluteJointDef.bodyA = link;
    revoluteJointDef.bodyB = newLink;
    world->CreateJoint( &revoluteJointDef );

    link = newLink;//next iteration
}

PhysicsSprite* circleBodySprite = [PhysicsSprite spriteWithFile:@"cb.png"];
[self addChild:circleBodySprite z:1];

b2CircleShape circleShape;
circleShape.m_radius = circleBodySprite.contentSize.width/2 / PTM_RATIO;
fixtureDef.shape = &circleShape;
b2Body* chainBase =world->CreateBody( &bodyDef );
chainBase->CreateFixture( &fixtureDef );
[circleBodySprite setPhysicsBody:chainBase];
sprite = chainBase;

revoluteJointDef.bodyA = link;
revoluteJointDef.bodyB = chainBase;


revoluteJointDef.localAnchorA.Set(0,linkWidth);

revoluteJointDef.localAnchorB.Set(0,linkWidth);
world->CreateJoint( &revoluteJointDef );
}
4

1 回答 1

1

问题在于createNewSpr方法...

你已经分配sprite给一个身体......所以当你三次调用这个方法时,sprite只指第三个对象......

当您在update方法中进行比较时,它只是将重力放在第三个对象上......

希望这可以帮助.. :)

于 2012-05-07T16:16:59.313 回答