繁体   English   中英

SpriteKit只允许一键

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM