安排更新问题

时间:2012-05-07 09:48:19

标签: objective-c cocos2d-iphone game-physics schedule

这听起来非常简单。我已经创建了一个方法,我在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 );
}

1 个答案:

答案 0 :(得分:1)

问题出在createNewSpr方法......

您已将sprite分配给正文...因此,当您将此方法调用三次时,sprite仅指第三个对象..

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

希望这会有所帮助.. :)

相关问题