首页 文章

SpriteKit:SKSpriteNodes 之间的差距

提问于
浏览
1

当蓝色方块落在红色方块上时,将留下间隙。但是当我让蓝色方块从较低位置掉下来时,没有间隙。此外,当我给蓝色正方形一个更高的质量时,红色正方形下的间隙(在地面上)消失,当质量太高时,红色正方形消失了。我还把方块缩小了,因为像素数量不等于 png 文件的像素,这导致了更大的差距。当我将“showsPhysics”设置为 true 时,还有一个间隙可以看到甚至更大,当正方形没有按比例缩小时。

SKSpriteNodes 之间的差距(图片)

//: Playground - noun: a place where people can play

import UIKit
import SpriteKit
import PlaygroundSupport

class Scene: SKScene {

var square = SKSpriteNode(texture: SKTexture(imageNamed: "red"), size: SKTexture(imageNamed: "red").size())
var square2 = SKSpriteNode(texture: SKTexture(imageNamed: "blue"), size: SKTexture(imageNamed: "blue").size())
var label = SKLabelNode(fontNamed: "Chalkduster")

override func didMove(to view: SKView) {
    self.addChild(square)
    self.addChild(square2)
    self.addChild(label)
}

override func didChangeSize(_ oldSize: CGSize) {

    // Add Scene Physics
    self.physicsBody?.usesPreciseCollisionDetection = true
    self.physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(x: -1, y: -1, width: self.size.width + 2, height: self.size.height + 2))
    self.backgroundColor = UIColor.white
    self.physicsBody?.usesPreciseCollisionDetection = true

    //Add square
    square.texture?.usesMipmaps = true
    square.zPosition = 11
    square.physicsBody = SKPhysicsBody(rectangleOf: square.size)
    square.physicsBody?.usesPreciseCollisionDetection = true
    square.physicsBody?.mass = 0.1
    square.physicsBody?.allowsRotation = false
    square.physicsBody?.usesPreciseCollisionDetection = true
    square.setScale(0.25)
    square.position = CGPoint(x: self.frame.width / 2, y: self.square.size.height)
    square.name = "square"

    //Add square2
    square2.texture?.usesMipmaps = true
    square2.zPosition = 11
    square2.physicsBody = SKPhysicsBody(rectangleOf: square2.size)
    square2.physicsBody?.usesPreciseCollisionDetection = true
    square2.physicsBody?.mass = 0.1
    square2.physicsBody?.allowsRotation = false
    square2.physicsBody?.usesPreciseCollisionDetection = true
    square2.setScale(0.25)
    square2.position = CGPoint(x: self.frame.width / 2, y: self.square.size.height * 6)
    square2.name = "square2"

    //Add label
    label.fontSize = 30
    label.text = "w: \(self.frame.size.width) h: \(self.frame.size.height)"
    label.position = CGPoint(x: self.frame.size.width / 2, y: self.frame.size.height - 30)
    label.fontColor = UIColor.black
    label.zPosition = 100

}
}

class ViewController: UIViewController {

let scene = Scene()
var viewSize = CGSize(width: 0, height: 0)

override func loadView() {
    self.view = SKView()

}

override func viewDidLoad() {
    super.viewDidLoad()

    if let view = self.view as! SKView? {
        view.frame = CGRect(origin: CGPoint(x: -1, y: -1), size: viewSize)
        view.presentScene(scene)
        view.showsPhysics = false
        view.showsFPS = true
        view.showsNodeCount = true

    }

}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    scene.size = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height)
}
}
PlaygroundPage.current.liveView = ViewController()

2 回答

  • 0

    作为print命令的证明,精灵节点之间没有实际的间隙,并且来自 super-zoomed-in-screenshot 的第二个证明:

    在此输入图像描述

    你提供的游乐场不会复制一个问题,这让我想知道......你的项目是怎么回事?

    第 2 页:

    第 2 页:

  • 0

    场景的宽度和高度必须乘以 2.因此场景必须比 ViewControllers 视图大 4 倍。它也就像 Apple 为 Spritekit 提供的游戏模板中的那样。

    scene.size = CGSize(width: self.view.frame.size.width * 2, height: self.view.frame.size.height * 2)
    

    https://github.com/simon2204/Playground

相关问题