0

スプライト キットのスプライトを y 軸上で上下に移動させるにはどうすればよいですか (iPhone は横向きです)。スプライトは設定された X 値にとどまる必要があります。これは、ユーザーがドラッグしたときに行われます。

4

3 に答える 3

3

You need to implement touchesMoved

Subtract the the current location from previous location of the receiver and you will get the amount to move your sprite.

Objective-C

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [touches anyObject];
  CGPoint scenePosition = [touch locationInNode:self];
  CGPoint lastPosition = [touch previousLocationInNode:self];

  CGPoint translation = CGPointMake(0.0, scenePosition.y - lastPosition.y);
  yourSprite.position = CGPointMake(yourSprite.position.x, yourSprite.position.y + translation.y); 
}

Swift 2.2

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
  for touch in touches {
    let scenePosition = touch.locationInNode(self)
    let lastPosition = touch.previousLocationInNode(self)

    let translation = CGPoint(x: 0.0, y: scenePosition.y - lastPosition.y)
    yourSprite.position = CGPointMake(yourSprite.position.x, yourSprite.position.y + translation.y);
  }
}

Good Luck!!

于 2013-11-14T11:37:23.917 に答える