2011-05-10 72 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