精灵套件侧滚动

时间:2013-10-13 19:13:26

标签: ios sprite-kit

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

4 个答案:

答案 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)
  1. create constant backgroundTexture created as an SKTexture with your image.
  2. create constants for moving your background to the left (shiftBackground) and for adding another instance of your background to the right side of your currently displayed one (replaceBackground)
  3. create constant (movingAndReplacingBackground) as an SKAction that repeats forever, setting the parameter of function repeatActionForever to a SKAction.sequence with your shiftBackground and replaceBackground constants referenced.
  4. create a loop that defines the definitions for your background. (for variable i, if amount of i is less than 3, increment i by one.
  5. in the loop, defend the texture, position, size, and zPosition (if sibling order is turned off) and then call your movingAndReplacingBackground as your action, which then iterates your sequence.
  6. finally, the last part of the loop is adding your background. It will perform this action any time there is less than 3 iterations of it. and as soon as your first background reaches far left of screen, it will remove the nodes to 2, therefore running the action in the loop over again.

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)

我一直在为一个无限平铺滚动条的库工作,希望它可以成为侧卷轴的起点:

RPTileScroller

它使用类似tableView的委托,所以我希望能够很容易地为滚动条提供切片或任何类型的节点。我还没有实现任何碰撞逻辑,但我还计划添加它。