gpt4 book ai didi

ios - Spritekit 碰撞

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

我项目中的所有这些代码都运行良好,但我遇到了一些问题。当飞机接触到屏幕的边界时,它会在接触后开始旋转。我不知道如何让它在碰到屏幕边界时不旋转。只有在我使用时才会出现这个问题:

plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)

其他形式的定义我没有问题,但我需要它是纹理的大小。

plane = SKSpriteNode(imageNamed: sett.store() as! String)
plane.size = CGSize(width: 80, height: 80)
plane.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 4)
// scorenode collison off, makes images spin on any contact
plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)
plane.physicsBody?.categoryBitMask = Physics.Plane
plane.physicsBody?.collisionBitMask = Physics.Bird
plane.physicsBody?.contactTestBitMask = Physics.Bird | Physics.Score | Physics.Coin
plane.physicsBody?.affectedByGravity = true
plane.physicsBody?.dynamic = true
plane.zPosition = 2
self.addChild(plane)

// screen boarder so cant leave screen
let boredBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
boredBody.friction = 0
self.physicsBody = boredBody

我的另一个问题又是飞机

plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)

但我也需要这个,所以它是纹理的大小。当它在屏幕上击中我的分数节点时,它应该只增加 1 分,但当它穿过 scoreNode 时它会变化 1-大约 60 分。我该如何解决这个问题?

let scoreNode = SKSpriteNode()
scoreNode.size = CGSize(width: 800, height: 1)
scoreNode.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 350)
scoreNode.physicsBody = SKPhysicsBody(rectangleOfSize: scoreNode.size)
scoreNode.physicsBody?.affectedByGravity = false
scoreNode.physicsBody?.dynamic = false
scoreNode.physicsBody?.categoryBitMask = Physics.Score
scoreNode.physicsBody?.collisionBitMask = 0
scoreNode.physicsBody?.contactTestBitMask = Physics.Plane
scoreNode.color = SKColor.whiteColor()

// when scoreNode and plane touch && initates score and highScore
if firstBody.categoryBitMask == Physics.Score && secondBody.categoryBitMask == Physics.Plane || firstBody.categoryBitMask == Physics.Plane && secondBody.categoryBitMask == Physics.Score {
score += 1
if score >= highScore{
highScore = score
scoreLabel.text = "SCORE: \(score)"
highScoreLabel.text = "HIGH SCORE: \(highScore)"
let highScoreDefault = NSUserDefaults.standardUserDefaults()
highScoreDefault.setValue(highScore, forKey: "highScore")
highScoreDefault.synchronize()
} else {
scoreLabel.text = "SCORE: \(score)"
highScoreLabel.text = "HIGH SCORE: \(highScore)"
}
}

最佳答案

它在旋转,因为碰撞在物理体上施加了角速度。这是应该的。如果你真的不想让一个物体有角速度,你可以通过设置节点物理体来实现

allowsRotation 属性设置为 false。

在这种情况下,它会是这样的:

scoreNode.physicsBody?.allowsRotation = false

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/instp/SKPhysicsBody/allowsRotation

关于ios - Spritekit 碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39322544/

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