Keeping a center point when scene is moving - swift

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.

Related

Are sprites automatically removed?

I'm currently trying to code some falling stars in my game, and they seem to disappear once they move out of the screen. But I'm not sure if swift is actually automatically deleting for me, or if they're still lingering around.
I'm starting them at the top of the screen and using physicsBody to use gravity to bring them down.
star.physicsBody = SKPhysicsBody(rectangleOf: star.size)
I know apple documentation says "Later, if the sprite is removed from the scene or is no longer visible, SpriteKit can delete the texture data if it needs that memory for other purposes."
https://developer.apple.com/documentation/spritekit/skspritenode/getting_started_with_sprite_nodes
but I find that kind of vague and just want some confirmation that because I can't see my star sprite anymore on my screen, I can also assume that it's being removed.
(and not just clumping up somewhere off screen)
No - it's not automatically removed.
By 'removed from scene' the SK documentation doesn't mean that if you can't see it anymore, it means that the 'RemoveFrom...' method was called on the sprite in question.
If the sprite isn't visible, then the game engine won't draw it and could optimise memory by removing it's texture, but the sprite is still being tracked and colliding and bouncing off objects etc.
The SK Scene extends infinitely in all directions and your device's screen is simply a moveable window onto the scene. If you decide in your game that a sprite which moves offscreen is no longer needed, then you need code to detect this and then issue 'RemoveFromParent' against the sprite.

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.

Sprites disappear from scene when change the size of screen

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

To follow a ball character like monkey kick off game

I am working on a game which has working similar to game monkey kick off. I have one ball bouncing on a place at the left side of screen and depending upon the position of the ball I apply linearImpulse to the ball on user touch so that it appears that the ball is kicked.
But what happens is when i apply impulse the ball goes out of the screen bounds. I dont want the ball to go out of the horizontal screen bounds whereas it can go out of the screen vertically.I tried using CCFollow but it doesn't give the realistic feel to the game flow.
I tried THIS tutorial, but dint help much.I managed to scroll the background,Only this part remains.
Any Ideas on how the ball should not move out of the horizontal screen boundary..? but in case of vertical boundary on the other hand it can go out of the bounds.
If you're not using physics, it's a simple bounds check. Assuming the screen width is 320 points this will keep the ball inside the screen horizontally:
CGPoint ballPos = ball.position;
ball.position.x = fmaxf(0, fminf(320, ballPos.x));
ball.position = ballPos;
UPDATE: I noticed you mentioned linear impulse. So you're using a physics engine. In that case, create a horizontal wall on both sides. See the cocos2d Box2D or Chipmunk templates how to enclose the screen with a collision border, then use only the left and right side borders in your game.

Problem regarding CCFollow and CCParallaxNode

I have a problem regarding CCFollow. I am using CCFollow along with CCParallaxNode. I have added layers to parallax node and then apply runaction: on parallax node.
[pn runaction:[CCFollow actionWithTarget:sprite worldBoundary:CGRectMake(0, 0, 5600, 320)]];
but using this the sprite always runs at middle of screen. Is there any way to keep it at left of screen and also layers follow this sprite?
Thanx.
If you want your player to remain on the left side of the screen, then lock the players X position to a specific region that makes sense. You should also be moving the background and other elements of your game, not your player (with exceptions, to the players position visually on the screen).