获取当前的触摸位置随时在Cocos2d的iPhone [英] Get current touch location anytime in Cocos2d for iPhone
本文介绍了获取当前的触摸位置随时在Cocos2d的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.
EDIT
bit我认为,但这是我做的:
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的iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文