简体   繁体   English

SpriteKit只允许一键

[英]SpriteKit allow only one touch

Everytime a touch is made, a node is added and will move to another node. 每次触摸时,都会添加一个节点并将其移动到另一个节点。 I want to avoid that you can touch the node many times, and the node will been added as many times as you click. 我想避免您可以多次触摸该节点,并且单击该节点将被添加多次。 It should be only added once, and after the SKAction is done, you can touch the node again. 仅应添加一次,并且在完成SKAction之后,您可以再次触摸该节点。

In my Code below, I tried it with userInteractionEnabled. 在下面的代码中,我使用userInteractionEnabled进行了尝试。 But after the 'Zauberer' is added the third time, it recognizes no touches anymore. 但是在第三次添加“ 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 ];
    }];

}

}

Ok, I got it. 好,我知道了。 If you first touch anywhere else on the screen, and than on the 'Zauberer' node, it wouldn't react. 如果您先触摸屏幕上的其他任何地方,然后触摸“ Zauberer”节点,则它不会反应。 You need to put the self.userInteractionEnabled = NO; 您需要输入self.userInteractionEnabled = NO; in the if sentence, to avoid the problem. 在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