雪碧套件侧滚动

我开始使用Sprite工具包,我想知道如何创build无限侧滚动游戏? 我读了Sprite套件文档,并通读了场景的预处理。 据说,如果内容大于场景,我们可以重新调整场景的位置。 我尝试了它,但是,当我滚动整个背景图像时,我开始看到场景的默认背景。 我如何创build无限的背景? 任何人都可以向我指出正确的文件或文章,谈论他的问题? 谢谢。

像这样的东西应该让你开始:

添加背景图片…

for (int i = 0; i < 2; i++) { SKSpriteNode * bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"]; bg.anchorPoint = CGPointZero; bg.position = CGPointMake(i * bg.size.width, 0); bg.name = @"background"; [self addChild:bg]; } 

在你的更新方法。

 [self enumerateChildNodesWithName:@"background" usingBlock: ^(SKNode *node, BOOL *stop) { SKSpriteNode *bg = (SKSpriteNode *) node; bg.position = CGPointMake(bg.position.x - 5, bg.position.y); if (bg.position.x <= -bg.size.width) { bg.position = CGPointMake(bg.position.x + bg.size.width * 2, bg.position.y); } }]; 

这是从他的例子使用背景图像大小1136像素的一个例子。

为此我做了一个名为SKScrollingNode的通用化合物,因为我通常添加多个滚动背景来实现视差效果。 使用它非常简单:

在场景类中声明它

 @property(strong,nonatomic) SKScrollingNode * clouds; 

创build它并将其添加到场景实例

 self.clouds = [SKScrollingNode spriteNodeWithImageNamed:@"clouds"]; self.clouds.scrollingSpeed = .5; // Speed at which the clouds will scroll [self addChild:clouds]; 

在你的场景“更新”的方法,只是打电话

 [self.clouds update:currentTime] 

完成!

你可以在这里find'SKScrollinNode'的完整代码:

https://github.com/kirualex/SprityBird/blob/master/spritybird/Classes/Scenes/

下面是我使用SpriteKit和Swift进行精确引用的代码示例。

 func background1() { let backgroundTexture = SKTexture(imageNamed: "bg") //move background right to left; replace let shiftBackground = SKAction.moveByX(-backgroundTexture.size().width, y: 0, duration: 15) let replaceBackground = SKAction.moveByX(backgroundTexture.size().width, y:0, duration: 0) let movingAndReplacingBackground = SKAction.repeatActionForever(SKAction.sequence([shiftBackground,replaceBackground])) for var i:CGFloat = 0; i<3; i++ { //defining background; giving it height and moving width let background = SKSpriteNode(texture:backgroundTexture) background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * i), y: CGRectGetMidY(self.frame)) background.size.height = self.frame.height background.zPosition = -20 background.runAction(movingAndReplacingBackground) self.addChild(background) 
  1. 创build与您的图像SKTexture创build常量backgroundTexture。
  2. 创build用于将背景移动到左侧(shiftBackground)的常量以及将背景的另一个实例添加到当前显示的背景的右侧(replaceBackground)
  3. 创build常量(movingAndReplacingBackground)作为永久重复的SKAction,将函数repeatActionForever的参数设置为SKAction.sequence,并引用shiftBackground和replaceBackground常量。
  4. 创build一个定义背景定义的循环。 (对于variablesi,如果i的数量小于3,则将i递增1。
  5. 在循环中,捍卫纹理,位置,大小和zPosition(如果同胞顺序closures),然后将您的movingAndReplacingBackground作为您的动作调用,然后迭代您的序列。
  6. 最后,循环的最后部分是添加你的背景。 只要less于3次迭代,它就会执行这个动作。 只要你的第一个背景到达屏幕的最左边,它将把节点移除到2,因此再次在循环中执行动作。

我把它包含在一个函数中,因为对于视差背景,我可以复制这个函数4次,重命名variables,改变shiftBackground常量的“持续时间”,并根据它们的距离获得不同的速度。

我希望这有帮助! 当Swift进入Swift 3时,这个“C-Style”for循环将被弃用,所以我不知道如何解决这个问题。

我一直在为一个无限的平铺卷轴的图书馆工作,希望可以成为一个侧面卷轴的起点:

RPTileScroller

它使用了一个类似于tableView的委托,所以我希望能够很容易地为这个滚动条提供tile或者任何types的节点。 我还没有实现任何碰撞逻辑,但我也计划添加。