gpt4 book ai didi

swift - 矩形物理体引起角度变化

转载 作者:行者123 更新时间:2023-11-28 08:08:13 29 4
gpt4 key购买 nike

我有一个矩形 Sprite 构成我的屏幕边框。我还有较小的移动矩形 Sprite ,它们以直线、水平线移动。

即使它们在移动(因为矩形物理体), Sprite 也不应该旋转。出于某种原因,敌舰 Sprite 在击中边界 Sprite 几次后开始旋转。

我下面的代码(攻击船旋转不正确):

    self.anchorPoint = CGPoint.init(x: 0.5, y: 0.5)
setUpHeaderUI()
self.physicsWorld.gravity = CGVector.zero
self.physicsWorld.contactDelegate = self
createBG()
addShip()

self.physicsBody = SKPhysicsBody.init(edgeLoopFrom: self.frame)
self.physicsBody?.collisionBitMask = shipCategory | enemyShipCategory
self.physicsBody?.categoryBitMask = wallCategory
self.physicsBody?.friction = 0

func addAttackShip(){
//30,40,40
let enemy = attackShip.init()
enemy.setScale(0.08)
enemy.zRotation = CGFloat(0)
enemy.physicsBody = SKPhysicsBody.init(rectangleOf: enemy.size)
enemy.physicsBody?.isDynamic = true
enemy.name = "enemy"
enemy.zPosition = 2
enemy.physicsBody?.mass = CGFloat.init(10)
enemy.physicsBody?.friction = 0
enemy.physicsBody?.categoryBitMask = enemyShipCategory
enemy.physicsBody?.collisionBitMask = enemyShipCategory | wallCategory
enemy.physicsBody?.contactTestBitMask = greenBulletCategory | shipCategory
enemy.position = CGPoint.init(x: 0, y: 155.504 - enemy.size.height / 2)
enemy.zRotation = CGFloat(2.4870942)

let healthindicator = SKSpriteNode.init(color: UIColor.green, size: CGSize.init(width: 150, height: 30))
healthindicator.name = "healthdisplay"
healthindicator.zPosition = 3
healthindicator.zRotation = -1 * CGFloat(2.4870942)
enemy.addChild(healthindicator)
addChild(enemy)
}

最佳答案

你试过吗?

let enemy = SKSpriteNode()
enemy.physicsBody!.allowsRotation = false
// Put the correct value in the skrange:
enemy.constraints = [SKConstraint.zRotation(SKRange(constantValue: 0)]

关于swift - 矩形物理体引起角度变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44510155/

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