2013-03-23 32 views
0

我試圖做一個簡單的遊戲,在cocos2d在現在我有類似的東西移動2 CCSprite與UITouch

#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 

作品CCSprite的舉動,但是當我在同時觸及2 CCSprite,它們重疊本身! 我如何將它們分開移動? 對不起,我的英語和感謝您的幫助!

+0

多點觸控已啓用?對你的UIWindow或UIView使用setMultipleTouchEnabled:是,然後爲要接收觸摸的圖層設置isTouchEnabled屬性。然後試試 – samfisher 2013-03-23 22:41:28

+0

是的,我有:[glView setMultipleTouchEnabled:YES];在AppDelegate中 – pbeo 2013-03-23 22:45:21

回答

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

你的方法是不夠好,決定是否精靈包含給定的觸感和名稱。如果不仔細查看代碼,我不能說他們做了什麼。