[英]SpriteKit allow only one touch
每次觸摸時,都會添加一個節點並將其移動到另一個節點。 我想避免您可以多次觸摸該節點,並且單擊該節點將被添加多次。 僅應添加一次,並且在完成SKAction之后,您可以再次觸摸該節點。
在下面的代碼中,我使用userInteractionEnabled進行了嘗試。 但是在第三次添加“ Zauberer”之后,它不再識別任何觸摸。
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.userInteractionEnabled = NO;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"Zauberer"]){
Wurfstein = [SKSpriteNode spriteNodeWithImageNamed:@"Wurfstein.png"];
Wurfstein.position = CGPointMake(Mensch.position.x, Mensch.position.y);
Wurfstein.zPosition = 1;
Wurfstein.scale = 0.6;
Wurfstein.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:5];
Wurfstein.physicsBody.dynamic = NO;
Wurfstein.physicsBody.allowsRotation = NO;
Wurfstein.physicsBody.usesPreciseCollisionDetection = YES;
Wurfstein.physicsBody.restitution = 0;
Wurfstein.physicsBody.categoryBitMask = SteinCategory ;
Wurfstein.physicsBody.collisionBitMask = ZaubererCategory;
Wurfstein.physicsBody.contactTestBitMask = ZaubererCategory;
SKAction *action = [SKAction moveTo:Zauberer.position duration:0.5];
SKAction *remove = [SKAction removeFromParent];
[self addChild:Wurfstein];
[Wurfstein runAction:[SKAction sequence:@[action,remove]]completion:^{
self.userInteractionEnabled = YES;
[Zauberer removeFromParent];
[self performSelector:@selector(Zauberer) withObject:nil afterDelay:5.0 ];
}];
}
}
好,我知道了。 如果您先觸摸屏幕上的其他任何地方,然后觸摸“ Zauberer”節點,則它不會反應。 您需要輸入self.userInteractionEnabled = NO; 在if語句中,避免出現問題。
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:@"Zauberer"]){
self.userInteractionEnabled = NO;
Wurfstein = [SKSpriteNode spriteNodeWithImageNamed:@"Wurfstein.png"];
Wurfstein.position = CGPointMake(Mensch.position.x, Mensch.position.y);
Wurfstein.zPosition = 1;
Wurfstein.scale = 0.6;
Wurfstein.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:5];
Wurfstein.physicsBody.dynamic = NO;
Wurfstein.physicsBody.allowsRotation = NO;
Wurfstein.physicsBody.usesPreciseCollisionDetection = YES;
Wurfstein.physicsBody.restitution = 0;
Wurfstein.physicsBody.categoryBitMask = SteinCategory ;
Wurfstein.physicsBody.collisionBitMask = ZaubererCategory;
Wurfstein.physicsBody.contactTestBitMask = ZaubererCategory;
SKAction *action = [SKAction moveTo:Zauberer.position duration:0.5];
SKAction *remove = [SKAction removeFromParent];
[self addChild:Wurfstein];
[Wurfstein runAction:[SKAction sequence:@[action,remove]] completion:^{
self.userInteractionEnabled = YES;
[Zauberer removeFromParent];
[self performSelector:@selector(Zauberer) withObject:nil afterDelay:4.0 ];
}];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.