无法进行背景滚动时遇到问题

时间:2015-01-04 22:06:36

标签: ios objective-c

我正在制作一个带有精灵套件和目标c的游戏。目前我正试图让我的背景无限滚动。我似乎无法做到这一点。这是我的代码:

SKAction方法

-(void) setUpActions {
SKAction* move = [SKAction moveByX:-landscape.frame.size.width * 2 y:0 duration:2.0];
SKAction* reset = [SKAction moveByX:landscape.frame.size.width * 2 y:0 duration:0.0];
SKAction* moveInfinitely = [SKAction sequence:@[move, reset]];


//background and log Moving Actions
moveOver = [SKAction repeatActionForever:moveInfinitely];
}

SKAction调用方法

-(void) runTheAction {
[landscape runAction:moveOver];
}

当我运行应用程序时,我的屏幕上没有显示任何内容。

1 个答案:

答案 0 :(得分:0)

创建两个背景精灵。

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

这应该非常简单