0
votes

I have 6 sprites that I want to move around:

-(id) init {

if( (self=[super init]) )
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];

    CCSprite * backGround = [CCSprite spriteWithFile:@"background_ipad.png"];
    backGround.position = ccp(winSize.width/2, winSize.height/2);
    [self addChild:backGround z:0];

    cloth1 = [CCSprite spriteWithFile:@"clothe_1.png"];
    cloth1.position = ccp(-200, -15);
    [self addChild:cloth1 z:1];
    cloth2 = [CCSprite spriteWithFile:@"clothe_2.png"];
    cloth2.position = ccp(130, 225);
    [self addChild:cloth2 z:2];

    cloth3 = [CCSprite spriteWithFile:@"clothe_3.png"];
    cloth3.position = ccp(365, 225);
    [self addChild:cloth3 z:3];

    cloth4 = [CCSprite spriteWithFile:@"clothe_4.png"];
    cloth4.position = ccp(-110, -15);
    [self addChild:cloth4 z:4];

    cloth5 = [CCSprite spriteWithFile:@"clothe_5.png"];
    cloth5.position = ccp(130, -20);
    [self addChild:cloth5 z:5];

    cloth6 = [CCSprite spriteWithFile:@"clothe_6.png"];
    cloth6.position = ccp(365, -15);
    [self addChild:cloth6 z:6];

}
return self;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

}

and this method to move:

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];
    //Sprite follows the finger movement.
    [cloth1 setPosition:location];
}

and the thing is that, I want to add more sprites there to move the sprite. I triend adding more sprites in the //follow the finger movement but then all sprites follow the finger movement. I want to move one single sprite. For example: when touching cloth1, move cloth 1; when touching cloth2, move cloth 2; but not both at the same time.

Can someone tell me how to do this?

2
You deleted your question and posted an almost identical one. Why didn't you just edit it? - Kreiri
Sorry, because I thought It would be better to do it again. - Vergmort

2 Answers

1
votes
@interface YourClass : NSObject
{
    NSMutableArray  *mSpriteArray;
    CCSprite  *mSpriteOnHand;
}

//In implementation:

-(id) init 
{
  .. //your old code
  ..

   [mSpriteArray addObject: cloth1];
   [mSpriteArray addObject: cloth2];
   [mSpriteArray addObject: cloth3];
   [mSpriteArray addObject: cloth4];
   [mSpriteArray addObject: cloth5];
   [mSpriteArray addObject: cloth6];

}

-(void)onEnter
{
    [super onEnter];
    self.touchEnabled = YES; //    self.isTouchEnabled = YES;

}


- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    mSpriteOnHand = nil;

    for(CCSprite *cloth in mSpriteArray)
    {
        if(CGRectContainsPoint([cloth boundingBox], location))
        {
            mSpriteOnHand = cloth;
            break;
        }
    }
}


- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    if(mSpriteOnHand)
       mSpriteOnHand.position = location;

 }

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    mSpriteOnHand = nil;
}

-(void)onExit
{
   [mSpriteArray release]; 
   mSpriteArray = nil;
   [super onExit];
}
0
votes

return statement always terminates function and returns control to the calling function. You wrote:

 return self;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

[[CCTouchDispatcher sharedDispatcher]... line will never get executed.

Is your class a subclass of CCLayer? If you set layer's isTouchEnabled property to YES, it will add this layer as standard (non-targeted) touch delegate.

If you must use targeted touch in your layer, you should return YES in -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event if you are claiming this touch, and NO if you aren't. Updates of a claimed touch are sent only to the delegate which claimed it.

To determine which sprite is touched: make an instance variable to store "selected" sprite, in touchBegan method check which sprite's bounding box contains touch location and store this sprite in instance variable (also, looks like you want to claim a touch only if it's touching a sprite).

[cloth1 setPosition:location];

-- your sprite will "jump" to touch position. Usually it doesn't look nice. I would get touch's locationInView: and previousLocationInView:, convert them to GL, get the difference and change sprite's position by that difference.