gpt4 book ai didi

swift - 对象定位已关闭

转载 作者:行者123 更新时间:2023-11-30 10:07:46 25 4
gpt4 key购买 nike

my game scene

嗨,我正在尝试让一个对象(在本例中是绿色 Frog )在与场景一样宽的平台上与玩家 Sprite (红色 Frog )对齐生成,我的意思是,正在让对象生成,以便当玩家前进时它不会与对象重叠。 (图中显示绿色 Frog 位于两只红色 Frog 之间且与其中一只红色 Frog 不在一条直线上)

我的对象定位代码如下

obstacle.position = CGPointMake(-(backgroundSprite.size.width / 2) + CGFloat(randomX) + (spacing * CGFloat(i)), 0)

目前,它在屏幕左侧、场景一半处生成。背景 Sprite 是对象被添加到的对象,其定义如下:

let theSize:CGSize = CGSizeMake(levelUnitWidth, levelUnitHeight)       
let tex:SKTexture = SKTexture(imageNamed: imageName)
backgroundSprite = SKSpriteNode(texture: tex, color: SKColor.blackColor(), size: theSize)

随机 x 也是在背景 Sprite 的 x 轴上随机生成它们的原因(我也尝试过调整,但没有成功)

let randomX = arc4random_uniform(  UInt32 (backgroundSprite.size.height) )

最后,间距是同一级别单位中对象之间的距离。

let spacing:CGFloat = 250

我尝试过实现玩家 Sprite 宽度作为引用,但没有成功。有人可以告诉我我在这里做错了什么吗?

如果您需要查看全部代码,这里是完整的代码:

if (theType == LevelType.road) {

for (var i = 0; i < Int(numberOfObjectsInLevel); i++) {

let obstacle:Object = Object()


obstacle.theType = LevelType.road


obstacle.createObject()
addChild(obstacle)


let spacing:CGFloat = 250
obstacle.position = CGPointMake((backgroundSprite.size.width / 4) + CGFloat(randomX) + (spacing * CGFloat(i)), 0)

}

编辑:

我尝试用我已有的代码来实现您在编辑帖子中编写的代码,这就是我得到的。

  if (theType == LevelType.road) {


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 - thePlayer.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 += thePlayer.size.width/2.0

spawnLocations.append( xPosition )
}

return spawnLocations
}()

print(xAxisSpawnLocations)

let yAxisSpawnLocations: [CGFloat] = [0]

let obstacle:Object = Object()

obstacle.theType = LevelType.road


obstacle.createObject()
addChild(obstacle)

let randx = xAxisSpawnLocations[Int(arc4random_uniform(UInt32(xAxisSpawnLocations.count)))]

obstacle.position = CGPoint(x: randx, y: yAxisSpawnLocations[0] )
obstacle.zPosition = 200

}

编辑2:

所以这次再次以正确的方式实现了代码,我得到了这个:

The second time

所以玩家仍然没有与对象对齐,并且由于某种原因它只在屏幕的右侧生成。我认为这是因为我有一个包含所有内容的 worldNode。

worldNode 保存在 worldNode 中起始点为 (0,0) 的玩家,并且还保存保存对象的关卡单位。相机位置以玩家节点为中心,我不确定这是否是问题所在,但我将提供下面的代码,以便您查看。

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 )

}

我很确定这是我的问题,但请告诉我你的想法。

最佳答案

就像我在评论中所说,关键是预定义 x(和 y)轴的坐标并基于此生成节点。首先,让我们在 GameScene 类中定义一个玩家:

 let player = SKSpriteNode(color: .redColor(), size: CGSize(width: 50, height: 50))

现在,预定义生成位置(x 轴和 y 轴):

let xAxisSpawnLocations: [CGFloat] = [50.0, 125.0, 200.0, 275.0, 350.0, 425.0]
let yAxisSpawnLocations: [CGFloat] = [50.0, 125.0, 200.0, 275.0]

现在,当我们知道可能的位置时,首先定位我们的玩家并将其添加到场景中:

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

您可以根据玩家的宽度、高度以及屏幕尺寸创建这些位置,但为了简单起见,我对所有内容进行了硬编码。

所以,让我们用绿色 Frog 填充玩家正上方的一行:

for xLocation in xAxisSpawnLocations {

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

结果会是这样的:

enter image description here

或者,例如,将玩家向右移动一个位置,并在他的正上方制作一列绿色 Frog :

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


for yLocation in yAxisSpawnLocations {

let greenFrog = SKSpriteNode(color: .greenColor(), size: player.size)
greenFrog.position = CGPoint(x: xAxisSpawnLocations[1], y: yLocation)
addChild(greenFrog)
}

它应该看起来像这样:

enter image description here

编辑:

根据您的评论,您可以根据节点数量、屏幕宽度和节点大小在屏幕上分布节点:

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)

您应该处理添加太多节点或节点太大时发生的情况,但这可以让您了解如何沿 x 轴分布节点并保持它们之间的相同距离。

关于swift - 对象定位已关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35101219/

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