2014-01-17 34 views
0

我正在嘗試使多點觸摸工作,以便我可以同時移動兩個精靈。我跟着這個教程http://www.saturngod.net/detecting-touch-events-in-cocos2d-iphone-ganbaru-games,這是我在ccTouchesBegan代碼:cocos2d中的多點觸摸錯誤 - 索引1超出界限

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

NSArray *touchArray = [touches allObjects]; 

// We're going to track the first two touches (i.e. first two fingers) 
// Create "UITouch" objects representing each touch 
UITouch *fingerOne = [touchArray objectAtIndex:0]; 
UITouch *fingerTwo = [touchArray objectAtIndex:1]; 

// Convert each UITouch object to a CGPoint, which has x/y coordinates we can actually  use 
CGPoint pointOne = [fingerOne locationInView:[fingerOne view]]; 
CGPoint pointTwo = [fingerTwo locationInView:[fingerTwo view]]; 

// The touch points are always in "portrait" coordinates 
// You will need to convert them if in landscape (which we are) 
pointOne = [[CCDirector sharedDirector] convertToGL:pointOne]; 
pointTwo = [[CCDirector sharedDirector] convertToGL:pointTwo]; 

if (CGRectContainsPoint(ball.boundingBox, pointOne)) 
{ 
    //ball.position = ccp(location.x , location.y); 
    areWeTouchingABall = YES; 
    //printf("*** ccTouchesBegan (x:%f, y:%f)\n", location.x, location.y); 
} 

if(CGRectContainsPoint(sp.boundingBox, pointOne)){ 
    areWeTouchingASquare = YES; 
    // printf("*** ccTouchesBegan (x:%f, y:%f)\n", location.x, location.y); 
} 


// Only run the following code if there is more than one touch 
if ([touchArray count] > 1) 
{ 
    if (CGRectContainsPoint(ball.boundingBox, pointTwo)) 
    { 
     //ball.position = ccp(location.x , location.y); 
     areWeTouchingABall = YES; 
     //printf("*** ccTouchesBegan (x:%f, y:%f)\n", location.x, location.y); 
    } 

    if(CGRectContainsPoint(sp.boundingBox, pointTwo)){ 
     areWeTouchingASquare = YES; 
     // printf("*** ccTouchesBegan (x:%f, y:%f)\n", location.x, location.y); 
    } 
} 

}

這是touchesMoved:

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

NSArray *touchArray = [touches allObjects]; 

// We're going to track the first two touches (i.e. first two fingers) 
// Create "UITouch" objects representing each touch 
UITouch *fingerOne = [touchArray objectAtIndex:0]; 
UITouch *fingerTwo = [touchArray objectAtIndex:1]; 

// Convert each UITouch object to a CGPoint, which has x/y coordinates we can actually use 
CGPoint pointOne = [fingerOne locationInView:[fingerOne view]]; 
CGPoint pointTwo = [fingerTwo locationInView:[fingerTwo view]]; 

// The touch points are always in "portrait" coordinates 
// You will need to convert them if in landscape (which we are) 
pointOne = [[CCDirector sharedDirector] convertToGL:pointOne]; 
pointTwo = [[CCDirector sharedDirector] convertToGL:pointTwo]; 

if (areWeTouchingABall == YES) // 
{ 
    ball.position = ccp(pointOne.x , pointOne.y); 
    ball.zOrder = 1; 
    sp.zOrder = 0; 
} 

if (areWeTouchingASquare == YES) // 
{ 
    sp.position = ccp(pointOne.x , pointOne.y); 
    sp.zOrder = 1; 
    ball.zOrder = 0; 
} 


// Only run the following code if there is more than one touch 
if ([touchArray count] > 1) 
{ 
    /*if (areWeTouchingABall == YES && CGRectContainsPoint(ball.boundingBox, pointOne)) // 
    { 
     ball.position = ccp(pointOne.x , pointOne.y); 
     ball.zOrder = 1; 
     sp.zOrder = 0; 
    }*/ 

    if (areWeTouchingABall == YES && CGRectContainsPoint(ball.boundingBox, pointTwo)) // 
    { 
     ball.position = ccp(pointTwo.x , pointTwo.y); 
     ball.zOrder = 1; 
     sp.zOrder = 0; 
    } 

    /*if (areWeTouchingASquare == YES && CGRectContainsPoint(ball.boundingBox, pointOne)) // 
    { 
     sp.position = ccp(pointOne.x , pointOne.y); 
     sp.zOrder = 1; 
     ball.zOrder = 0; 
    }*/ 

    if (areWeTouchingASquare == YES && CGRectContainsPoint(ball.boundingBox, pointTwo)) // 
    { 
     sp.position = ccp(pointTwo.x , pointTwo.y); 
     sp.zOrder = 1; 
     ball.zOrder = 0; 
    } 
} 

}

,這是touchesEnded:

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

areWeTouchingABall = NO; 
areWeTouchingASquare = NO; 
//printf("*** ccTouchesEnded (x:%f, y:%f)\n", location.x, location.y); 

}

我每次用一根手指在任何地方碰,我得到這個錯誤:

「終止應用程序由於未捕獲的異常 'NSRangeException',原因是:「* - [__ NSArrayI objectAtIndex:]:指數1越界[0..0]'「,

當我用兩根手指觸摸時,錯誤沒有出現,但多點觸摸無法正常工作(我無法用手指每個都拖動一個精靈。第二個精靈感動跳躍直接給其它手指的位置,以便這兩個精靈是下一個手指)

我就確定添加代碼:

[glView setMultipleTouchEnabled:YES]; 

我AppDelegate.m文件和我有聯繫在我的init方法中啓用。

我該如何解決這個問題,以便多點觸摸正常工作,錯誤被刪除?

回答

1
UITouch *fingerOne = [touchArray objectAtIndex:0]; 
UITouch *fingerTwo = [touchArray objectAtIndex:1]; 

無法保證在觸摸事件中您總能收到兩次觸摸。第一次測試有多少接觸有使用

UITouch *fingerOne = [touchArray objectAtIndex:0]; 
UITouch *fingerTwo = nil; 
if (touchArray.count > 1) 
{ 
    fingerTwo = [touchArray objectAtIndex:1]; 
} 

即使是這樣,這將不是邏輯,因爲數組中的第二觸摸並不總是對應於手指#2工作是touchArray。我建議閱讀tracking individual fingers here

+0

謝謝你的鏈接。我閱讀了文檔和其他一些文檔,並對其進行了一些瞭解。我在這個鏈接找到示例代碼:https://developer.apple.com/library/ios/samplecode/Touches/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007435 這正是我想要的使用多點觸控,即移動物體(1個自身或同時多個),但我不確定如何繼續使用Cocos2d做到這一點。 可能你知道我應該接下來要採取的步驟嗎? – EonNomad

+0

只是爲了讓你知道,我對Objective-C和Cocos2d都很新,並且努力不僅僅是簡單地拿出示例代碼並重新適合它們。 – EonNomad