gpt4 book ai didi

swift - WorldNode 与对象生成冲突

转载 作者:行者123 更新时间:2023-11-28 06:49:30 25 4
gpt4 key购买 nike

这个问题是关于这个问题的:

Object positioning is off

于是我再次实现了代码:

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])

}

这次是正确的方法,我得到了这个:

The second time

所以玩家仍然没有与对象对齐,并且出于某种原因它只在屏幕的右侧生成。这是因为我有一个包含所有内容的 worldNode,并且相机位置位于 worldNode 内的 Sprite 上。 (这是我得出的结论)

worldNode 包含在 worldNode 中起点为 (0,0) 的玩家,它还包含包含对象的关卡单元。摄像机位置以玩家节点(在世界节点中)为中心

我试图让对象在整个 levelUnit 上生成,而不仅仅是在它的一侧。

我将提供以下代码:

玩家的起始位置:

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(保存对象),但它仍然不起作用。

有人可以告诉我我做错了什么吗?

最佳答案

问题出在以下行:

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

i 的值是从 0 到 numberOfNodes-1。这意味着 xPosition 从 0 到某个正数不等。但是,正如您所说,相机位于 (0,0),因此所有绿色 Frog 都将布置在相机的右侧。您需要计算出第一只 Frog 接近 -frame.maxX/2 而最后一只 Frog 接近 +frame.maxX/2。简单(或懒惰)的方法是从结果中减去它。即。

xPosition -= frame.maxX/2

关于swift - WorldNode 与对象生成冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35305497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com