[英]Move Sprite on Y-axis by dragging screen
如何在Sprite Kit中使Sprite在y轴上上下移动(iPhone处于横向)。 子画面必须保持在设定的X值上。 当用户拖动它时完成。
您需要实现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);
}
}
祝好运!!
绝对定位的非常简单的解决方案:
-(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;
}
您可以使用SKAction类的此方法:-
-(void)touchesMoved:(NSSet *)触摸withEvent:(UIEvent *)事件
{
[mySprite runAction:[SKAction moveToY:400 duration:0.2];
}
该SKAction方法创建一个垂直移动节点的动作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.