禁用碰撞3秒钟

时间:2018-02-07 06:49:16

标签: objective-c collision

我试图通过在我的播放器对象上将physicsBody设置为nil 3秒来禁用冲突。我尝试了以下但它根本不起作用。谢谢你的任何建议。

double delayInSeconds = 10.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)
(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSLog(@"Do some work");
self.player.physicsBody = nil;
});

1 个答案:

答案 0 :(得分:0)

我只是通过在触发didBeginContact时禁用碰撞来找到答案,然后使用计时器在3秒后设置碰撞。只需重新考虑我的逻辑,但它比我想象的要容易。工作代码如下:

-(void)didBeginContact:(SKPhysicsContact *)contact{

NSLog(@"contact detected");
//Disbale Collision
self.player.physicsBody = nil;
//time i want to delay
double delayInSeconds = 3.0;
//Delay func
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)
(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
NSLog(@"Log Delay");
//enable collision after 3 seconds
self.player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.player.frame.size];
});

多数民众赞成。