如何使精灵套件中的精灵在 y 轴上上下移动(iPhone 处于横向状态)。精灵必须保持在设定的 X 值上。这是在用户拖动它时完成的。
问问题
323 次
3 回答
3
您需要实现touchesMoved
从接收器的先前位置减去当前位置,您将获得移动精灵的数量。
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);
}
斯威夫特 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);
}
}
祝你好运!!
于 2013-11-14T11:37:23.917 回答
1
一个非常简单的绝对定位解决方案:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchLocation;
for (UITouch *touch in touches) {
touchLocation = [touch locationInNode:self];
}
CGPoint spriteLocation = yourSprite.position;
spriteLocation.y = touchLocation.y;
yourSprite.position = spriteLocation;
}
于 2014-01-20T19:40:42.600 回答
1
您可以使用 SKAction 类的这种方法:-
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *) event
{
[mySprite runAction:[SKAction moveToY:400 duration:0.2];
}
这个 SKAction 方法创建一个垂直移动节点的动作。
于 2014-02-07T08:03:26.473 回答