我有两个静态 (STATIC_MASS) SpaceManager 精灵。一个是另一个的孩子——我的意思是一种建立另一个,但是虽然孩子的图像出现在正确的地方,孩子似乎并不存在于花栗鼠物理引擎中,就像我一样会期望。就我而言,我有一个篮板(矩形精灵)和一个箍(圆形精灵)。因为我可能想移动篮板,所以我想将篮筐连接到篮板上,以便篮筐自动与篮板一起移动。
在这里,我们看到一个带有附加箍的旋转篮板。它在屏幕上看起来不错,但其他物体只会从篮板上反弹,但会直接穿过篮筐(从术语上看是不好的)。我的孩子精灵似乎不存在于物理引擎中?
// Add Backboard
cpShape *shapeRect = [smgr addRectAt:cpvWinCenter mass:STATIC_MASS width:200 height:10 rotation:0.0f ];// We're upgrading this
cpCCSprite * cccrsRect = [cpCCSprite spriteWithShape:shapeRect file:@"rect_200x10.png"];
[self addChild:cccrsRect];
// Spin the static backboard: http://stackoverflow.com/questions/2691589/how-do-you-make-a-sprite-rotate-in-cocos2d-while-using-spacemanager
// Make static object update moves in chipmunk
// Since Backboard is static, and since we're going to move it, it needs to know about spacemanager so its position gets updated inside chipmunk.
// Setting this would make the smgr recalculate all static shapes positions every step
// cccrsRect.integrationDt = smgr.constantDt;
// cccrsRect.spaceManager = smgr;
// Alternative method: smgr.rehashStaticEveryStep = YES;
smgr.rehashStaticEveryStep = YES;
// Spin the backboard
[cccrsRect runAction:[CCRepeatForever actionWithAction:
[CCSequence actions:
[CCRotateTo actionWithDuration:2 angle:180],
[CCRotateTo actionWithDuration:2 angle:360],
nil]
]];
// Add the hoop
cpShape *shapeHoop = [smgr addCircleAt:ccp(100,-45) mass:STATIC_MASS radius: 50 ];
cpCCSprite * cccrsHoop = [cpCCSprite spriteWithShape:shapeHoop file:@"hoop_100x100.png"];
[cccrsRect addChild:cccrsHoop];
上面的代码有点乱。箍的图像显示在板子旁边,这就是我想要的,如果我检测到碰撞,碰撞只发生在屏幕的左下角。奇怪的是,即使我检测到碰撞,我的对象实际上似乎并没有碰撞,它只是穿过它而不是从它反弹。
注:SpaceManager 是一个与 cocos2D-iphone 配合使用的工具包