How to change SKView mailbox color when using scaleMode.AspectFit

I use the standard full-screen view of SpriteKit from the code template (draft new iOS game), and when I use

scene.scaleMode = .AspectFit 

to make sure the scene is suitable, the remaining (mailbox) area is painted black.

I tried these options to change the color:

 let skView = self.view as SKView // was hoping this one would work skView.backgroundColor = UIColor.redColor() // didn't expect this would work, since scene is scaled anyways scene.backgroundColor = SKColor.redColor() 

I also tried changing the SKView background color in the storyboard editor to a different color, but no luck.

Any tips on where to look to change the color of the mail area?

+5
source share
2 answers

I think it should be a way to set the color or place sprites in the ears of letterhead ... but it seems that they are not. In addition, I do not agree with LearnCocos2D. There is nothing wrong with AspectFit. It has its application.

What I did was use AspectFit, but also calculate the scene size based on the screen size. This is not so useful for an OSX game, but it works great for iOS.

 func createScene() -> GameScene { let screenSize = UIScreen.mainScreen().bounds.size var size:CGSize = CGSizeZero size.height = Constants.sceneHeight size.width = screenSize.width * (size.height / screenSize.height); return GameScene(size: size) } override func viewDidLoad() { super.viewDidLoad() let scene = self.createScene() // Configure the view. let skView = self.view as SKView skView.showsFPS = false skView.showsNodeCount = false /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFit skView.presentScene(scene) } 
+2
source

After rendering the scene, its contents are copied to the view. If the view and scene are the same size, the contents can be directly copied to the view. If they are different from each other, then the scene is scaled to fit the view. The scaleMode property determines how content is scaled.

The mailbox is created in the view, so it is not. This often requires the .aspectFit setting if you want to use a specific scene size and a universal coordinate system. If this is your case, the following should help you.

You need to find out how much standard .aspectFit is required in the add-on to remove the box with your current device and add it to your scene. You end up with a scene that is lighter a little larger or slightly wider than your original size, but the scene is at least the same size as the original size of the scene. Then you can move the anchorPoint to its original (0, 0) coordinates or (0.5, 0.5) depending on which coordinate system you want to use for your game.

The code for this is minimized and may change over time. If you want a quick fix, I created the Framework just for this purpose. It is small but portable :)

https://github.com/Tokuriku/tokuriku-framework-stash

Simply:

  • Download the ZIP file for the repository
  • Open the "SceneSizer" subfolder
  • Drag SceneSizer.framework "lego block" into your project.
  • Make sure the Framework is Embedded, Not Just Linked
  • Import Framework somewhere in your import SceneSizer code

And you're done, now you can call the sizer class with: SceneSizer.calculateSceneSize(#initialSize: CGSize, desiredWidth: CGFloat, desiredHeight: CGFloat) -> CGSize

The documentation is in a folder for clean and full use with a standard scene. Hope this helps!

+1
source

Source: https://habr.com/ru/post/1201679/


All Articles