我目前正在开发一款游戏,每次点击屏幕时都会有一个主playerSprite
跳跃。每隔几分钟,一个fattySprite
节点从天空落到playerSprite
顶部。每增加一个节点堆叠,跳跃高度就会降低。为什么跳高会减少?我已尝试在fattySprite
节点中将各种physicsBody属性设置为0,以防止这些额外节点影响跳跃高度。
代码段:
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
self.anchorPoint = CGPoint(x: 0, y: 0)
// Character creation
self.playerSprite = PlayerNode(position: CGPoint(x: self.frame.size.width / 9, y: self.frame.size.height / 4),
size: CGSize(width: self.frame.size.width / 5, height: self.frame.size.height / 10))
self.playerSprite?.physicsBody?.categoryBitMask = playerCategory
self.playerSprite?.physicsBody?.contactTestBitMask = playerCategory | trapCategory
self.playerSprite?.physicsBody?.affectedByGravity = true
self.playerSprite?.physicsBody?.restitution = 0.0
self.playerSprite?.physicsBody?.friction = 0
self.addChild(self.playerSprite!)
self.playerSprite?.beginAnimation()
// Fatties spawning
let wait_fatty = SKAction.wait(forDuration: 3, withRange: 2)
let spawnFatty = SKAction.run({
let fattySprite = PlayerNode(position: CGPoint(x: self.frame.size.width / 9, y: self.frame.size.height),
size: CGSize(width: self.frame.size.width / 5, height: self.frame.size.height / 10))
fattySprite.physicsBody?.categoryBitMask = self.fattiesCategory
fattySprite.physicsBody?.contactTestBitMask = self.fattiesCategory | self.playerCategory
fattySprite.physicsBody?.mass = 0
fattySprite.physicsBody?.allowsRotation = false
fattySprite.physicsBody?.linearDamping = 0
fattySprite.physicsBody?.angularDamping = 0
fattySprite.physicsBody?.restitution = 0.0
fattySprite.physicsBody?.friction = 0
self.addChild(fattySprite)
})
let fattySequence = SKAction.sequence([wait_fatty, spawnFatty])
self.run(SKAction.repeatForever(fattySequence))
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if self.playerSprite?.action(forKey: "jump") == nil { // check that there's no jump action running
//self.playerSprite?.physicsBody?.velocity = CGVector(dx: 0, dy: 500 )
self.playerSprite?.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 500))
}
}
答案 0 :(得分:0)
您必须在为其提供值之前创建一个物理实体。 在所有这些对fattySprite.physicsbody的调用中?您正在尝试将值分配给nil变量。
这样做: fattySprite.physicsbody = SKPhysicsbody(rectangleOf:fattySprite.size)
然后分配所有物理属性。我看不出你是如何构建你的playernode的,可能有更多适当的physicsbody-init函数。检查文档:) https://developer.apple.com/reference/spritekit/skphysicsbody