WorldNode与对象生成冲突 [英] WorldNode conflicting with object spawning

查看:119
本文介绍了WorldNode与对象生成冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于这个的:

This question is in reference to this one:

对象定位已关闭

所以我再次实现了代码:

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:

因此,播放器仍未与对象对齐,并且由于某种原因,它仅在屏幕的右侧生成.这是由于我有一个握住所有东西的worldNode,并且相机位置位于worldNode内的Sprite上. (我的结论是什么)

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)

worldNode拥有一个玩家,该玩家在worldNode中的起点为(0,0),并且还拥有用于放置对象的关卡单位.相机位置位于玩家节点(位于worldNode中)的中心

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)

我正在尝试使对象直接在levelUnit上生成,而不仅仅是在其一侧.

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

我将提供以下代码:

玩家的起始位置:

let startingPosition:CGPoint = CGPointMake(0, 0)

woldNode代码:

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   

相机定位代码:

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  )

 }

我尝试在定位代码中使用levelUnit(用于保存对象),但是仍然无法正常工作.

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.

推荐答案

问题出在以下几行:

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

i的值是从0到numberOfNodes-1.这意味着xPosition从0到某个正数变化.但是,正如您所说的,相机位于(0,0),因此所有绿色青蛙都将布置在相机的右侧.您需要进行数学运算以使第一个青蛙在-frame.maxX/2附近,而最后一个青蛙在+frame.maxX/2附近.一种简单(或懒惰)的方法是从结果中减去该值.即.

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

这篇关于WorldNode与对象生成冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆