i'm new in the development of swift ios game using spirtekit and i need help. I want to develop a vertical 2d endless run, where with a swipe you are able to move the player in direction and if there is an obstacle, it is created in a position calculate with the position of the player. Once create the obstacle i would like to make it falling down but alway with the same relation with the player (this means that it has to move only on y axis). Can you suggest me some ideas on how to manage this?
-
Just add the same move action that you are using on the background to that new object. If I'm reading this correctly your player will not actually "move" the background does, which means anything you place on the screen can just move at the same speed as the background – TheValyreanGroup Apr 25 '17 at 22:49
-
Hi, first thank you for your reply. I think I miss some informations or I wasn't so clear. As i have now is a static background and a player (SKNode) that moves in directions with swipes. After every swipe i need to check some conditions and if verified i have to create an obstacle (SKNode). Once create the obstacle, for creating the illusion of forward movement, I have to move down the obstacle and, of course, also the player but they have to move down simultaneously. I hope that now it's more clear. Thank you! – andrea Apr 26 '17 at 07:41
-
Oh. Just turn gravity on and off when you need it and make sure the nodes have the same mass. – TheValyreanGroup Apr 26 '17 at 13:16
-
I've tried but it's not the result that i want, because the objects created in different moment (let's also say seconds) have different speed. The object created at T is faster than the one created at T+1 and also the player is faster than every other node in the scene. This destroy the position relation between the node in the scene and so i need something different. – andrea Apr 26 '17 at 16:36
1 Answers
This book teaches you exactly how to make what you are looking for:
https://www.amazon.com/Getting-Started-SpriteKit-Jorge-Jordan-ebook/dp/B01891X7TM
This is the end product:
https://itunes.apple.com/lr/app/inside-the-hat/id1076967804?mt=8
You can get it for free here (with trial): https://www.packtpub.com/application-development/getting-started-spritekit
The source code for this is around somewhere on the web, but I forgot where :( I bought the book a year ago and can't find it now.
Basically, you have a long background that is .moveTo
.. in .update
you check the position of the background, then reset it based on its XY position (it basically just goes all the way down, warps to the top position, then falls)
Also in .update
you check for score, or a timer, or just a random check, then spawn an enemy, obstacle, etc. You give the enemy / obstacle SKActions
to move towards the player, and let .didBegin(contact:)
handle scoring, sound effects, death, etc.

- 3,985
- 1
- 13
- 34