检测长触感

Nit*_*ish 1 cocos2d-iphone

我需要在游戏中检测到我想要制作的长时间触摸.我该怎么办?
我面临的其他问题是限制同时触摸.即如果用户一个接一个地触摸超过两次,精灵就不会跳跃.
还有一种方法,我可以通过它添加触摸持续时间因素到精灵的跳跃高度?

谢谢

use*_*184 5

你怎么抓住你的触摸?我总是使用以下方法:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

- (void)ccTouchMoved:(UITouch *)touchx withEvent:(UIEvent *)event

- (void)ccTouchEnded:(UITouch *)touchx withEvent:(UIEvent *)event
Run Code Online (Sandbox Code Playgroud)

在这些方法中,您可以完全控制所有触摸.例如,记住触摸开始的时间:

self.startTime = [NSDate date];
Run Code Online (Sandbox Code Playgroud)

对于实例变量startTime,或检查触摸是否属于某个对象:

 CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
Run Code Online (Sandbox Code Playgroud)

这样您就可以按照自己喜欢的方式轻松实现触摸逻辑......

一个很好的方法是 - 我发现 - 定义游戏对象中的所有必需变量,比如......

@interface Enemy : CCSprite <CCTargetedTouchDelegate> {

    EnemyState state;
    NSInteger enemyID;
    NSDate *startTime;
    NSDate *endTime;

    UITouch *lastTouch; 
Run Code Online (Sandbox Code Playgroud)

添加为ccTouchBegan显示具体示例代码:

   - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
    {
        D_DBG (@"ENEMY ID %i",self.enemyID);

//implement your logic if a touch for this enemy is allowed or not
        if (![self.delegate touchIsAllowed: enemyID touch: touch]) return NO;

//if the enemy is untouched, then may be touched
        if ((state == kEnemyStateUngrabbed) &&  (![self containsTouchLocation:touch] )) return NO;

        state = kEnemyStateGrabbed;
        self.startTime = [NSDate date];
        [self.delegate  informAboutEnemyStarted: self.enemyID startTime: self.startTime atPoint: self.position];
    return YES;
    }
Run Code Online (Sandbox Code Playgroud)