Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't drag sprite after adding a shape to it
    primarykey
    data
    text
    <p>I am creating a bunch of sprites from label textures dynamically and that is working fine. I have a few methods which I can use to tap a sprite and make it 'jiggle' then I can drag them around the screen. My problem is that I needed to add a Chipmunk shape to the sprites so that they don't overlap and they push each other out of the way. Once I add the shape to the sprites, then my methods for selecting/dragging the sprites no longer works. I am a bit lost.</p> <p>This is the method used for creating the sprites/shapes.</p> <pre><code>-(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-&gt;p = wordSprite.position; cpSpaceAddBody(space, body); cpShape* shape = cpPolyShapeNew(body, num, verts, cpvzero); shape-&gt;e = 0.5f; shape-&gt;u = 0.5f; shape-&gt;data = wordSprite; cpSpaceAddShape(space, shape); // END SHAPE CREATION [movableSprites addObject:wordSprite]; [self addChild:wordSprite]; } </code></pre> <p>These are the methods I use for selecting and moving the sprites around (which no longer function after adding the shape/body).</p> <pre><code>- (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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload