我开始使用Sprite工具包,我想知道如何创建一个无限的侧滚动游戏?我阅读了Sprite工具包文档,并阅读了场景的预处理。据说我们可以重新调整场景的位置,以防内容大于场景。我尝试了它然后它工作,当我滚动整个背景图像时,我开始看到场景的默认背景。如何创建无限背景?任何人都能指出正确的文件或谈论他的问题的文章吗?谢谢。
答案 0 :(得分:12)
这样的事情应该让你开始:
添加背景图片......
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像素。
答案 1 :(得分:3)
为此我做了一个名为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/
答案 2 :(得分:1)
My code example for this exact reference using SpriteKit and Swift is seen below.
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)
I wrapped this in a function because for a parallax background, I was able to copy this function 4 times, rename the variables, change the "duration" for the shiftBackground constant, and get different speeds based on their distance.
I hope this helps! When Swift goes to Swift 3 this "C-Style" for loop will be deprecated, so I'm not sure how to fix that yet for the long term.
答案 3 :(得分:0)
我一直在为一个无限平铺滚动条的库工作,希望它可以成为侧卷轴的起点:
它使用类似tableView的委托,所以我希望能够很容易地为滚动条提供切片或任何类型的节点。我还没有实现任何碰撞逻辑,但我还计划添加它。