【问题标题】:How to make a background continuously scroll vertically with spritekit如何使用 spritekit 使背景连续垂直滚动
【发布时间】:2014-10-03 05:37:47
【问题描述】:

我设置了背景,它正在水平滚动,这是我的代码:

-(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,他出现在滚动背景后面。有什么命令可以将他带到前线,高于任何其他背景?

谢谢!

【问题讨论】:

    标签: ios ios7 sprite-kit


    【解决方案1】:

    试试下面的方法,希望对你有用。

    @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"];
        }];
    }
    

    【讨论】:

    • 谢谢!我现在正在尝试这个。到目前为止,它看起来很好,除了这一行... float previousYPosition = entryPathBlock.position.y;我收到错误“使用未声明的标识符 'entryPathBlock'。我该如何解决这个问题?(对不起,我还是新手!)
    • 和 enumerateChildNodesWithName 缺少结束“]”
    • 编辑后的答案没有错误,太棒了!当我运行它时,背景会出现,但它没有滚动。我应该添加或更改什么以使其自动滚动吗?我会继续查看代码以尝试找到它,谢谢您的帮助!
    • 啊,由于某种原因,它仍然无法滚动。如果您碰巧看到出了什么问题,那就太好了,但非常感谢您的帮助!我正在四处寻找是否可行。
    • 你在哪里调用startScrolling方法?确保在 buildBackground 方法之后调用它
    【解决方案2】:

    我不完全理解你的方法,但我可以回答你的问题,所以我会继续这样做 - 我希望我没有误解。

    SKNode 有一个方法 insertChild:atIndex: 可以让你把背景节点放在后面,或者把精灵放在前面。

    SKNode Class Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-24
      相关资源
      最近更新 更多