使用SpriteKit可重复背景的PanGesture

时间:2013-12-03 05:17:48

标签: ios iphone uigesturerecognizer sprite-kit

我无法在我的游戏菜单中使用可重复的背景。

用户可以在屏幕上滑动手指以选择要播放的角色。 当角色滑入视野时,我有不同背景的视差效果。

以下示例。

- (void)didMoveToView:(SKView *)view 
{
    self.pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(dragScene:)];
    self.pan.minimumNumberOfTouches = 1;
    self.pan.delegate = self;
    [self.view addGestureRecognizer:self.pan];
}

- (void)dragScene:(UIPanGestureRecognizer *)gesture 
{
    CGPoint trans = [gesture translationInView:self.view];
    SKAction *moveSky = [SKAction moveByX:trans.x*0.03 y:0 duration:0];
    [_skyBackground runAction:moveSky];
}

我想重复背景。我知道如何通过自动滚动背景来做到这一点,但我似乎无法让它在这里工作。它需要在左右两个方向上重复。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以再创建两个背景节点 - 一个位于当前背景节点的左侧,另一个位于右侧。移动现有的_skyBackground节点时移动它们。 然后,在更新方法中,检查三个节点中的任何一个是否需要“移位” - 要么在其他两个节点后面,要么在前面。如果需要,你基本上交换三个节点的位置。

-(void)update:(NSTimeInterval)currentTime {

    //get the left background node (or if using an ivar just use _leftNode)
    SKSpriteNode *leftNode = (SKSpriteNode*)[self childNodeWithName:@"leftNode"];

    //my positioning might be off but you'll get the idea
    if (leftNode.position.x < -leftNode.size.width*2)
    {
        leftNode.position = CGPointMake(leftNode.size.width, leftNode.position.y);
    }

    if (leftNode.position.x > leftNode.size.width*2)
    {
        leftNode.position = CGPointMake(-leftNode.size.width, leftNode.position.y);
    }

    //repeat the same for _skyBackground and _rightNode

}

如果图像之间存在微小的间隙,则可能需要3张以上的图像。