在 Cocos2d for iPhone 中随时获取当前触摸位置 [英] Get current touch location anytime in Cocos2d for iPhone

查看:27
本文介绍了在 Cocos2d for iPhone 中随时获取当前触摸位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been using the following method to detect when a touch began and what the location is:

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

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

    touchLocation = [self convertToNodeSpace:touchLocation];

    return YES;
}

但如果我的手指仍在屏幕上,我也想知道如何随时获取当前位置.有谁知道怎么做?谢谢.

But I would also like to know how to get the current location anytime of my touch if my finger is still on the screen. Anyone know how? Thanks.

编辑

我想我会尽快将其发布为答案,但这就是我所做的:

I'll post this as an answer in a bit I think, but this is what I did:

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

I just kept converting a variable in a ccTime method defined outside of all my methods:

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

推荐答案

试试这样...

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

}

这篇关于在 Cocos2d for iPhone 中随时获取当前触摸位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆