如何使背景向左滚动
How to make a background scroll left
我正在制作一个用 Objective-C
和 SpriteKit
编写的游戏,其中玩家可以从右向左或从左向右滚动背景。
目前我已经想出如何编写代码使用户滑动时背景向右滚动,但不知道如何使其向左滚动。
所以,我的问题是如何让它向左滚动?这是代码:
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);
}
尝试
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);
}
我正在制作一个用 Objective-C
和 SpriteKit
编写的游戏,其中玩家可以从右向左或从左向右滚动背景。
目前我已经想出如何编写代码使用户滑动时背景向右滚动,但不知道如何使其向左滚动。
所以,我的问题是如何让它向左滚动?这是代码:
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);
}
尝试
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);
}