SpriteKit只允许一次触摸

时间:2014-08-01 17:29:39

标签: ios objective-c sprite-kit touchesbegan

每次触摸时,都会添加一个节点并移动到另一个节点。我想避免您可以多次触摸节点,并且在您单击时添加节点的次数。它应该只添加一次,并且在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 ];
    }];

}

}

1 个答案:

答案 0 :(得分: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 ];
    }];

}

}