views:

55

answers:

1

I'm converting the game I'm working on from UIkit to coocos2d. Using UIKIT I would use the code below code to pass the touch evens to a method. What would be the equivalent in 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];

}

What would be the equivalent using 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];

}

I've tried to figure this out on my own, and I've spent hours on google, but I haven't come up with a workable solution.

+1  A: 

I figured it out. I discovered that, what I was forgetting to do, was to add:

self.isTouchEnabled = YES;

to the Layer's init method.

After I did that the following code worked for me (beginTouchPoint and endTouchPoint are properties of the class):

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

}

Cassie