我正在动态地从标签纹理创建一堆精灵,并且工作正常。我有一些方法可以用来点击一个精灵然后让它“摇晃”然后我可以将它们拖到屏幕上。我的问题是我需要为精灵添加一个花栗鼠形状,这样它们就不会重叠,它们会互相推开。一旦我将形状添加到精灵,然后我选择/拖动精灵的方法不再有效。我有点失落。
这是用于创建精灵/形状的方法。
-(void) addNewSpriteX: (float)x y:(float)y wordIndex:(int)i
{
CCLabelTTF *label;
if ( i == 0 )
{
label = [CCLabelTTF labelWithString:[[words objectAtIndex:i] uppercaseString] fontName:[self getRandomFont] fontSize:kFontSize];
}
else
{
label = [CCLabelTTF labelWithString:[[words objectAtIndex:i] uppercaseString] fontName:[self getRandomFont] fontSize:[self getFontSize:[[counts objectAtIndex:0] intValue] andCurrentCount:[[counts objectAtIndex:i] intValue]]];
}
wordSprite = [CCSprite spriteWithTexture:[label texture]];
// ask director the the window size
wordSprite.color = [self getRandomColor];
wordSprite.position = ccp( x, y );
// FROM HERE TO THE NEXT COMMENT IS WHERE I AM ADDING THE SHAPE AND BODY (DOING IT WRONG?)
CGPoint p1 = CGPointMake(-wordSprite.contentSize.width/2, -wordSprite.contentSize.height/2);
CGPoint p2 = CGPointMake(-wordSprite.contentSize.width/2, +wordSprite.contentSize.height/2);
CGPoint p3 = CGPointMake(+wordSprite.contentSize.width/2, +wordSprite.contentSize.height/2);
CGPoint p4 = CGPointMake(+wordSprite.contentSize.width/2, -wordSprite.contentSize.height/2);
int num = 4;
CGPoint verts[] = {p1, p2, p3, p4};
cpBody *body = cpBodyNew(50.0, INFINITY);
body->p = wordSprite.position;
cpSpaceAddBody(space, body);
cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero);
shape->e = 0.5f; shape->u = 0.5f;
shape->data = wordSprite;
cpSpaceAddShape(space, shape);
// END SHAPE CREATION
[movableSprites addObject:wordSprite];
[self addChild:wordSprite];
}
这些是我用来选择和移动精灵的方法(在添加形状/身体后不再起作用)。
- (void)selectSpriteForTouch:(CGPoint)touchLocation {
CCSprite * newSprite = nil;
for (CCSprite *sprite in movableSprites) {
if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {
newSprite = sprite;
break;
}
}
if (newSprite != wordSprite) {
[wordSprite stopAllActions];
[wordSprite runAction:[CCRotateTo actionWithDuration:0.1 angle:0]];
CCRotateTo * rotLeft = [CCRotateBy actionWithDuration:0.1 angle:-4.0];
CCRotateTo * rotCenter = [CCRotateBy actionWithDuration:0.1 angle:0.0];
CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.1 angle:4.0];
CCSequence * rotSeq = [CCSequence actions:rotLeft, rotCenter, rotRight, rotCenter, nil];
[newSprite runAction:[CCRepeatForever actionWithAction:rotSeq]];
wordSprite = newSprite;
}
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self selectSpriteForTouch:touchLocation];
return TRUE;
}
- (CGPoint)boundLayerPos:(CGPoint)newPos {
CGPoint retval = newPos;
retval.x = MIN(retval.x, 0);
retval.x = MAX(retval.x, -background.contentSize.width+size.width);
retval.y = self.position.y;
return retval;
}
- (void)panForTranslation:(CGPoint)translation {
if (wordSprite) {
CGPoint newPos = ccpAdd(wordSprite.position, translation);
wordSprite.position = newPos;
} else {
CGPoint newPos = ccpAdd(self.position, translation);
self.position = [self boundLayerPos:newPos];
}
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
[self panForTranslation:translation];
}
答案 0 :(得分:0)
如果你正在使用chipmunk + TouchMove,你应该使用cpMouse。 查看www.cocos2d-iphone.org/forum/topic/4936
Github上有两个例子, 1)Kerchink(与新版本一起使用)被删除:(, 2)抓取或抓取(与旧版本一起使用).http://code.google.com/p/grabbed/
cpMouse只是看不见的身体,它与touchBegan连接,对象在toucharea上,使用PinJoints,SlideJoints等。当你touchMoved时,与cpMouse连接的对象也会被移动。
如果您不想使用cpMouse和代码,---您可能正在使用step()函数来迭代您的花栗鼠,这会继续狡猾地改变所有精灵的旋转和位置。因此,如果您尝试更改精灵的位置,步进功能将覆盖旋转和位置,并且不会产生任何影响。