无法使背景无限滚动
Having trouble making background scroll infinitely
我正在用 sprite kit 和 objective 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];
}
当我运行应用程序时,我的屏幕上没有任何显示。
创建两个背景精灵。
// 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);
}
这应该很简单
我正在用 sprite kit 和 objective 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];
}
当我运行应用程序时,我的屏幕上没有任何显示。
创建两个背景精灵。
// 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);
}
这应该很简单