Cocos2d iphone Can';添加形状后不要拖动精灵
我正在从标签纹理动态创建一组精灵,效果很好。我有一些方法可以用来轻触精灵,让它“抖动”,然后在屏幕上拖动它们。我的问题是,我需要给精灵添加一个花栗鼠形状,这样它们就不会重叠,也不会互相推开。将形状添加到精灵后,选择/拖动精灵的方法将不再有效。我有点迷路了 这是用于创建精灵/形状的方法Cocos2d iphone Can';添加形状后不要拖动精灵,cocos2d-iphone,Cocos2d Iphone,我正在从标签纹理动态创建一组精灵,效果很好。我有一些方法可以用来轻触精灵,让它“抖动”,然后在屏幕上拖动它们。我的问题是,我需要给精灵添加一个花栗鼠形状,这样它们就不会重叠,也不会互相推开。将形状添加到精灵后,选择/拖动精灵的方法将不再有效。我有点迷路了 这是用于创建精灵/形状的方法 -(void) addNewSpriteX: (float)x y:(float)y wordIndex:(int)i { CCLabelTTF *label; if ( i == 0 )
-(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];
}
这些是我用来选择和移动精灵的方法(在添加形状/主体后不再起作用)
如果您正在使用chipmunk+TouchMove,您应该使用,cpMouse。 查看www.cocos2d-iphone.org/forum/topic/4936 Github上有两个例子, 1) 已删除的墨迹(与新版本一起使用):(, 2) 抓取或抓取到(与旧版本一起使用)。http://code.google.com/p/grabbed/ cpMouse只是一个无形的物体,它在触摸时与物体相连,物体位于触摸区域,使用针状关节、滑动点等。当你触摸移动时,与cpMouse相连的物体也会移动 如果您不想使用cpMouse和您的代码,您可以使用step()函数迭代您的花栗鼠,这将继续狡猾地更改所有精灵的旋转和位置。因此,如果您试图更改精灵的位置,阶跃函数将覆盖旋转和位置,不会产生任何效果 [link]()cpMouse还有一个例子。
- (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];
}