我正在为iOS开发SpriteKit游戏,我正在学习通过阅读Ray Wenderlich教程(Space Shooter SpriteKit)来构建游戏
但是我想在纵向模式下制作应用程序,因此我希望背景从上到下移动.
#pragma mark - TBD - Game Backgrounds
//1
NSArray *parallaxBackgroundNames = @[@"bg_galaxy.png", @"bg_planetsunrise.png",
@"bg_spacialanomaly.png", @"bg_spacialanomaly2.png"];
CGSize planetSizes = CGSizeMake(200.0, 200.0);
//2
_parallaxNodeBackgrounds = [[FMMParallaxNode alloc] initWithBackgrounds:parallaxBackgroundNames
size:planetSizes
pointsPerSecondSpeed:10.0];
//3
_parallaxNodeBackgrounds.position = CGPointMake(size.width/2.0, size.height/2.0);
//4
[_parallaxNodeBackgrounds randomizeNodesPositions];
//5
[self addChild:_parallaxNodeBackgrounds];
//6
NSArray *parallaxBackground2Names = @[@"bg_front_spacedust.png",@"bg_front_spacedust.png"];
_parallaxSpaceDust = [[FMMParallaxNode alloc] initWithBackgrounds:parallaxBackground2Names
size:size
pointsPerSecondSpeed:25.0];
_parallaxSpaceDust.position = CGPointMake(0,0);
[self addChild:_parallaxSpaceDust];
编辑:
我能够从上到下移动,但它不会顺利移动.
#pragma mark - TBD - Game Backgrounds
for (int i = 0; i < 2; i++) {
SKSpriteNode * bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
bg.anchorPoint = CGPointZero;
bg.position = CGPointMake(0, i * bg.size.height);
bg.name = @"background";
[self addChild:bg];
}
-(void)update:(NSTimeInterval)currentTime {
[self enumerateChildNodesWithName:@"background" usingBlock: ^(SKNode *node, BOOL *stop) {
SKSpriteNode *bg = (SKSpriteNode *) node;
bg.position = CGPointMake(bg.position.x, bg.position.y-5);
if (bg.position.y <= -bg.size.height) {
bg.position = CGPointMake(bg.position.x , bg.position.y + bg.size.height * 2);
}
}];
}
最佳答案 我还没有测试过您的代码,但是每帧可能有5个点太多,无法顺利移动背景节点?
尝试计算每帧移动的点数:
@implementation GameScene
{
NSTimeInterval _lastUpdateTime;
NSTimeInterval _deltaTime;
}
-(void)update:(NSTimeInterval)currentTime {
if (_lastUpdateTime) {
_deltaTime = currentTime - _lastUpdateTime;
} else {
_deltaTime = 0;
}
_lastUpdateTime = currentTime;
CGPoint bgVelocity = CGPointMake(0.0, -50.0); // Current speed is 50 points per second
CGPoint amtToMove = CGPointMake(bgVelocity.x * _deltaTime, bgVelocity.y * _deltaTime);
[self enumerateChildNodesWithName:@"background" usingBlock: ^(SKNode *node, BOOL *stop) {
SKSpriteNode *bg = (SKSpriteNode *) node;
bg.position = CGPointMake(bg.position.x+amtToMove.x, bg.position.y+amtToMove.y);
if (bg.position.y <= -bg.size.height) {
bg.position = CGPointMake(bg.position.x , bg.position.y + bg.size.height * 2);
}
}];