2011-05-10 71 views
0

我一直用下面的方法来检测,当触摸开始,什么位置是:获取当前触摸位置随时在cocos2d为iPhone

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

我遇到的问题,即使使用ccTouchMoved,我也需要移动手指才能使事件发生。 – VagueExplanation 2011-05-10 17:45:17

+0

因此,当你的手指仍然在同一个位置按下时,你想要连续生成事件吗? – makdad 2011-05-10 17:53:31

+0

是的,它听起来很奇怪,但节点空间实际上会因为地图滚动到您的触摸位置而发生变化,所以即使您的手指位于相同的位置,如果滚动,触摸也将是节点空间中的不同位置。 – VagueExplanation 2011-05-10 17:58:22

回答

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

} 
+0

谢谢......我开始以自己的方式做更多的问题。我会看看这是否适用于我,并给你一个更新。 – VagueExplanation 2011-05-12 19:09:36

+0

好吧,我认为这可行。问题在于我需要将我的触摸位置设置为全局,以便其他事物可以访问它。我也一遍又一遍地将精灵的结束位置转换为节点空间。 – VagueExplanation 2011-05-16 18:17:32

+0

很好看...... – RollRoll 2014-09-12 23:22:24