随时随地在Cocos2d for iPhone中获取当前触摸位置

时间:2011-05-10 17:11:47

标签: iphone cocos2d-iphone touch

我一直在使用以下方法来检测触摸何时开始以及位置是什么:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{

    CGPoint touchLocation = [touch locationInView: [touch view]];
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];

    touchLocation = [self convertToNodeSpace:touchLocation];

    return YES;
}

但我还想知道如果我的手指仍然在屏幕上,我可以随时获取当前位置。谁知道怎么样?感谢。

修改

我会稍微发布这个作为答案,但这就是我所做的:

我只是在我所有方法之外定义的ccTime方法中转换变量:

- (void)update:(ccTime)dt {
heroMoveEndLoc = [self convertToNodeSpace:heroMoveEndLoc];
}

1 个答案:

答案 0 :(得分:9)

试试这个......

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

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    x_point = location.x;
    y_point  = location.y;
    [self schedule:@selector(updateFunction)];
}

-(void)updateFunction
{
    NSLog(@"Location is %0.02f %0.02f",x_point,y_point);
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self unschedule:@selector(updateFunction)];

}