我开始使用Sprite kit,并想知道如何创建一个无限边滚动的游戏?我阅读了Sprite kit文档并查看了场景的预处理。它说明我们可以在内容大于场景时重新调整场景的位置。我尝试了它,它有效果,但是当我滚动整个背景图像时,我开始看到场景的默认背景。如何创建无限的背景?有人可以指向正确的文档或讨论此问题的文章吗?谢谢。
我开始使用Sprite kit,并想知道如何创建一个无限边滚动的游戏?我阅读了Sprite kit文档并查看了场景的预处理。它说明我们可以在内容大于场景时重新调整场景的位置。我尝试了它,它有效果,但是当我滚动整个背景图像时,我开始看到场景的默认背景。如何创建无限的背景?有人可以指向正确的文档或讨论此问题的文章吗?谢谢。
类似这样的东西应该可以帮助你开始:
添加背景图片...
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);
}
}];
这是从RW的示例中得出的,他的示例使用了1136像素的背景图片尺寸。
我为此目的创建了一个通用组件,名为SKScrollingNode
,因为我通常添加多个滚动背景来实现视差效果。使用它非常简单:
在您的场景类中声明它。
@property(strong,nonatomic) SKScrollingNode * clouds;
self.clouds = [SKScrollingNode spriteNodeWithImageNamed:@"clouds"];
self.clouds.scrollingSpeed = .5; // Speed at which the clouds will scroll
[self addChild:clouds];
[self.clouds update:currentTime]
完成!
您可以在此处找到“SKScrollinNode”组件的完整代码:
https://github.com/kirualex/SprityBird/blob/master/spritybird/Classes/Scenes/
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)
我一直在开发一个无限瓦片滚动库,希望能成为横向滚动游戏的起点:
它使用类似tableView的代理,所以我希望很容易提供瓦片或任何类型的节点给滚动器。目前还没有实现碰撞逻辑,但我计划也会添加进去。