2012-02-02 18 views
6

में मैं Cocos2d नवीनतम संस्करण यहाँ के लिए काम करने के लिए स्वाइप पाने के लिए कोशिश कर रहा हूँ मेरी कोड है!स्वाइप Coco2d

अद्यतन 1:

मैं निम्नलिखित और अभी भी कोई स्वाइप का पता चलता है करने के लिए कोड अद्यतन किया गया।

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


} 

उत्तर

11

मैं भी इस काम करने के लिए कोशिश की है, लेकिन मैं विधि नियंत्रित करने के लिए एक आसान और भी बेहतर मिल गया है।

तो उदाहरण के लिए यदि आप बाईं ओर एक स्वाइप का पता लगाना चाहते हैं तो मैं ऐसा करूँगा।

आप के इंटरफेस में दो चर घोषित कर रहे हैं वर्ग

CGPoint firstTouch; 
CGPoint lastTouch; 

अपने वर्ग के कार्यान्वयन की init विधि में

self.isTouchEnabled = YES; 

3.Add अपनी कक्षा

इन तरीकों छूता सक्षम
-(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]; 
    } 

} 

"डूस्टफ" विधि को बाएं स्वाइप होने पर क्या कहा जाता है।

+0

मैं UIGestureRecognizer का उपयोग करना पसंद करता हूं क्योंकि विभिन्न प्रकार के स्पर्श ईवेंट बनाना आसान है। – azamsharp

+2

यह प्रतिभाशाली है! –

3

कोड सही है और काम करना चाहिए।

आप यह सत्यापित करना चाहते हैं कि न तो उपयोगकर्ता इंटरैक्शन और न ही स्पर्श इनपुट ग्लू व्यू या मुख्य विंडो पर अक्षम हैं।

आपको यह भी जांचना चाहिए कि क्या cocos2d किसी भी तरह छू रहा है या नहीं। ईएजीएलवी व्यू छूने का पहला रिसीवर है, और उन्हें सीसीटीएचडी डिस्पैचर के लिए आगे बढ़ाता है। मैं कल्पना कर सकता हूं कि यदि आपने लक्षित प्रतिनिधियों को लक्षित किया है तो वे स्पर्श को "निगल सकते हैं"। हालांकि कोकोस 2 डी को इशारा पहचानकर्ताओं के बाद ही स्पर्श प्राप्त करना चाहिए।

+0

मैंने अपने मूल प्रश्न में कोड अपडेट किया लेकिन अभी भी स्वाइप का पता नहीं चला है। – azamsharp

संबंधित मुद्दे