2014-01-17 10 views
0

동시에 두 개의 스프라이트를 이동할 수 있도록 멀티 터치 작업을 시도하고 있습니다. 나는이 튜토리얼 http://www.saturngod.net/detecting-touch-events-in-cocos2d-iphone-ganbaru-games을 따라이는 내가 ccTouchesBegan에있는 코드 :이있는 touchesMoved입니다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); 
    } 
} 

}

:

-(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 bounds [0 .. 0] ' ",

두 손가락으로 만지면 오류가 나타나지 않지만 멀티 터치가 올바르게 작동하지 않습니다. 스프라이트를 손가락으로 각각 드래그 할 수 없습니다. . 내가 코드를 추가해야했다

두 번째 스프라이트는) 직선 모두 스프라이트는 한 손가락을 받고있다 그래서 다른 손가락 위치로 점프를 감동 : 내 AppDelegate.m 파일에

[glView setMultipleTouchEnabled:YES]; 

을 나는 터치가 내 init 메소드에서 사용 가능합니다.

멀티 터치가 제대로 작동하고 오류가 제거되도록하려면 어떻게해야합니까?

답변

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

터치 이벤트에서 항상 두 번 터치한다는 보장은 없습니다. 첫 번째 테스트 배열의 두 번째 터치는 항상 손가락 # 2 일치하지 않을 수 있기 때문에

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

심지어 다음이 논리적으로 작동하지 않습니다 사용 touchArray에 얼마나 많은 접촉. 나는 tracking individual fingers here에 읽을 것을 권합니다.

+0

감사합니다. 나는 문서와 몇 가지 다른 것들을 읽고 조금 더 이해하고있다. 이 링크에서 샘플 코드를 찾았습니다. https://developer.apple.com/library/ios/samplecode/Touches/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007435 정확하게 원하는 부분이 있습니다. 멀티 터치로 할 수 있습니다. 하나의 객체 또는 여러 객체를 동시에 움직일 수 있습니다. 그러나 Cocos2d로 그 작업을 수행하는 방법을 잘 모르겠습니다. 다음 단계는 어떤 단계를 거쳐야하는지 알 수 있습니까? – EonNomad

+0

그냥 알려 주시면 Objective-C와 Cocos2d에 대해 아직 완전히 익숙하지 않고 단순히 샘플 코드를 가져 와서 다시 적용하지 않으려 고 노력하고 있습니다. – EonNomad