Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:41:13+00:00 2026-05-27T18:41:13+00:00

I am creating a bunch of sprites from label textures dynamically and that is

  • 0

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.

This is the method used for creating the sprites/shapes.

-(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];

}

These are the methods I use for selecting and moving the sprites around (which no longer function after adding the shape/body).

- (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];    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:41:14+00:00Added an answer on May 27, 2026 at 6:41 pm

    If you are working with chipmunk + TouchMove, you should use, cpMouse.
    Check out http://www.cocos2d-iphone.org/forum/topic/4936

    There were two examples on Github,
    1) Kerchink (used with new version) which is removed :(,
    2) Grabbed or Grabbed to (used with old version).http://code.google.com/p/grabbed/

    cpMouse is just invisible body which connected with on touchBegan, with the object on the toucharea, using PinJoints,SlideJoints etc. When you touchMoved, the object connected with cpMouse also get moved.

    If you would like to go without cpMouse and with your code, — you may be using step() function to iterate your chipmunk, which continue sly changes the all sprite’s rotation and position. So if you are trying change the position of the sprites, step function will override the rotation and position and there will be no effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a GridView/DetailsView page. I have a grid that displays a bunch
I'm creating a bunch of migrations, some of which are standard create table or
I am creating as stored procedure that brings back a bunch of data that
I have an application creating a bunch of divs through a loop. Each div
So I am creating a website that I want to have an admin directory
There's a bunch of special macros that MFC uses when creating dialogs, and in
So you're creating a bunch of code in an external .js file that requires
I want to create a bunch of forms that all have the same properties
I'm creating a standard menu in my WPF application. I know that I can
I'm creating a website and part of the functionality is that a user can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.