如何实现sprite kit的触摸和保持?

时间:2014-02-12 02:46:56

标签: ios iphone ipad sprite-kit

我最近开始使用sprite-kit。我知道touchesBegan只适用于一个水龙头,但有什么我可以使用的,可以识别按下的触摸?

3 个答案:

答案 0 :(得分:5)

如果你想实现像拍摄这样的东西,你需要开始用touchesBegan方法拍摄并停止用touchesEnded方法拍摄:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self startShooting];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self stopShooting];
}

出于其他目的,您可以将UILongPressGestureRecognizer添加到SKScene

答案 1 :(得分:4)

或者,您可以使用带有更新方法的布尔值:

bool isTouching = false;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    isTouching = true;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    isTouching = false;
}

-(void)update:(CFTimeInterval)currentTime {
    if(isTouching){
        //shooting! 
    }
}

如果您愿意,可以轻松地在isTouching块中组合您的方法,并使用touchesBegan来说明同时瞄准子弹。

您不必担心计时器,因为只要isTouching == true

,更新方法将继续执行代码块

答案 2 :(得分:3)

var isTouching = false

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    handleTouches(touches) 
    isTouching = true;
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    handleTouches(touches)
    isTouching = false;
}

override func update(currentTime: NSTimeInterval) {
    if isTouching{
        //Shoot CODE!
    }
}