我目前正在开发一款必须在 iPhone 屏幕上绘制一系列气泡的游戏。每个气泡都有自己的花栗鼠身体,它是使用名为 PhysicsEditor 的多边形物理编辑器绘制的。有了它,我创建了一个 .plist 文件,其中包含创建花栗鼠身体所需的信息(质量、弹性、锚点等)。
下面是我用来创建具有给定坐标的气泡的代码。
- (cpBody *)drawBubblesWithX:(int)x andY:(int)y {
NSString *name = names[arc4random() % 7];
CCSprite *sprite; // Will contain the sprite object if name isn't Nil
// create physics shape
[[GCpShapeCache sharedShapeCache] anchorPointForShape:@"BubbleSlot"];
cpBody *body = [[GCpShapeCache sharedShapeCache] createBodyWithName:@"BubbleSlot" inSpace:space withData:nil];
cpBodySetPos(body, cpv(x, y));
if (name != nil) { // If a color has been selected
// create and add sprite
sprite = [CCSprite spriteWithFile:[NSString stringWithFormat:@"%@.png", name]];
[self addChild:sprite];
// set the position of the sprite to be the same as the physics body
sprite.position = CGPointMake(x, y);
// set the position of body based on the sprite object
cpBodySetUserData(body, (__bridge cpDataPointer)(sprite));
}
return body;
}
[[GCpShapeCache sharedShapeCache] 对象包含用于创建主体的 .plist 文件。在 .plist 中,只有 1 个主体,名为“BubbleSlot”,用作创建游戏中所有气泡的模板。
身体的锚点约为 (0.5, 0.5)。
My problem is that the cpBodySetPos() function does not set the position of my physics object. It is changed by the anchor point. For example, to set the position of my cpBody object in the center of the iPhone screen, I would do cpBodySetPos(body, cpv(160,240)). But for some reason, I must set the anchorpoint of the body in the .plist file with the values of (160,240) , then create the body accordingly.