如何使背景向左滚动

时间:2015-01-18 17:43:30

标签: objective-c sprite

我正在制作一个用Objective-CSpriteKit编写的游戏,玩家可以从右到左或从左到右滚动背景。

目前我已经想出如何编写代码以在用户滑动时使背景向右滚动,而不是如何使其向左滚动。

所以,我的问题是如何让它滚动到左边?这是代码:

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];

//Update Method:

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);
}

1 个答案:

答案 0 :(得分:0)

尝试

 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);
}