Iphone 触摸开始时间戳
在我的游戏中,我需要计算接触的持续时间。我是这样做的:Iphone 触摸开始时间戳,iphone,box2d,Iphone,Box2d,在我的游戏中,我需要计算接触的持续时间。我是这样做的: -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { self.endTime = [NSDate date]; //NSDate *endTime in .h NSLog(@"%@",self.endTime); } -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)even
-(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);
我用这个时间间隔作为计算运动员跳跃高度的一个因素。
少是起点,低是跳跃,多是起点,高是跳跃。我这样做是因为:
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];
}
if(tStart/10009.430和&tStart9.470)
{
[运动员跳跃:8.0f];
}
然而,我想在tochBegan上执行这个动作,这样玩家可以在触摸屏幕时立即跳起。对于这一需求,touchStart中的tStart值已开始。我该怎么做
感谢对于给定的触摸,UITouch实例是相同的,因此在CCTouch开始时,使用最早的时间戳保存触摸,然后等待ccTouchEnded。当您获得之前保存的UITouch时,表示玩家举起了手指 更新 你可以
- 用户触摸屏幕后立即跳转
- 当触摸持续到x毫秒时,跳转并加速跳转(来自grapefrukt的建议)
- 当用户释放触摸或已过x毫秒的最长时间时跳转
- 跳跃并加速跳跃,直到触球结束
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;
}
}
UITouch*jump=nil;
-(BOOL)CCTouchStart:(UITouch*)触摸事件:(UIEvent*)事件{
//没有跳转,开始一个
如果(跳转==nil){
跳跃=触摸;
//跳转到另一个线程上
[NSThread performSelectorInBackground:(选择)使用对象跳转:触摸];
}
}
-(无效)跳跃:(UITouch*)触摸{
NSInterval time=[touch timestamp];//我*认为*这是自系统正常运行以来的时间:P
int-jumpSpeed=1;
//每100毫秒增加一次跳跃速度,直到“maxTimeYouAreAllowedToJump”毫秒
而(([NSProcessInfo系统正常运行时间]-time
所以,基本上你想知道用户在触摸屏幕的那一刻按住手指的时间有多长?ios有点神奇,但没那么神奇。你需要重新思考你的跳跃机制,最常见的方法是在跳跃后,如果按钮(或触摸)仍被按下,保持“推动”玩家一段时间。这就是大多数平台开发人员的做法:)但是如果你说如果触控仍然被抑制,我可以让玩家跳跃一段时间,那么我一定有办法知道,触控仍然被抑制。我怎么能找到呢?手势识别器能帮我吗?谢谢Jano。但是我需要在CCTouchStart中执行跳转操作,即在调用ccTouchEnded之前。你的建议也一样吗?一点解释会非常有用:)我实现了上面的代码,效果很好。但选项2是我问题的解决方案。即在触摸进行时跳跃和提升。在这种情况下应该怎么做?很高兴听到。我猜跳跃:它本身应该绘制跳跃,并不断更改坐标,直到最长时间结束或触摸为零。如果这是box2d,我不知道它是如何绘制的。我们所说的跳跃是什么意思:它本身应该绘制跳跃?在方法跳跃中:你必须在屏幕上绘制游戏的角色,并加速游戏速度,只要手指按到最大时间。但我在物理模拟方面做得太过火了。