aTZ aTZ - 1 month ago 15
Swift Question

Find the relative Y position of a SKNode with swift

I'm trying to adapt a Mega Jump game tutorial (this one) to an endless level. In a part of the code, you make the background and foreground go down off the screen, while the player jumps on the platforms.

The idea is that when a platform goes off the screen, it is deleted and a new platform node is created on the top. The problem is that as the foreground goes "down", I don't know how to calculate where is the highest Y point on the screen so I can create a new platform node on the top.

The part of the code where the background and foreground moves are these ones:

if player.position.y > 200
{
background.position = CGPoint(x: 0, y: -((player.position.y - 200) / 10))
midground.position = CGPoint(x: 0, y: -((player.position.y - 200) / 4))
foreground.position = CGPoint(x: 0, y: -(player.position.y - 200))
}


I hope this makes sense, this is the first time I ask for help here.

Answer Source

Assuming you are adding the platforms to the foreground and your code is in the context of an SKScene class,

platform.position.y = self.frame.maxY - foreground.position.y