【问题标题】:Get current touch location anytime in Cocos2d for iPhone在 Cocos2d for iPhone 中随时获取当前触摸位置
【发布时间】:2011-05-10 17:11:46
【问题描述】:

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

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

【问题讨论】:

  • 我遇到的问题,即使使用 ccTouchMoved,我也需要移动我的触摸来使事件发生。
  • 那么,您想在手指仍按在同一位置时连续生成事件吗?
  • 是的,这听起来很奇怪,但节点空间实际上会发生变化,因为地图滚动到您的触摸位置,所以即使您的手指在同一个地方,如果滚动,触摸将是节点空间中的不同位置.
  • 那么您可能需要编辑您的问题以包含节点空间/滚动信息/代码。从现在的措辞来看,很难理解如何提供帮助。编辑,对不起,我看到你这样做了。

标签: iphone cocos2d-iphone touch


【解决方案1】:

试试这样...

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

}

【讨论】:

  • 谢谢...我开始以自己的方式做事时遇到越来越多的问题。我会看看这是否对我有用并给你一个更新。
  • 好的,我认为这行得通。问题是我需要将我的触摸位置设为全局,以便其他东西可以访问它。我还一遍又一遍地将精灵结束位置转换为节点空间。
猜你喜欢
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
相关资源
最近更新 更多