在 Cocos2d for iPhone 中随时获取当前触摸位置

2024-08-12移动开发问题
2

本文介绍了在 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 中随时获取当前触摸位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7