使用 Sprite Kit 移动背景

Posted

技术标签:

【中文标题】使用 Sprite Kit 移动背景【英文标题】:Moving background with Sprite Kit 【发布时间】:2014-04-07 17:49:44 【问题描述】:

我已经开始使用 Sprite Kit。我正在开发一款具有移动背景的游戏。现在我想知道如果我触摸屏幕如何使背景移动得更快,以及当我松开手指时如何正常移动背景。

我现在正在像这样移动我的背景。

代码开头是这样的:

-(id)initWithSize:(CGSize)size     
     if (self = [super initWithSize:size]) 

     [self addBackground]


;
return self;


 -(void)addBackground


SKTexture* background = [SKTexture textureWithImageNamed:@"backgroundImage"];

SKAction* moveBackground = [SKAction moveByX:-1704*2 y:0 duration:0.004 * background.size.width*2];
SKAction* putOnStart = [SKAction moveByX:1704*2 y:0 duration:0];
SKAction* moveForever = [SKAction repeatActionForever:[SKAction sequence:@[moveBackground, putOnStart]]];

 for( int i = 0; i < 2 + self.frame.size.width / ( background.size.width * 2 ); ++i )      
    SKSpriteNode* sprite = [SKSpriteNode spriteNodeWithTexture:background size:CGSizeMake(1704 ,320)];
         sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2);
    [background runAction:moveForever];
    [self addChild:sprite];

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    touching = YES;


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    touching = NO;

我在视图出现时初始化它。现在问题是我想更新SKAction moveBackround 的持续时间。

【问题讨论】:

【参考方案1】:

抱歉之前的回答。我完全误读了你的问题,喝了太多咖啡:)

看看这段用于移动背景的代码。正如您所问的,它能够以 2 种速度移动,具体取决于是否记录了触摸。

#import "MyScene.h"

// this a from a project I did for an iPhone 5s
// the background image I used is 2048 wide by 640 high and is a @2x.png

 static const float CONSTANT_MOVE_POINTS_PER_SEC = 300.0;

 static inline CGPoint CGPointMultiplyScalar(const CGPoint a,
                                             const CGFloat b)

    return CGPointMake(a.x * b, a.y * b);


 static inline CGPoint CGPointAdd(const CGPoint a,
                                  const CGPoint b)

    return CGPointMake(a.x + b.x, a.y + b.y);


@implementation MyScene 
    SKNode *_bgLayer;
    int _gameSpeedPlus;
    NSTimeInterval _dt;
    NSTimeInterval _lastUpdateTime;


-(id)initWithSize:(CGSize)size     
    if (self = [super initWithSize:size])
    
        _bgLayer = [SKNode node];
        [self addChild:_bgLayer];
        _gameSpeedPlus = 0;

        for (int i = 0; i < 2; i++) 
            SKSpriteNode * bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
            bg.anchorPoint = CGPointZero;
            bg.position = CGPointMake(i * bg.size.width, 0);
            bg.name = @"bg";
            [_bgLayer addChild:bg];
        
    
    return self;


- (void)moveBg 
    CGPoint bgVelocity = CGPointMake(-(CONSTANT_MOVE_POINTS_PER_SEC + _gameSpeedPlus), 0);
    CGPoint amtToMove = CGPointMultiplyScalar(bgVelocity, _dt);
    _bgLayer.position = CGPointAdd(_bgLayer.position, amtToMove);

    [_bgLayer enumerateChildNodesWithName:@"bg"
                               usingBlock:^(SKNode *node, BOOL *stop)
                                   SKSpriteNode * bg = (SKSpriteNode *) node;
                                   CGPoint bgScreenPos = [_bgLayer convertPoint:bg.position
                                                                     toNode:self];
                                   if (bgScreenPos.x <= -bg.size.width) 
                                       bg.position = CGPointMake(bg.position.x+bg.size.width*2,
                                                             bg.position.y);
                               
                           ];


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    _gameSpeedPlus = 200; // change value to increase or decrease speed


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    _gameSpeedPlus = 0;


-(void)update:(CFTimeInterval)currentTime 
    if (_lastUpdateTime) 
        _dt = currentTime - _lastUpdateTime;
     else 
        _dt = 0;
    
    _lastUpdateTime = currentTime;
        [self moveBg];
    


@end

【讨论】:

嘿。但我没有在任何地方调用 changeBackgroundSpeed? 这应该如何工作? SKAction 被创建,然后永远循环。所以它总是低速。 @wumm - 您可以使用 [self runAction:_playerAnimation withKey:@"anyName"] 对 SKSpriteNode 进行 SKAction。然后,您可以随时使用 [self removeActionForKey:@"animation"] 删除(停止)它。无论操作是永远循环还是仅运行一次,这都有效。来源(runAction:withKey:)developer.apple.com/library/ios/documentation/SpriteKit/…:

以上是关于使用 Sprite Kit 移动背景的主要内容,如果未能解决你的问题,请参考以下文章

背景图像大小 Sprite Kit 游戏

从右到左滚动背景 Swift / Sprite Kit

如何使用 Swift 2(Xcode 7)在我的 Sprite Kit 游戏中为背景音乐设置静音按钮?

sprite Kit的奇怪行为

iOS Sprite Kit didBeginContact 无法正常工作

移动端适配之雪碧图(sprite)背景图片定位