【问题标题】:How to set the background image for a SKScene?如何为 SKScene 设置背景图像?
【发布时间】:2013-11-05 01:47:09
【问题描述】:

由于目前无法将SKScene 的颜色设置为clearColor,因此

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


        self.backgroundColor = [SKColor clearColor];

    }
    return self;
}

如此处所示:LINK

那么如何为SKScene 设置背景图片?请尽可能具体,示例代码会很棒!

【问题讨论】:

    标签: ios uiview ios7 sprite sprite-kit


    【解决方案1】:

    swift 4 版本:

        let background = SKSpriteNode(imageNamed: "CheckIcon")
        background.size = frame.size
        background.position = CGPoint(x: frame.midX, y: frame.midY)
        addChild(background)
    

    【讨论】:

      【解决方案2】:

      在场景中心使用SKSpriteNode

      -(id)initWithSize:(CGSize)size {    
          if (self = [super initWithSize:size]) {
              // Replace @"Spaceship" with your background image:
              SKSpriteNode *sn = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
      
              sn.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
              sn.name = @"BACKGROUND";
      
              [self addChild:sn];
          }
          return self;
      }
      

      【讨论】:

      • 这部分工作,因为它实际上拉伸了我的图像,不知道为什么会这样做......
      • 如果您的图像名为 Spaceship.png,它会将图像拉伸两倍以用于视网膜显示。您需要一个名为 Spaceship@2x.png 的文件用于视网膜显示器。此文件不应被拉伸。
      • 场景还有一个 scaleMode 属性,它定义了场景的内容如何被缩放。
      • 没有更帅的做法吗?
      【解决方案3】:

      这个解决方案帮助了我:

      将调用/添加场景的代码从viewDidLoad 放到viewWillLayoutSubviews

      - (void)viewWillLayoutSubviews
          {
          [super viewWillLayoutSubviews];
      
          // Configure the view.
          SKView * skView = (SKView *)self.view;
          skView.showsFPS = YES;
          skView.showsNodeCount = YES;
      
          // Create and configure the scene.
          SKScene * scene = [MyScene sceneWithSize:CGSizeMake(skView.bounds.size.width*2,skView.bounds.size.height*2)];
          scene.scaleMode = SKSceneScaleModeAspectFill;
      
          // Present the scene.
          [skView presentScene:scene];
      }
      

      据我所知,原因是skView.bounds在即将布局视图和显示视图时不同。总的来说,我发现heightwidth是切换的。

      我从这里找到了这个解决方案: Background image size Sprite Kit Game

      【讨论】:

        【解决方案4】:

        这是一种更优雅的方法,可以确保无论您现在或将来使用何种分辨率,您的图像都将与背景一样大。

        如果您想支持横幅广告,则必须将高度降低 50 或 32 个设备单位,并将位置向上/向下移动 25 或 16 个像素(纵向或横向)。 如果您想同时支持横向和纵向,我还建议您使用不同的图像,否则您必须开始使用信箱。

        在现场……

        SKSpriteNode* background = [SKSpriteNode spriteNodeWithImageNamed:@"myBackground"];
        background.size = self.frame.size;
        background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        [self addChild:background];
        

        【讨论】:

          猜你喜欢
          • 2014-06-23
          • 1970-01-01
          • 2021-07-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多