如何检查SpriteKit屏幕的任何一半是否有触摸?

时间:2014-07-23 00:09:00

标签: ios swift sprite-kit

我正在制作一个spriteKit游戏,其中有一些东西在移动,我想要这样做,如果我触摸屏幕的右半部分它向右转,如果我触摸左半部分,它就向左转。我怎样才能不断获得屏幕上所有触摸的位置?

1 个答案:

答案 0 :(得分:4)

您应该可以执行以下操作

目标-C

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];

    if (touchLocation.x < self.frame.size.width / 2) {
        // Left side of the screen
    } else {
        // Right side of the screen
    }
}

夫特

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch:UITouch = touches.anyObject() as UITouch
    let touchLocation = touch.locationInNode(self)

    if touchLocation.x < self.frame.size.width / 2 {
        // Left side of the screen
    } else {
        // Right side of the screen
    }
}