0

我正在将我正在开发的游戏从 UIkit 转换为 coocos2d。使用 UIKIT,我将使用下面的代码将触摸事件传递给方法。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];

}

使用 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];

}

我试图自己解决这个问题,我在谷歌上花了几个小时,但我还没有想出一个可行的解决方案。

4

1 回答 1

1

我想到了。我发现,我忘记做的是添加:

self.isTouchEnabled = YES;

到Layer的init方法。

在我这样做之后,以下代码对我有用(beginTouchPoint 和 endTouchPoint 是类的属性):

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

}

于 2010-08-23T06:50:38.153 回答