使用UITouch移动2个CCSprite

时间:2013-03-23 22:37:51

标签: ios cocos2d-iphone move ccsprite

我尝试在cocos2d中制作一个简单的游戏,现在我有类似的东西

#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,但是当我同时触摸2个CCSprite时,它们会重叠! 我怎么能分开移动它们? 对不起我的英文,谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

您遇到问题的原因是您没有存放与您的划水板相连的触点。因此,如果两个触摸都在您的桨状精灵中,则isTouched1isTouched2变量都具有值YES。所以在你的ccTouchMoved:withEvent:方法中,在这种情况下,两个精灵都将被放置在相同的位置。将你的触摸存储在一些变量中,或者我建议使用字典,将触摸作为键和精灵,你需要将其作为值移动。在这种情况下,您的ccTouchMoved:withEvent:方法将如下所示

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

确定精灵是否包含给定触摸的方法名称不够好。如果不查看代码,我无法说出他们做了什么。