gpt4 book ai didi

ios - SKPhysicsBody 对象碰撞未对齐

转载 作者:行者123 更新时间:2023-11-29 02:01:47 24 4
gpt4 key购买 nike

我有一个播放器。他必须跳过一些街区。

但是,似乎 SKPhysicsBody(rectangleOfSize: ... ) 没有正确对齐。当角色撞到方 block 的上半部分时,它只是穿过而他不能从下方穿过,因为它与不可见的 SKPhysicsBody 发生碰撞。

根据我放置 anchor 的方式,有时甚至会发生物理体位于其 SKSpriteNode 左侧或右侧的情况。

我的 block 代码:

    self.smallBlock.anchorPoint = CGPointMake(0.5, 0.5)
self.smallBlock.position = CGPointMake(CGRectGetMaxX(self.frame) + self.smallBlock.size.width, CGRectGetMinY(self.frame) + self.runningBar.size.height * 2)

self.smallBlock.physicsBody = SKPhysicsBody(rectangleOfSize: self.smallBlock.size)
self.smallBlock.physicsBody?.dynamic = false
self.smallBlock.physicsBody?.categoryBitMask = colliderType.Block.rawValue
self.smallBlock.physicsBody?.contactTestBitMask = colliderType.Character.rawValue
self.smallBlock.physicsBody?.collisionBitMask = colliderType.Character.rawValue
self.smallBlock.physicsBody?.usesPreciseCollisionDetection = true

self.addChild(self.smallBlock)

self.character.anchorPoint = CGPointMake(0, 0.25)
self.character.position = CGPointMake(CGRectGetMinX(self.frame) - self.character.size.width, CGRectGetMinY(self.frame) + self.character.size.height)
self.initPosX = CGRectGetMinX(self.frame) + (self.character.size.width * 1.25)

角色代码

    self.character.anchorPoint = CGPointMake(0, 0.25)
self.character.position = CGPointMake(CGRectGetMinX(self.frame) - self.character.size.width, CGRectGetMinY(self.frame) + self.character.size.height)
self.initPosX = CGRectGetMinX(self.frame) + (self.character.size.width * 1.25)

self.character.physicsBody = SKPhysicsBody(rectangleOfSize: self.character.size)
self.character.physicsBody?.affectedByGravity = true
self.character.physicsBody?.categoryBitMask = colliderType.Character.rawValue
self.character.physicsBody?.contactTestBitMask = colliderType.Block.rawValue
self.character.physicsBody?.collisionBitMask = colliderType.Block.rawValue
self.character.physicsBody?.usesPreciseCollisionDetection = true
self.character.physicsBody?.affectedByGravity = true
self.character.physicsBody?.allowsRotation = false

self.addChild(self.character)

最佳答案

解决方法:

我已将 skView.showsPhysics = true 添加到我的 View Controller 。当您在模拟器中运行该应用程序时,它将勾勒出每个对象的轮廓,如图所示 here .

事实证明,设置自定义 anchor 会弄乱 rectangleOfSize 位置。

关于ios - SKPhysicsBody 对象碰撞未对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30276319/

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