ios – 如何使用spritekit连续滚动背景

ios – 如何使用spritekit连续滚动背景,第1张

概述我有我的背景设置,它是水平滚动,这是我的代码: -(void)initalizingScrollingBackground{for (int i = 0; i < 2; i++){ SKSpriteNode *bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"]; bottomScrollerHeight = b 我有我的背景设置,它是水平滚动,这是我的代码:

-(voID)initalizingScrollingBackground{for (int i = 0; i < 2; i++){    SKSpriteNode *bg = [SKSpriteNode spriteNodeWithImagenamed:@"background"];    bottomScrollerHeight = bg.size.height;    bg.position = CGPointMake(i * bg.size.wIDth,0);    bg.anchorPoint = CGPointZero;    bg.name = @"background";    [self addChild:bg];}

}

还有这段代码:

- (voID)moveBottomScroller{[self enumerateChildNodesWithname:@"background" usingBlock: ^(SKNode *node,BOol *stop) {     SKSpriteNode * bg = (SKSpriteNode *) node;     CGPoint bgVeLocity = CGPointMake(-BG_VELociTY,0);     CGPoint amtToMove = CGPointMultiplyScalar(bgVeLocity,_dt);     bg.position = CGPointAdd(bg.position,amtToMove);     //Checks if bg node is completely scrolled off the screen,if yes then put it at the end of the other node     if (bg.position.x <= -bg.size.wIDth)     {         bg.position = CGPointMake(bg.position.x + bg.size.wIDth*2,bg.position.y);     }     [bg removeFromParent];     [self addChild:bg];        //Ordering is not possible. so this is a Hack }];

}

第二部分使背景滚动.没有它,背景仍然是.

此外,没有movebottomscroller,我的精灵出现在背景之上.使用movebottomscroller,他出现在滚动背景后面.是否有任何命令将他带到前面,超过任何其他背景?

谢谢!

解决方法 尝试下面的方法,希望对你有用.

@interface GameScene()@property (nonatomic) NSTimeInterval lastTimeSceneRefreshed;@end@implementation GameScene- (instancetype)initWithSize:(CGSize)size {    if (self = [super initWithSize:size]) {        [self buildBackground];        [self startScrolling];    }    return self;}// This method will add 3 background nodes- (voID)buildBackground {    float centerX = CGRectGetMIDX(self.frame);    SKSpriteNode *firstBackgroundNode = [SKSpriteNode spriteNodeWithImagenamed:@"background"];    firstBackgroundNode.name = @"background";    firstBackgroundNode.position = CGPointMake(centerX,firstBackgroundNode.size.height*firstBackgroundNode.anchorPoint.y);    [self addChild:firstBackgroundNode];    float prevIoUsYposition = firstBackgroundNode.position.y;    for (int i = 0; i < 2; i++) {        SKSpriteNode *backgroundNode = [SKSpriteNode spriteNodeWithImagenamed:@"background"];        backgroundNode.position = CGPointMake(centerX,prevIoUsYposition + backgroundNode.frame.size.height);        prevIoUsYposition = backgroundNode.position.y;        backgroundNode.name = @"background";        [self addChild:backgroundNode];    }}- (voID)update:(CFTimeInterval)currentTime {            // Updating background nodes    // We don't want to update backgrounds each frame (60 times per second)    // Once per second is enough. This will reduce cpu usage    if (currentTime - self.lastTimeSceneRefreshed > 1) {        [self backgroundNodesRepositioning];        self.lastTimeSceneRefreshed = currentTime;    }}- (voID)backgroundNodesRepositioning {    [self enumerateChildNodesWithname:@"background" usingBlock: ^(SKNode *node,BOol *stop)    {        SKSpriteNode *backgroundNode = (SKSpriteNode *)node;        if (backgroundNode.position.y + backgroundNode.size.height < 0) {            // The node is out of screen,move it up            backgroundNode.position = CGPointMake(backgroundNode.position.x,backgroundNode.position.y + backgroundNode.size.height * 3);        }    }];}- (voID)startScrolling {    SKAction *moveAction = [SKAction moveByX:0 y:-200 duration:1];    [self enumerateChildNodesWithname:@"background" usingBlock: ^(SKNode *node,BOol *stop)    {        [node runAction:[SKAction repeatActionForever:moveAction] withKey:@"movement"];    }];}
总结

以上是内存溢出为你收集整理的ios – 如何使用spritekit连续滚动背景全部内容,希望文章能够帮你解决ios – 如何使用spritekit连续滚动背景所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1024421.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存