gpt4 book ai didi

iphone - 使用特定的 image.png 或 image.jpg 调整 SKSpriteNode 和 SKPhysicsBody

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

我正在开发一个具有某种特定形状的角色的游戏,我正在使用此行创建一个 SKSpriteNode 对象:

let rockl:SKSpriteNode = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: newweight*ancho/16, height: alto/5))

我用这一行指定一个物理实体:

rockl.physicsBody = SKPhysicsBody(rectangleOfSize: pipeBotton.size)

我用这一行指定图像:

rockl.texture = SKTexture(imageNamed: "character.png")
rockl.texture.filteringMode = SKTextureFilteringMode.Nearest

但是边框与图像的边框不一样。如何将对象调整为图像?或者创建自定义形状的技术是什么?

最佳答案

您需要使用 SKPhyiscsBody(texture, size) 创建您的 physicalsBody 。来自文档:

Use this method when your sprite has a shape that you want replicated in its physics body. The texture is scaled to the new size and then analyzed. A new physics body is created that includes all of the texels in the texture that have a nonzero alpha value. The shape of this body attempts to strike a good balance between performance and accuracy. For example, fine details may be ignored if keeping them would cause a significant performance penalty.

您还可以直接使用纹理创建 SKSpriteNode,而不是先使用平面颜色创建它:

let rocklTexture = SKTexture(imageNamed: "character.png")
rocklTexture.filteringMode = SKTextureFilteringMode.Nearest

let rocklSize = CGSize(width: newweight*ancho/16, height: alto/5)

let rockl = SKSpriteNode(texture: rocklTexture, size: rocklSize)
rockl.physicsBody = SKPhysicsBody(texture: rocklTexture, size: rocklSize)

关于iphone - 使用特定的 image.png 或 image.jpg 调整 SKSpriteNode 和 SKPhysicsBody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25651657/

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