I'm a little confused here. It seems like this should work, but I'm not getting the desired results. My game has two "teams" and after a round, the players' positions need to get reset to a pre-determined position (named here, startingLine). I have everything working fine with collisions, filtering user data, etc., but when I call my "movePlayerPositions" method, the bodies on the screen don't update.
Here's the relevant code:
- (void) movePlayerPositions {
// get new position array
playerPos = [[self createPlayerPositions] mutableCopy];
int i = 0;
// for each body in the world
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) {
// get the fixture list
for (b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext()) {
// if the user data matches the right team
if ([(AtlasSprite *)f->GetUserData() isEqual:@"mySprite"]) {
CGPoint p = [[playerPos objectAtIndex:i] CGPointValue];
b2Vec2 point = b->GetPosition();
NSLog(@"Location of Body before transform is %f, %f", point.x, point.y);
b->SetTransform(b2Vec2(p.x, p.y), 0);
point = b->GetPosition();
NSLog(@"Location of Body AFTER transform is %f, %f", point.x, point.y);
i++;
}
}
}
}
- (NSArray *) createPlayerPositions {
NSArray *tmp = [NSArray arrayWithObjects: [NSValue valueWithCGPoint:CGPointMake((myGame.startingLine - 1), 9.5)],
[NSValue valueWithCGPoint:CGPointMake((myGame.startingLine - 1), 7.125)],
[NSValue valueWithCGPoint:CGPointMake((myGame.startingLine - 1), 4.75)],
[NSValue valueWithCGPoint:CGPointMake((myGame.startingLine - 1), 2.375)],
[NSValue valueWithCGPoint:CGPointMake((myGame.startingLine - 1), 0)],
[NSValue valueWithCGPoint:CGPointMake((myGame.startingLine - 3), 8.3125)],
[NSValue valueWithCGPoint:CGPointMake((myGame.startingLine - 3), 5.9375)],
[NSValue valueWithCGPoint:CGPointMake((myGame.startingLine - 3), 3.5625)],
[NSValue valueWithCGPoint:CGPointMake((myGame.startingLine - 3), 1.1875)],
nil];
return tmp;
}
// to call this in my game, I'm using
[self movePlayerPositions];
Basically, I have an array of points which make up the "lineup." I need to move each body on the team to one of those points. In the console, I am showing that the positions of the body are actually getting changed by the b->SetTransform() method correctly. The problem is the Sprites and bodies on the screen are not getting updated in the World.
Am I missing an "update" or "draw" type command that will reset my view?
Thanks in advance!