简体   繁体   中英

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.

In my Code below, I tried it with userInteractionEnabled. But after the 'Zauberer' is added the third time, it recognizes no touches anymore.

 -(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. You need to put the self.userInteractionEnabled = NO; in the if sentence, to avoid the problem.

-(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 ];
    }];

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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