Search code examples
ioscocos2d-iphonemoveccsprite

Move 2 CCSprite with UITouch


I try make a simple game in cocos2d at now i have something like that

#import "GameplayLayer.h"

@implementation GameplayLayer
-(id)init {
    self = [super init];
    if (self != nil) {
        CGSize screenSize = [CCDirector sharedDirector].winSize;
        // właczenie obsługi dotyku
        self.isTouchEnabled = YES;
        if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {

        }
        else{
            paddle1 = [CCSprite spriteWithFile:@"bijakiPhone.png"];
            paddle2 = [CCSprite spriteWithFile:@"bijakiPhone.png"];
            puck = [CCSprite spriteWithFile:@"krazekiPhone.png"];
        }
        //Polozenie i inicjalizacja paletki nr 1
        [paddle1 setPosition:
         CGPointMake(screenSize.width/2,
                     screenSize.height*0.17f)];
        [self addChild:paddle1];
        //Polozenie i inicjalizacja paletki nr 2
        [paddle2 setPosition:
         CGPointMake(screenSize.width/2,
                     screenSize.height*0.83f)];
        [self addChild:paddle2];
        //Polozenie i inicjalizacja krązka
        [puck setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];
        [self addChild:puck];
    }
    return self;
}
//onEnter
- (void)onEnter
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
    [super onEnter];
}

//onExit
- (void)onExit
{
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
    [super onExit];
}
-(BOOL)containsTouch:(UITouch *)touch {
    CGRect r=[paddle1 textureRect];
    CGPoint p=[paddle1 convertTouchToNodeSpace:touch];
    return CGRectContainsPoint(r, p );
}
-(BOOL)containsTouch2:(UITouch *)touch {
    CGRect r=[paddle2 textureRect];
    CGPoint p=[paddle2 convertTouchToNodeSpace:touch];
    return CGRectContainsPoint(r, p );
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    if ([self containsTouch:touch]){
       CCLOG(@"krarzek 1 tapniety");
        isTouched1 = YES;
    }


    if ([self containsTouch2:touch]){
        CCLOG(@"krarzek 2 tapniety");
        isTouched2 = YES;
    }

    return YES;
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
    if (isTouched1){
        CGPoint newTouchLocation = [touch locationInView:touch.view];
        newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
        [paddle1 setPosition:newTouchLocation];

    }


    if (isTouched2){
        CGPoint newTouchLocation = [touch locationInView:touch.view];
        newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
        [paddle2 setPosition:newTouchLocation];
    }

}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
    if (isTouched1){
        isTouched1=NO;
        CCLOG(@"krarzek 1 zwolniony");
}
    if (isTouched2){
        isTouched2=NO;
        CCLOG(@"krarzek 2 zwolniony");
    }
}
@end

Works CCSprite move, but when i touch 2 CCSprite at same time, They overlap itself! How i can move them separately? Sorry for my English and Thanks for help!


Solution

  • The reason of your problem that you don't store somewhere what touch is connected to your paddle. So in case of both touches are inside your paddle sprites, both isTouched1 and isTouched2 variables have value YES. So in your ccTouchMoved:withEvent: method both sprites will be placed to the same position in this case. Store your touches in some variable or I suggest to use dictionary for this with touch as a key and sprite that you need to move as value. In this case your ccTouchMoved:withEvent: method will be look like this

    -(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
    {
            CGPoint newTouchLocation = [touch locationInView:touch.view];
            newTouchLocation = [[CCDirector sharedDirector] convertToGL:newTouchLocation];
            CCNode paddle = [_yourDict objectForKey: touch];
            [paddle setPosition:newTouchLocation];
    }
    

    And names of your methods that determine if sprite contains given touch are not good enough. I could not say what they do without looking through the code.