代码之家  ›  专栏  ›  技术社区  ›  Bran

Spritekit地面不接触屏幕底部

  •  1
  • Bran  · 技术社区  · 6 年前

    我正在制作一款spritekit游戏,一切正常,但我的场地在模拟器屏幕的中间。我尽了一切努力让它出现在屏幕的底部。一个功能。

    let groundTexture = SKTexture(imageNamed: "Ground")
    groundTexture.filteringMode = .nearest
    
    for i in stride(from: 0, to: 2 + self.frame.size.width / groundTexture.size().width, by: 1) {
    
        let ground = SKSpriteNode(texture: groundTexture)
        ground.zPosition = 1.0 //-10
        //ground.position = CGPoint(x: (groundTexture.size().width / 2.0 + (groundTexture.size().width * CGFloat(i))), y: groundTexture.size().height / 4.0) //original position 
    
        ground.position = CGPoint(x: 0, y: groundTexture.size().height / +0)  //tried this from a tutorial 
        ground.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.size.width * 2.0, height: groundTexture.size().height / 4.0)) //erase  * 4 test
        ground.setScale(1.2)
        ground.physicsBody?.isDynamic = false
        ground.physicsBody?.allowsRotation  = false
        ground.physicsBody?.affectedByGravity = false
        ground.physicsBody?.categoryBitMask = groundCategory 
    
        //contact and collision bitmask 
        ground.physicsBody?.contactTestBitMask = playerCategory | enemy1Category | enemy2Category | enemy3Category | enemy4Category | obstacleCategory | coinCatergory
        ground.physicsBody?.collisionBitMask = playerCategory | enemy1Category | enemy2Category | enemy3Category | enemy4Category | obstacleCategory | coinCatergory
        ground.physicsBody?.restitution = 0.0
        self.addChild(ground)
    
        let moveLeft = SKAction.moveBy(x: -groundTexture.size().width, y: 0, duration: 5)
        let moveReset = SKAction.moveBy(x: groundTexture.size().width, y: 0, duration: 0)
        let moveLoop = SKAction.sequence([moveLeft, moveReset])
        let moveForever = SKAction.repeatForever(moveLoop)
    
        ground.run(moveForever)
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Ron Myschuk    6 年前

    默认情况下,场景的 anchorPoint 除非另有规定,否则坐标为0,0。 anchorPoint(x: 0, y: 0) 是屏幕的中心。您没有为地面指定位置,因此它会自动添加到场景锚点(屏幕的中间)。

    您需要将场景锚点更改到屏幕底部,或者相应地调整地面位置,例如。。。

    ground.position = CGPoint(x: 0 - self.size.width / 2 + ground.size.width / 2, y: 0 - self.size.height / 2 + ground.size.height / 2)
    

    (上例假设您正在场景中添加地面,并且self=场景)

    供您参考。。。

    enter image description here