2012-02-02 24 views
6

tôi đang cố gắng để có được những swiping để làm việc cho Cocos2d phiên bản mới nhất tại đây là mã của tôi:Vuốt trong Coco2d

-(void) setupGestureRecognizers 
{ 
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)]; 

    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 

    [swipeLeft setNumberOfTouchesRequired:1]; 

    [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeLeft]; 


} 

Nó không phát hiện các swipe ở tất cả!

CẬP NHẬT 1:

Tôi đã cập nhật mã thành mã sau đây và vẫn không phát hiện thấy swipes.

-(void) setupGestureRecognizers 
{ 
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)]; 

    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft]; 

    [swipeLeft setNumberOfTouchesRequired:1]; 

    [[[[CCDirector sharedDirector] openGLView] window] setUserInteractionEnabled:YES]; 

    [[[CCDirector sharedDirector] openGLView] setUserInteractionEnabled:YES]; 
    [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeLeft]; 


} 

Trả lời

11

Tôi đã cố gắng thực hiện công việc này nhưng tôi đã tìm thấy phương pháp kiểm soát dễ dàng hơn và tốt hơn.

ví dụ: nếu bạn muốn phát hiện một thao tác vuốt sang trái, tôi sẽ theo dõi sau.

Khai báo hai biến trong giao diện của bạn lớp

CGPoint firstTouch; 
CGPoint lastTouch; 

Trong phương thức init việc thực hiện các lớp học của bạn cho phép chạm

self.isTouchEnabled = YES; 

3.Add những phương pháp để lớp học của bạn

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSSet *allTouches = [event allTouches]; 
    UITouch * touch = [[allTouches allObjects] objectAtIndex:0]; 
    CGPoint location = [touch locationInView: [touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    //Swipe Detection Part 1 
    firstTouch = location; 
} 

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSSet *allTouches = [event allTouches]; 
    UITouch * touch = [[allTouches allObjects] objectAtIndex:0]; 
    CGPoint location = [touch locationInView: [touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    //Swipe Detection Part 2 
    lastTouch = location; 

    //Minimum length of the swipe 
    float swipeLength = ccpDistance(firstTouch, lastTouch); 

    //Check if the swipe is a left swipe and long enough 
    if (firstTouch.x > lastTouch.x && swipeLength > 60) { 
     [self doStuff]; 
    } 

} 

phương thức "doStuff" được gọi là nếu vuốt trái đã xảy ra.

+0

Tôi thích sử dụng UIGestureRecognizer vì dễ dàng tạo các loại sự kiện cảm ứng khác nhau. – azamsharp

+2

đây là thiên tài! –

3

Mã đúng và phải hoạt động.

Bạn có thể muốn xác minh rằng cả thông tin người dùng không tương tác cũng không chạm vào đều bị tắt trên chế độ xem gl hoặc cửa sổ chính.

Bạn cũng nên kiểm tra xem cocos2d có đang ăn chạm không. Lớp EAGLView là người nhận đầu tiên của các chạm, và chuyển tiếp chúng đến CCTouchDispatcher. Tôi có thể tưởng tượng rằng nếu bạn có các đại biểu cảm ứng mục tiêu, họ có thể "nuốt" các điểm chạm. Mặc dù cocos2d chỉ nhận được chạm sau khi nhận dạng cử chỉ.

+0

Tôi đã cập nhật mã trong câu hỏi ban đầu của mình nhưng vẫn không phát hiện thấy các lần vuốt. – azamsharp

Các vấn đề liên quan