我开始使用Sprite工具包,我想知道如何创建一个无限滚动的游戏?我阅读了Sprite工具包文档,并阅读了场景的预处理过程。据说我们可以重新调整场景的位置,以防内容比场景大。我试过了,但是,当我滚动整个背景图像时,我开始看到场景的默认背景。如何创建无限的背景?谁能给我推荐正确的文档或文章来讨论他的问题?谢谢。
发布于 2013-10-14 11:35:58
像这样的东西应该会让你开始:
添加背景图像...
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];
}在你的update方法中。
[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的一个例子,他的例子使用了1136px的背景图像大小。
发布于 2014-02-15 19:30:57
为此,我做了一个名为SKScrollingNode的通用合成器,因为我通常会添加多个滚动背景来实现视差效果。使用它非常简单:
在scene类中声明它
@property(strong,nonatomic) SKScrollingNode * clouds;创建它并将其添加到scene实例中
self.clouds = [SKScrollingNode spriteNodeWithImageNamed:@"clouds"];
self.clouds.scrollingSpeed = .5; // Speed at which the clouds will scroll
[self addChild:clouds];在你的场景“update”方法中,只需调用
[self.clouds update:currentTime]完成了!
你可以在这里找到'SKScrollinNode‘合成器的完整代码:
https://github.com/kirualex/SprityBird/blob/master/spritybird/Classes/Scenes/
发布于 2016-09-01 01:38:13
我的代码示例使用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)我将其封装在一个函数中,因为对于视差背景,我能够复制该函数4次,重命名变量,更改shiftBackground常量的“持续时间”,并根据它们的距离获得不同的速度。
我希望这能帮到你!当Swift转到Swift 3时,这个“C风格”的for循环将被弃用,所以我还不确定如何长期修复它。
https://stackoverflow.com/questions/19349168
复制相似问题