2010-08-18 26 views
0

Je convertis le jeu sur lequel je travaille depuis UIkit en coocos2d. En utilisant UIKIT, j'utiliserais le code ci-dessous pour passer le test à une méthode. Quel serait l'équivalent dans Cocos2d?Conversion de beginTouches et endTouches en ccBeginTouch et ccEndTouch

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Save the position 
    for (UITouch *touch in touches) { 
     // Send to the dispatch method, which will make sure the appropriate subview is acted upon 
     [self dispatchFirstTouchAtPoint:[touch locationInView:boardView] forEvent:nil]; 
    } 
} 

// Saves the first position for reference when the user lets go. 
- (void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event 
{ 
    beginTouchPoint = touchPoint; 
} 

// Handles the continuation of a touch. 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches) { 
     // Send to the dispatch method, which will make sure the appropriate subview is acted upon 
     [self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:boardView]]; 
    } 
} 

-(void) dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position 
{ id sender; 
    int directionSwiped; 
    int row,column; 
    CGFloat xDelta = position.x - beginTouchPoint.x; 
    CGFloat yDelta = position.y - beginTouchPoint.y; 
     [self findSwipeDirectionWith: xDelta and: yDelta]; 

} 

Quel serait l'équivalent en utilisant cocos2d?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Save the position 
    for (UITouch *touch in touches) { 
     // Send to the dispatch method, which will make sure the appropriate subview is acted upon 
     [self dispatchFirstTouchAtPoint:[touch locationInView:boardView] forEvent:nil]; 
    } 
} 

// Saves the first position for reference when the user lets go. 
- (void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event 
{ 
    beginTouchPoint = touchPoint; 
} 

// Handles the continuation of a touch. 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches) { 
     // Send to the dispatch method, which will make sure the appropriate subview is acted upon 
     [self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:boardView]]; 
    } 
} 

-(void) dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position 
{ id sender; 
    int directionSwiped; 
    int row,column; 
    CGFloat xDelta = position.x - beginTouchPoint.x; 
    CGFloat yDelta = position.y - beginTouchPoint.y; 
     [self findSwipeDirectionWith: xDelta and: yDelta]; 

} 

J'ai essayé de comprendre cela moi-même, et je l'ai passé des heures sur Google, mais je ne suis pas venu avec une solution viable.

Répondre

1

Je l'ai compris. J'ai découvert que, ce que j'oublie de faire, était d'ajouter:

self.isTouchEnabled = YES; 

à la méthode init de la couche.

Après avoir fait que le code suivant a fonctionné pour moi (beginTouchPoint et endTouchPoint sont propriétés de la classe):

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

    UITouch* myTouch = [touches anyObject];  
    CGPoint location = [myTouch locationInView: [myTouch view]]; 
    beginTouchPoint = [[CCDirector sharedDirector]convertToGL:location];  
} 

    // Handles the continuation of a touch.* 
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    static BOOL isFirstTouch = YES; 
    UITouch* myTouch = [touches anyObject]; 
    int row,column; 
    int directionSwiped; 
    CGPoint location = [myTouch locationInView: [myTouch view]];  
    endTouchPoint = [[CCDirector sharedDirector]convertToGL:location]; 
    CGFloat xDelta = endTouchPoint.x - beginTouchPoint.x; 
    CGFloat yDelta = endTouchPoint.y - beginTouchPoint.y; 
    [self findSwipeDirectionWith: xDelta and: yDelta]; 

}