对象定位已关闭

时间:2016-01-30 11:30:18

标签: swift object sprite-kit swift2

my game scene

嗨,我正试图让一个物体(在这种情况下是一只绿色的青蛙)与一个平台上的玩家精灵(红色青蛙)一起产生,这个平台和场景一样宽,我的意思是这个,正在使对象产生,以便当玩家前进时它不会与对象重叠。 (图为绿蛙如何在两只红蛙之间,与其中一只红蛙不相符)

我的对象定位代码如下

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

这个目前在屏幕的左侧产生了半个场景。后台精灵是添加对象的内容,其定义如下:

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

随机x也是在背景精灵的x轴上随机生成它们(我也试过调整它没有运气)

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

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

let spacing:CGFloat = 250

我已经尝试将玩家精灵宽度作为参考实现,并且没有奏效。有些人可以告诉我这里我做错了什么。

如果您需要查看全部代码,请参阅以下完整代码:

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  )

}

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

1 个答案:

答案 0 :(得分:1)

就像我在评论中所说的那样,关键是预定义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)

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

所以,让我们在青蛙的玩家正上方填一行:

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

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

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轴分布节点并保持它们之间的相同距离。