gpt4 book ai didi

ios - 滚动背景 - Sprite Kit

转载 作者:技术小花猫 更新时间:2023-10-29 10:57:24 29 4
gpt4 key购买 nike

所以我尝试使用这篇文章的解决方案 (Sprite kit side scrolling) 创建一个无限滚动的背景。 但是,我想让图像可重复。正如您在下面的视频中看到的,在图像完成水平方向后,有一些空隙。我想让图像填充那个空隙,所以不断重复。

http://www.youtube.com/watch?v=kyLTGz7Irrc或者 https://vimeo.com/79555900 (密码:spritekit)

我做了什么:

for (int i = 0; i < 2; i++) {
SKSpriteNode * bg = [SKSpriteNode spriteNodeWithImageNamed:@"bgimage"];
bg.anchorPoint = CGPointZero;
bg.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.origin.y);
bg.name = @"snow1";
[self addChild:bg];
}

在更新方法中:

[self enumerateChildNodesWithName:@"snow1" 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);
}];

最佳答案

无论如何,我修复了它。以防万一其他人需要它,我就是这样做的:

    // Create 2 background sprites
bg1 = [SKSpriteNode spriteNodeWithImageNamed:@"bg1"];
bg1.anchorPoint = CGPointZero;
bg1.position = CGPointMake(0, 0);
[self addChild:bg1];

bg2 = [SKSpriteNode spriteNodeWithImageNamed:@"bg2"];
bg2.anchorPoint = CGPointZero;
bg2.position = CGPointMake(bg1.size.width-1, 0);
[self addChild:bg2];

然后在更新方法中:

    bg1.position = CGPointMake(bg1.position.x-4, bg1.position.y);
bg2.position = CGPointMake(bg2.position.x-4, bg2.position.y);

if (bg1.position.x < -bg1.size.width)
bg1.position = CGPointMake(bg2.position.x + bg2.size.width, bg1.position.y);

if (bg2.position.x < -bg2.size.width)
bg2.position = CGPointMake(bg1.position.x + bg1.size.width, bg2.position.y);

关于ios - 滚动背景 - Sprite Kit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20019522/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com