【问题标题】:schedule update issue日程更新问题
【发布时间】:2012-05-07 09:48:19
【问题描述】:

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

【问题讨论】:

  • @xlc0212:更新方式为:-(void) update: (ccTime) dt { int32 velocityIterations = 8; int32 positionIterations = 1; world-&gt;Step(dt, velocityIterations, positionIterations); for (b2Body* b = world-&gt;GetBodyList(); b; b = b-&gt;GetNext()) { if (b == sprite) { b-&gt;ApplyForce( b2Vec2(0.0,20*b-&gt;GetMass()),b-&gt;GetWorldCenter()); } }//body with circle fixture }
  • 您应该使用格式正确的代码编辑您的问题,而不是将其作为评论中的混乱粘贴提供
  • 抱歉我的格式,我不知道编辑 cmets 的五分钟限制。
  • 您应该按照 Andrew 的建议编辑问题(而不是评论)。请在问题中添加您的 createNewSpr 代码
  • 我已经编辑了问题.... :)

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


【解决方案1】:

问题在于createNewSpr 方法...

您已将sprite 分配给一个主体...所以当您调用此方法三次时,sprite 仅引用第三个对象..

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

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

【讨论】:

  • 这使得所有的身体都对施加的重力做出反应,并且精灵的行为很奇怪。如何将其限制在某些实体上?
  • 为此,我认为在您的类 PhysicsSprite 中,您需要为要放置重力的物体保留标签或变量。或者将精灵保存在数组中。这就是您的逻辑。您是如何做到的...可能因人而异……我知道这两种方法……最好的方法是将那些受影响的对象保存在一个数组中,然后在此处使用该数组,:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 2017-04-21
  • 1970-01-01
相关资源
最近更新 更多