touchBegan 时间戳

发布于 2024-11-03 16:44:00 字数 896 浏览 1 评论 0原文

在我的游戏中,我需要计算触摸的持续时间。我这样做的方法是:

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

self.endTime = [NSDate date];  //NSDate *endTime in .h
NSLog(@"%@",self.endTime); 

}  

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event  
{  
    tStart = [[NSDate date] timeIntervalSinceDate:self.endTime];  
    NSLog(@"duration %f",tStart);  

我使用这个时间间隔作为一个因素来计算玩家跳跃的高度。 tStart 越小,跳跃越低,tStart 越多,跳跃越高。我这样做是这样的:

if(tStart/1000<=9.430)  
        {  
            [player jump:5.0f];  
        }  
        else if(tStart>9.430 && tStart<=9.470)  
        {  
            [player jump:7.0f];  
        }  
        else if(tStart/1000>9.470)  
        {  
            [player jump:8.0f];  
        }  

但是我想在 tochBegan 上执行此操作,以便玩家在触摸屏幕时可以立即跳跃。为此需要 touchBegan 中 tStart 的值。我该怎么做呢?

谢谢

In my game I need to calculate duration of touch. I did this by :

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

self.endTime = [NSDate date];  //NSDate *endTime in .h
NSLog(@"%@",self.endTime); 

}  

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event  
{  
    tStart = [[NSDate date] timeIntervalSinceDate:self.endTime];  
    NSLog(@"duration %f",tStart);  

I am using this time interval as a factor to calculate height of the jump the player makes.
Less is the tStart, low is the jump and more is the tStart, high is the jump. I am doing this as :

if(tStart/1000<=9.430)  
        {  
            [player jump:5.0f];  
        }  
        else if(tStart>9.430 && tStart<=9.470)  
        {  
            [player jump:7.0f];  
        }  
        else if(tStart/1000>9.470)  
        {  
            [player jump:8.0f];  
        }  

However I want to perform this action on tochBegan so that player may jump as soon as screen is touched. For this need the value of tStart in touchBegan. How should I do that?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2024-11-10 16:44:00

对于给定的触摸,UITouch 实例是相同的,因此在 ccTouchBegan 处保存具有最旧时间戳的触摸,然后等待 ccTouchEnded。当你得到之前保存的UITouch时,就意味着玩家抬起了手指。

更新

您就可以

  • 一旦用户触摸屏幕,
  • 跳转。当触摸持续 x 毫秒时跳跃并加速跳跃(来自葡萄柚的建议)。
  • 当用户释放触摸或经过 x 毫秒的最大时间时跳转。
  • 跳跃并加速跳跃,直到触摸结束。

选项 4 不切实际,因为用户可以根据需要一直按下去。因此,假设您想要进行变量跳转,以下是选项 3 的代码:

UITouch *jump = nil;

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    // no jump ongoing, start one
    if (jump==nil){
        jump = touch;
        // jump on a separate thread
        [NSThread performSelectorInBackground:(SEL)jump withObject:touch];
    }
}

-(void) jump:(UITouch*) touch {
    NSInterval time = [touch timestamp]; // I *think* this is the time since system uptime :P
    int jumpSpeed = 1;
    // increase the jump speed for every 100ms up to 'maxTimeYouAreAllowedToJump' ms
    while ( ([NSProcessInfo systemUptime]-time < maxTimeYouAreAllowedToJump) && (jump!=nil) ){
        jumpSpeed += 1;
        [NSThread sleepForTimeInterval:.1]; // sleep for 100ms
    }
    [self doTheJumpingWithSpeed:jumpSpeed]; // do the actual jump!
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    // user released the touch that initiated the jump
    if ((touch!=nil) && (touch==jump)) {
        jump =nil;
    }
}

For a given touch, the UITouch instance is the same, so at ccTouchBegan save the touch/touches with the oldest timestamp and then wait for ccTouchEnded. When you get the UITouch that you previously saved, it means the player lifted the finger.

update

You can

  • Jump as soon as the user touches the screen.
  • Jump and boost the jump while the touch is ongoing up to x milliseconds (suggestion from grapefrukt).
  • Jump when the user releases the touch or a max time of x milliseconds has elapsed.
  • Jump and boost the jump until the touch is over.

Option 4 is unpractical because the user can keep pressing as long as he wants. So given that you want to make a variable jump, here is code for option 3:

UITouch *jump = nil;

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    // no jump ongoing, start one
    if (jump==nil){
        jump = touch;
        // jump on a separate thread
        [NSThread performSelectorInBackground:(SEL)jump withObject:touch];
    }
}

-(void) jump:(UITouch*) touch {
    NSInterval time = [touch timestamp]; // I *think* this is the time since system uptime :P
    int jumpSpeed = 1;
    // increase the jump speed for every 100ms up to 'maxTimeYouAreAllowedToJump' ms
    while ( ([NSProcessInfo systemUptime]-time < maxTimeYouAreAllowedToJump) && (jump!=nil) ){
        jumpSpeed += 1;
        [NSThread sleepForTimeInterval:.1]; // sleep for 100ms
    }
    [self doTheJumpingWithSpeed:jumpSpeed]; // do the actual jump!
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    // user released the touch that initiated the jump
    if ((touch!=nil) && (touch==jump)) {
        jump =nil;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文