Astrum
Astrum

Reputation: 375

WorldNode conflicting with object spawning

This question is in reference to this one:

Object positioning is off

so I implemented the code again:

let xAxisSpawnLocations: [CGFloat] = {

    var spawnLocations:[CGFloat] = []

    //Create 5 possible spawn locations
    let numberOfNodes = 5

for i in 0...numberOfNodes - 1 {

    /*
        Spacing between nodes will change if:

        1) number of nodes is changed,
        2) screen width is changed,
        3) node's size is changed.
    */

    var xPosition = (frame.maxX - player.size.width) / CGFloat((numberOfNodes - 1)) * CGFloat(i)

    //add a half of a player's width because node's anchor point is (0.5, 0.5) by default
    xPosition += player.size.width/2.0

    spawnLocations.append( xPosition )
}

return spawnLocations
 }()

print(xAxisSpawnLocations)

        player.position = CGPoint(x: xAxisSpawnLocations[0], y: yAxisSpawnLocations[0])
        player.zPosition = 10
        addChild(player)

        //Fill one row (horizontally) right above the player with green frogs

        for xLocation in xAxisSpawnLocations {

           let greenFrog = SKSpriteNode(color: .greenColor(), size: player.size)
           greenFrog.position = CGPoint(x: xLocation, y: yAxisSpawnLocations[0])
           greenFrog.zPosition = 9000
           addChild(greenFrog)
        }

         player.position = CGPoint(x: xAxisSpawnLocations[1], y: yAxisSpawnLocations[0])

    }

this time the right way and I got this:

The second time

So the player still isn't in line with the objects and for some reason it only spawns on the right side of the screen. this is due to the fact that I have a worldNode that holds everything and that the camera position is positioned on a Sprite that is within the worldNode. (Which is what i concluded)

the worldNode holds the player which has a starting point of (0,0) in the worldNode and it also holds the level units which holds the objects. The camera position is centered on the player node(which is in the worldNode)

I'm trying to get the objects to spawn right across the levelUnit not just on one side of it.

I'll provide the code below:

Player's Starting Position:

let startingPosition:CGPoint = CGPointMake(0, 0)

The woldNode Code:

let worldNode:SKNode = SKNode()

//creates the world node point to be in the middle of the screen
self.anchorPoint = CGPointMake(0.5, 0.5)
addChild(worldNode)

//adds the player as a child node to the world node 
worldNode.addChild(thePlayer)
thePlayer.position = startingPosition
thePlayer.zPosition = 500   

The camera positioning code:

override func didSimulatePhysics() {

self.centerOnNode(thePlayer) 
}

//centers the camera on the node world.

func centerOnNode(node:SKNode) {

let cameraPositionInScene:CGPoint = self.convertPoint(node.position, fromNode: worldNode)
worldNode.position = CGPoint(x: worldNode.position.x , y:worldNode.position.y - cameraPositionInScene.y  )

 }

I tried using the levelUnit (that holds the objects) in the positioning code but it still doesn't work.

can someone please tell me what I'm doing wrong.

Upvotes: 1

Views: 57

Answers (1)

Michael
Michael

Reputation: 9044

The problem is with the following line:

var xPosition = (frame.maxX - player.size.width) / CGFloat((numberOfNodes - 1)) * CGFloat(i)

The value of i is from 0 to numberOfNodes-1. This means xPosition varies from 0 to some positive number. However, as you've said the camera is positioned at (0,0), so all of the green frogs will be laid out to the right of the camera. You need the maths to end up such that the first frog is near -frame.maxX/2 and the last is near +frame.maxX/2. The easy (or lazy) way would be to just subtract this from your result. ie.

xPosition -= frame.maxX/2

Upvotes: 1

Related Questions