Iphone 通过拖动屏幕在Y轴上移动精灵

Iphone 通过拖动屏幕在Y轴上移动精灵,iphone,sprite,sprite-kit,Iphone,Sprite,Sprite Kit,如何使精灵套件中的精灵在y轴上上下移动(iPhone处于横向)。精灵必须保持在设定的X值上。这是在用户拖动它时完成的。您需要实现触摸移动 从接收器的前一个位置减去当前位置,您将获得移动精灵的量 目标-C - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint scenePosition = [touch location

如何使精灵套件中的精灵在y轴上上下移动(iPhone处于横向)。精灵必须保持在设定的X值上。这是在用户拖动它时完成的。

您需要实现触摸移动

从接收器的前一个位置减去当前位置,您将获得移动精灵的量

目标-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);
  }
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
接触{
让scenePosition=touch.locationInNode(自身)
让lastPosition=touch.previousLocationInNode(自身)
让translation=CGPoint(x:0.0,y:scenePosition.y-lastPosition.y)
yourSprite.position=CGPointMake(yourSprite.position.x,yourSprite.position.y+translation.y);
}
}

祝你好运

您需要实施触摸移动

从接收器的前一个位置减去当前位置,您将获得移动精灵的量

目标-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);
  }
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
接触{
让scenePosition=touch.locationInNode(自身)
让lastPosition=touch.previousLocationInNode(自身)
让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;
}

绝对定位的一个非常简单的解决方案:

-(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类的此方法:-

-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{

}


此SKAction方法创建垂直移动节点的操作。

您可以使用SKAction类的此方法:-

-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{

}

此SKAction方法创建垂直移动节点的操作