Ok, my original level was one with ground pieces strung together by a helper method:
Code:
- (void)createLevel {
[self createGround1];
}
-(void)createGround1{
//row 1, col 1
int num = 2;
b2Vec2 verts[] = {
b2Vec2(-468.0f / PTM_RATIO, 34.5f / PTM_RATIO),
b2Vec2(468.0f / PTM_RATIO, 36.5f / PTM_RATIO)
};
[self createGroundEdgesWithVerts:verts numVerts:num spriteFrameName:@"ground1.png"];
}
- (void)createGroundEdgesWithVerts:(b2Vec2 *)verts numVerts:(int)num spriteFrameName:(NSString *)spriteFrameName {
CCSprite *ground =
[CCSprite spriteWithSpriteFrameName:spriteFrameName];
ground.position = ccp(groundMaxX+ground.contentSize.width/2,
ground.contentSize.height/2);
[groundSpriteBatchNode addChild:ground];
b2PolygonShape groundShape;
b2FixtureDef groundFixtureDef;
groundFixtureDef.shape = &groundShape;
groundFixtureDef.density = 0.0;
for(int i = 0; i < num - 1; ++i) {
b2Vec2 offset = b2Vec2(groundMaxX/PTM_RATIO +
ground.contentSize.width/2/PTM_RATIO,
ground.contentSize.height/2/PTM_RATIO);
b2Vec2 left = verts[i] + offset;
b2Vec2 right = verts[i+1] + offset;
groundShape.SetAsEdge(left, right);
groundBody->CreateFixture(&groundFixtureDef);
}
groundMaxX += ground.contentSize.width;
}
So as I mentioned earlier, i want to add a top-level road to the scene, so I decided to simply add a duplicate set of methods like so:
Well, modify the createLevel method and add duplicate methods for the other two, like so:
Code:
- (void)createLevel {
[self createGround1];
[self createGround3TopLevel];
}
-(void)createGround3TopLevel{
//row 1, col 1
int num = 6;
b2Vec2 verts[] = {
b2Vec2(-946.1f / PTM_RATIO, -21.9f / PTM_RATIO),
b2Vec2(-479.4f / PTM_RATIO, -21.9f / PTM_RATIO),
b2Vec2(-340.8f / PTM_RATIO, -2.1f / PTM_RATIO),
b2Vec2(-127.3f / PTM_RATIO, 98.3f / PTM_RATIO),
b2Vec2(-127.3f / PTM_RATIO, -27.6f / PTM_RATIO),
b2Vec2(947.5f / PTM_RATIO, -21.9f / PTM_RATIO)
};
[self createGroundEdgesWithVertsTopLevel:verts numVerts:num spriteFrameName:@"ground3.png"];
}
- (void)createGroundEdgesWithVertsTopLevel:(b2Vec2 *)verts numVerts:(int)num spriteFrameName:(NSString *)spriteFrameName {
CCSprite *groundTopLevel = [CCSprite spriteWithSpriteFrameName:spriteFrameName];
groundTopLevel.position = ccp(groundMaxX+groundTopLevel.contentSize.width/2,groundTopLevel.contentSize.height*1.25);
[groundSpriteBatchNode addChild:groundTopLevel];
b2PolygonShape groundShape;
b2FixtureDef groundFixtureDef;
groundFixtureDef.shape = &groundShape;
groundFixtureDef.density = 0.0;
for(int i = 0; i < num - 1; ++i) {
b2Vec2 offset = b2Vec2(groundMaxX/PTM_RATIO +
groundTopLevel.contentSize.width/2/PTM_RATIO,
groundTopLevel.contentSize.height/2/PTM_RATIO);
b2Vec2 left = verts[i] + offset;
b2Vec2 right = verts[i+1] + offset;
groundShape.SetAsEdge(left, right);
groundBody->CreateFixture(&groundFixtureDef);
}
groundMaxX += groundTopLevel.contentSize.width;
}
So it is my understanding it draws the first level piece and sets groundMaxX as the rightmost edge, then strings the next piece after it at that new starting groundMaxX.
What is happening is 2-fold:
Its placing one piece after another, so it really just placed my first piece, ground1, at the start and then it sorta placed my second piece, ground3, right after it and a bit above.
What I need is to actually place my pieces like so:
Attachment:
groundpieces.png [ 10.03 KiB | Viewed 477 times ]
The other thing that is happening is that its placing the sprite up there correctly but the vertices are not correctly placed. Here is another attachment showing that. Here you can actually see the result; it placed piece1 where it should go (you can catch a glimpse of the green rectangle on the left corner of the screenshot) and then it placed the second piece body divorced from sprite. The sprite is probably where it should be but the ground is still a little off.
Attachment:
Screen Shot 2012-04-17 at 9.06.15 AM.png [ 46.56 KiB | Viewed 477 times ]
Im trying to visualize the best way to fix this. Any tips?