gpt4 book ai didi

swift - : SKPhysicsBody(polygonFromPath: CGPath) using Swift and SpriteKit 有问题

转载 作者:搜寻专家 更新时间:2023-11-01 07:28:35 24 4
gpt4 key购买 nike

我正在尝试为 SKSpriteNode 创建一个物理体。我通常使用图像创建物理体并说:

snode = SKSpriteNode(imageNamed: snodeImage)
snode = SKPhysicsBody(texture: snode.texture!, size: snode.size)

我希望能够使用以下方法创建物理体:

snode.physicsBody = SKPhysicsBody(polygonFromPath: CGPath)

我不知道该怎么做,我试过在线查看,但我无法弄明白。有人可以给我看一个小例子吗?也许向我展示如何使用此构造函数创建一个简单的矩形物理体?

最佳答案

最简单的方法是使用 UIBezierPath,因为它有许多有用的构造函数,然后将其转换为 CGPath polygonFromPath 想要。

您需要一些代码来创建一个简单的矩形物理体:

let square = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 128, height: 128))
square.position = CGPoint(x: CGRectGetMidX(frame), y: CGRectGetMidY(frame))

let path = UIBezierPath(rect: CGRect(x: -64, y: -64, width: 128, height: 128))
square.physicsBody = SKPhysicsBody(polygonFromPath: path.CGPath)

addChild(square)

请注意,物理体是偏移的,因为它是从其节点的中心开始测量的。您可能希望在 GameViewController.swift 文件中使用 skView.showsPhysics = true 来帮助您调试物理形状。

关于swift - : SKPhysicsBody(polygonFromPath: CGPath) using Swift and SpriteKit 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34246492/

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