[英]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.