Search code examples
objective-ccocos2d-iphonegame-physicsschedule

schedule update issue


This might sound pretty straightforward. I've created a method and I've called it as below in the init method.

[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];

I've defined a for loop in my update method that imposes a gravity higher than that of the world on the sprites. Only the last call is affected by the new gravity but the first and second act on the world gravity. I am not sure what is wrong but I suspect it to be the scheduleUpdate. Please Help.

Edit: Update Method :

-(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 );
}

Solution

  • The problem is with createNewSpr method...

    You have assigned sprite to a body... So When you call this method three times, sprite refers to 3rd object only..

    When you compare in update method, it just puts gravity on 3rd object...

    Hope this helps.. :)