Sprites disappear from scene when change the size of screen - swift

I am coding my first game by using sprite kit.
I added boundary around my scene but when I have different screens for different devices, it shows that there is some gap region and the sprites will be disappear from the screen when they reach that gap region. I would like to know how to set the boundary automatically to be exactly the screen size when changing the screen size?

Try this
let scene = GameScene(size: skView.bounds.size)
This will take your current game scene, scene and set the boundaries of it to that of the screen.
Hope that helps :D

Related

Changing camera in two different scenes in Unity

I have a mobile game, and this game have two different scenes. I want to do in first scene the camera should be vertical, and in second scene the camera should be horizontal. However, when I change the scene the camera always be vertical. How can I fix this.
You can attach a script to your cameras and use the following to change the screen orientation:
i.e. to set screen vertical
Screen.orientation = ScreenOrientation.Portrait;

Keeping a center point when scene is moving

I am new to swift and making a game where the SKCamera is moving on the scene but I wanna keep the center of the scene updated when skcamera moves along. Just wondering what can I use.
thank you
What do you mean "keep the center of the scene updated when skcamera moves along" If you are using a camera, the camera is always the the center of the screen, and the entire scene is always updating. I am going to take a stab in the dark here and say you are not setting scene.camera = SKCameraNode. Other than this, you will need to clarify what you are asking for.
Use this to set the camera to the ball after it passes a certain point:
camera.y = ball.position.y > scene.height/2 ? ball.position.y : scene.height / 2
If you add a SKSpriteNode as child of your camera node, and you set it to the center of the camera node, it will stay at the center of the screen, even when the camera moves.

Make a Defender-like "world" with SKTileMapNode

Defender is an ancient game that presents a virtual world, a horizontal scroller, in a loop. The camera view only portrays a portion of the world at any given time. The player can fly in both directions around this world, continuously.
How could this be achieved with an SKTileMapNode Layout of the world?
My first thought is to have two instances of a SKTileMapNode that illustrates the whole world, and abut them in space where and when required as the player flies around the world.
Here's a map of the Defender world highlighted at the top of the screen:
The minimap in the screen shot you show indicates it is not endlessly scrolling, it’s just really long, with only a small part visible at any time.
Doing this with SKTilemapNode would involve one SKTilemapNode and adding tiles only for the ground, which you could then attach physics bodies to. The background could be a single image or multiple images to create a parallax effect or you could go with a black colour background and stars created with SpriteKit Particles.
An alternate approach would be to use two background images and position them programmatically so as one image scrolls offscreen, it is moved left or right (depending on which way the user is scrolling) to appear adjacent to the other background image, giving the appearance of an endless scrolling background.

Creating layers with different Z-Axis positions in SpriteKit?

The scene
I was wondering about creating
different layers with different Z-Axis values to give more reality like the above picture as it has
1- The green background then
2- The play ground it self then
3- The blurred black trees representing the camera depth of field
So, I thought about Creating the green background then the the black ground with more value of zPosition then the blurred stuff scaled up with more zPosition values, but the problem is when camera moves there is no sense of reality of speed of movement for each layer as they all move together respecting the same positions .
Also I thought about using SceneKit instead as it contains full 3D tools , but the scene is 2D and does not seem to need scenekit.
Thanks in advance as the question seemed too complicated.
Okay, it is figured now.
The answer is gonna be that I put a method that makes the background moves against the camera movement direction with one half or third the camera speed.
As an example
If the camera moves 20 pixels to right , all scene layers seems to be moved 20 pixels to left . So I have to move the background about 8
pixels to right
So all layers seem to be moved 20 pixels to left except that background moved 12 to left, which is slower.

Unity coordinates doesn't work

I'm very beginner in Unity so please forgive if, this questions isn't so hard to answer:)
So, I have a text on a Canvas in the editor, it is okay, it's showing well on Scene editor and In Game as well.
But, when I added two Sprites, which going to be the player and the enemy, the positions of these sprites are behave a bit weird.
The text position is: x: -293 y: 195, when I'm modifying the position of the text it works fine.
When I add the sprites to x:0 y:0 and x:1 y:1, in the scene editor they appear in the left bottom corner, but when I check in the game, they placed in the middle of the screen.
My question is why the coordinates and the positions are so different on Scene (grey) and on Game (blue) ?
Because initialized render mode of Canvas in Unity is "ScreenSpace - Overlay". So it is shown on too big area in scene. If you want to work only in view field of camera, in inspector just change render mode of Canvas to "ScreenSpace-Camera" and drag your MainCamera to RenderCamera in inspector. Even if you use ScreenSpace-Camera, coordinate system of RectTransform (UI Objects transform) is different than Transform (Normal game objects transform)
in this view, if you get closer to the left-down corner of your scene, you will see your main camera area and Sprites that are in correct positions.
I hope this helps.